This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

How can I add mouse click to inputMap of JFormattedTextField

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic