• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I add mouse click to inputMap of JFormattedTextField  RSS feed

 
Abhijeet Vaidya
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to perform some action in editor of a JFormattedTextField. So I have added "Tab" and "Enter" in the input map and specified actionMap for it.



ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), AppConstants.CHECK);

ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), AppConstants.CHECK);

ftf.getActionMap().put(AppConstants.CHECK, new AbstractAction() {
public void actionPerformed(ActionEvent e) {

// custom code
}});


here ftf is JFormattedTextField.


How can I add mouse click to the input map the way I added Keystroke.getKeyStroke(KeyEvent.VK_TAB, 0)?

Right now when I enter the value and click the mouse button, ftf does not recognize it as valid input.



Thanks,
Abhijeet
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhijeet Vaidya:
I want to perform some action in editor of a JFormattedTextField. So I have added "Tab" and "Enter" in the input map and specified actionMap for it.

ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), AppConstants.CHECK);

ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), AppConstants.CHECK);


I'm not sure the TAB will work, because I think the focus-handling code grabs the tab before the component gets it. I haven't tried it recently, though, so perhaps I'm wrong.

The ENTER will work, but this will clobber the existing entry ("notify-field-accept") in the InputMap. This means that ftf.addActionCommand(...), which is the normal way to get the field to do something when the user hits the enter key, will no longer work.

As for mouse clicks, the InputMap/ActionMap will not involved.

You haven't said exactly what you are trying to do, but wouldn't it be easier to customize the field's Editor than to put listeners on the field itself?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just guessing here, but are you perhaps trying to validate the field content when the user is leaving it?

If that's the case, the easiest way to do this would be to register a FocusListener on the field and react to the focusLost event.
 
Abhijeet Vaidya
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your prompt reply..

What I am trying to do is -

I have to validate dates entered in the JTable. So I have created a class as the editor for JTable cells. The formattedTextField I am getting, by calling the getComponent() method of the DefaultCellEditor. I want to validate the date entered by the user, when he leaves the cell.


Thanks,
Abhijeet
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
I'm just guessing here, but are you perhaps trying to validate the field content when the user is leaving it?

If that's the case, the easiest way to do this would be to register a FocusListener on the field and react to the focusLost event.


If it's really a JFormattedTextField, then I would have to disagree. JFormattedTextField does its own validation when focus is lost.

Originally posted by Abhijeet Vaidya:
What I am trying to do is -

I have to validate dates entered in the JTable. So I have created a class as the editor for JTable cells. The formattedTextField I am getting, by calling the getComponent() method of the DefaultCellEditor. I want to validate the date entered by the user, when he leaves the cell.


I'm a bit confused, as DefaultCellEditor.getComponent() simply returns the component passed in to its constructor. Did you instantiate a JFormattedTextField to pass in?

btw, it's possible to use JFormattedTextFields in table cell editors, of course, but it's sometimes more trouble than it's worth. It's often easier to do the validation in your table model's setValueAt() method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!