JFormattedTextField on a JDialog with defaultButton()
Hello all,
Searching for "JFormattedTextField defaultButton" returns two results in this forum, both of them didn't get answered and I am currently stuck with this problem.
Let me explain,
By default JFormattedTextField consumes the ENTER and ESCAPE keys, which is fine by me.
The RootPane.setDefaultButton() also works when the user presses the ENTER key, unless the key is pressed in a JComponent that doesn't consume it.
So I am stuck, I would like to be able to use JFormattedTextFields and be able to use the setDefaultButton() feature.
As you all know this will cause the following effect. The user focuses on a JFormattedTextField, he enters a value and presses enter, Nothing Happens, he presses enter again and then the defaultButton() comes into action.
Since the two post were a couple of months ago, I was wondering if anyone has any developments or workarounds on this. Bellow you can see an example of what I am talking about. Notice that the button action gets called on the second ENTER keypress.
I am running Java 1.5.06
import java.awt.Dimension;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
publicclass TestFormattedTextFieldextends JFrame{
public TestFormattedTextField(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(500,500));
initComponents();
}
privatevoid initComponents(){
JPanel panel =new JPanel();
JFormattedTextField daField =new JFormattedTextField(NumberFormat.getNumberInstance());
daField.setColumns(20);
JButton defButton =new JButton("Default Button");
getRootPane().setDefaultButton(defButton);
panel.add(daField);
panel.add(defButton);
add(panel);
}
publicstaticvoid main(String[] args){
TestFormattedTextField tftf =new TestFormattedTextField();
tftf.setVisible(true);
}
}
[3003 byte] By [
Gyftusa] at [2007-10-3 5:14:34]

needs *major* testing
import java.awt.*;
import java.text.NumberFormat;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
class TestFormattedTextField extends JFrame{
JButton defButton = new JButton("Default Button");
boolean buttonPressed = false;
public TestFormattedTextField(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(500,500));
initComponents();
}
private void initComponents(){
JPanel panel = new JPanel();
JFormattedTextField daField = new JFormattedTextField(NumberFormat.getNumberInstance());
daField.setColumns(20);
getRootPane().setDefaultButton(defButton);
panel.add(daField);
panel.add(defButton);
add(panel);
defButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.out.println("OK");
buttonPressed = true;
}
});
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addKeyEventDispatcher(new KeyEventDispatcher(){
public boolean dispatchKeyEvent(KeyEvent ke){
if(ke.getID() == KeyEvent.KEY_RELEASED && ke.getKeyCode() == KeyEvent.VK_ENTER)
{
if(buttonPressed == false) defButton.doClick();
buttonPressed = false;
}
return false;
}
});
}
public static void main(String[] args) {
TestFormattedTextField tftf = new TestFormattedTextField();
tftf.setVisible(true);
}
}
Thank you for your answer, could you point me to some good documentation of the KeyboardFocusManager? I read about it on the swing focus tutorials but had problem getting it.
What implications can this method have?
I noticed you have a flag buttonPressed, is this necessary to avoid multiple events being dispatched?
> could you point me to some good documentation of the KeyboardFocusManager
this might get you started
http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html
and
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/KeyEventDispatcher.html
> What implications can this method have?
should be none, but testing will confirm.
> I noticed you have a flag buttonPressed, is this necessary to avoid multiple
> events being dispatched?
if the button is going to fire on [ENTER] (as the default button), it should fire
before the keyReleased event in the KeyBoardFocusManager code,
setting buttonPressed to true, therefore defButton.doClick(); will not be called
See also http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4741926
JayDSa at 2007-7-14 23:21:06 >

Hello again,
I will get on with reading the links you gave me.
I tested this method and I noticed the following:
1. If you raise an option pane from that dialog and you press enter, the defButton will be called.
2. If you press enter on a combo box after you have selected the item, the defButton will be called.
I looking for ways around them, if I ever get to figuring it out properly, I will post the code here
> 1. If you raise an option pane from that dialog and you press enter, the defButton will be called.
> 2. If you press enter on a combo box after you have selected the item, the defButton will be called.
part of the *major* testing to see how it affects the actual app.
another potential couple of issues:
actually click the button with the mouse, or tab to the button and press the
spacebar will set the flag, then if you later press enter, it shouldn't work the
first time ('enter' will reset the flag)
so, depending on the actual application, if there are too many 'side-effects',
better workarounds might be to use KeyBindings, perhaps redispatch the event
Actually I was thinkin about redispatching the event, but I am a total newb on that subject, and I am doing some researchBest Regards,Fotis