zzynx
asked on
Pressing <Enter> differs from pressing <Space Bar> on a focused button
Hi experts,
I have created a wizard panel containing 6 pages.
The panel has a <Previous> and a <Next> button to browse through the pages.
Simple ActionListener's are listening to the pressing of those two buttons.
Problem:
I'm on the 1st page and I tab to give the <Next> button the focus.
Now, when I press the space bar I jump to the 2nd page.
However, when I press <Enter> I jump to the 3rd page. (pressing again brings me to the 5th page)
What happens?
I think when I press <Enter> I jump to the next page and then for some reason
the <Next> button is triggered again, so it brings me in fact two pages further.
Somebody knows any solution or workaround?
Thanks in advance.
I have created a wizard panel containing 6 pages.
The panel has a <Previous> and a <Next> button to browse through the pages.
Simple ActionListener's are listening to the pressing of those two buttons.
Problem:
I'm on the 1st page and I tab to give the <Next> button the focus.
Now, when I press the space bar I jump to the 2nd page.
However, when I press <Enter> I jump to the 3rd page. (pressing again brings me to the 5th page)
What happens?
I think when I press <Enter> I jump to the next page and then for some reason
the <Next> button is triggered again, so it brings me in fact two pages further.
Somebody knows any solution or workaround?
Thanks in advance.
Can you post some of your code. Specifically the code that processes the Next button event.
space will activate the focussed button
enter will activate the default button
add some debug to detemine what events are getting fired
enter will activate the default button
add some debug to detemine what events are getting fired
ASKER
Nothing special in there:
nextFinishButton.addAction Listener(n ew java.awt.event.ActionListe ner() {
public void actionPerformed(java.awt.e vent.Actio nEvent evt) {
nextFinishButtonActionPerf ormed(evt) ;
}
});
private void nextFinishButtonActionPerf ormed(java .awt.event .ActionEve nt evt) {
if ( nextFinishButton.getText() .equals(
UserEnvironment.getDefault ().getTran slation("n ext") ) )
next(); // <<<<<<< Is performed twice when <Enter> is pressed. Once when space bar is pressed
else
finish();
}
nextFinishButton.addAction
public void actionPerformed(java.awt.e
nextFinishButtonActionPerf
}
});
private void nextFinishButtonActionPerf
if ( nextFinishButton.getText()
UserEnvironment.getDefault
next(); // <<<<<<< Is performed twice when <Enter> is pressed. Once when space bar is pressed
else
finish();
}
try setting the default button
and post the stack traces for the three events getting fired.
and post the stack traces for the three events getting fired.
Try making this mod and see what events are being fired. You don't seem to be checking, just blindly processing next() on any event.
private void nextFinishButtonActionPerf ormed(java .awt.event .ActionEve nt evt) {
System.err.println("nextFi nishButton ActionPerf ormed() event="+evt);
if ( nextFinishButton.getText() .equals(
UserEnvironment.getDefault ().getTran slation("n ext") ) )
next(); // <<<<<<< Is performed twice when <Enter> is pressed. Once when space bar is pressed
else
finish();
}
private void nextFinishButtonActionPerf
System.err.println("nextFi
if ( nextFinishButton.getText()
UserEnvironment.getDefault
next(); // <<<<<<< Is performed twice when <Enter> is pressed. Once when space bar is pressed
else
finish();
}
ASKER
>> add some debug to detemine what events are getting fired
I added the line
System.out.println("ID = " + evt.getID() + " paramString = " + evt.paramString());
in the function nextFinishButtonActionPerf ormed()
I get:
When <Enter> is pressed:
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next, when=11490 83188829,m odifiers=
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next, when=11490 83188829,m odifiers=
When <Space> is pressed:
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next, when=11490 83210790,m odifiers=
When <Enter> is pressed:
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next, when=11490 83215397,m odifiers=
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next, when=11490 83215397,m odifiers=
>> try setting the default button
How? It's a JPanel
>> and post the stack traces for the three events getting fired.
three events?
I added the line
System.out.println("ID = " + evt.getID() + " paramString = " + evt.paramString());
in the function nextFinishButtonActionPerf
I get:
When <Enter> is pressed:
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next,
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next,
When <Space> is pressed:
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next,
When <Enter> is pressed:
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next,
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next,
>> try setting the default button
How? It's a JPanel
>> and post the stack traces for the three events getting fired.
three events?
You need to get the Root Pane from your JFrame to set the default button.
JRootPane.setDefaultButton (javax.swi ng.JButton )
JRootPane.setDefaultButton
> How? It's a JPanel
you set it on the root pane that contains the panel
> three events?
the one when space is pressed
and two when enter is pressed
you set it on the root pane that contains the panel
> three events?
the one when space is pressed
and two when enter is pressed
You could also try calling the evt.consume() method right before you call the next() function. That should stop the event from being propigated after you processed it.
ASKER
>> You could also try calling the evt.consume() method right before you call the next() function.
Compile Error: consume() has protected access in java.awt.AWTEvent
Compile Error: consume() has protected access in java.awt.AWTEvent
ASKER
Problem solved!
The panel was wrapped in an EscDialog (extending JDialog) to make it disappear when <Escape> is pressed.
That EscDialog also captured the pressing of the <Enter> key.
And in this specific case, this code was performed:
DefaultKeyboardFocusManage r kfMgr = new DefaultKeyboardFocusManage r();
Component compFocused = kfMgr.getFocusOwner();
if (compFocused instanceof JButton) {
((JButton)compFocused).doC lick();
}
So, this triggered the <Next> button for a second time besides the normal trigger.
You couldn't know all that ;°)
Nevertheless, thanks for trying to help me. Appreciated.
I'll ask for a delete of this one.
The panel was wrapped in an EscDialog (extending JDialog) to make it disappear when <Escape> is pressed.
That EscDialog also captured the pressing of the <Enter> key.
And in this specific case, this code was performed:
DefaultKeyboardFocusManage
Component compFocused = kfMgr.getFocusOwner();
if (compFocused instanceof JButton) {
((JButton)compFocused).doC
}
So, this triggered the <Next> button for a second time besides the normal trigger.
You couldn't know all that ;°)
Nevertheless, thanks for trying to help me. Appreciated.
I'll ask for a delete of this one.
> That EscDialog also captured the pressing of the <Enter> key.
Thats because it is the default problem as I mentioned earlier.
The stack trace would have shown it up.
Thats because it is the default problem as I mentioned earlier.
The stack trace would have shown it up.
I explained what the reason was, and suggested diagnostics to determine the cause.
ASKER
>> Thats because it is the default problem as I mentioned earlier.
>> I explained what the reason was
Incorrect. That EscDialog really contained code I wrote myself to capture the Enter key.
If that code wouldn't have been there, pressing <Enter> or <Space bar> would have been exactly the same.
So
>> space will activate the focussed button
>> enter will activate the default button
was not the explanation.
And here's a little demo program to prove:
No matter how I trigger the (default!) <Increment> button the counter always increment once.
ButtonTest.java
------------------
/**
*
* @author zzynx
*/
public class ButtonTest extends javax.swing.JFrame {
private int counter = 1;
/** Creates new form ButtonTest */
public ButtonTest() {
initComponents();
updateInfo();
getRootPane().setDefaultBu tton(incBu tton);
}
private void updateInfo() {
theLabel.setText("Counter is " + counter);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
centerPanel = new javax.swing.JPanel();
theLabel = new javax.swing.JLabel();
southPanel = new javax.swing.JPanel();
buttonPanel = new javax.swing.JPanel();
decButton = new javax.swing.JButton();
incButton = new javax.swing.JButton();
addWindowListener(new java.awt.event.WindowAdapt er() {
public void windowClosing(java.awt.eve nt.WindowE vent evt) {
exitForm(evt);
}
});
theLabel.setText("jLabel1" );
centerPanel.add(theLabel);
getContentPane().add(cente rPanel, java.awt.BorderLayout.CENT ER);
southPanel.setLayout(new java.awt.FlowLayout(java.a wt.FlowLay out.RIGHT) );
decButton.setText("Decreme nt");
decButton.addActionListene r(new java.awt.event.ActionListe ner() {
public void actionPerformed(java.awt.e vent.Actio nEvent evt) {
decButtonActionPerformed(e vt);
}
});
buttonPanel.add(decButton) ;
incButton.setText("Increme nt");
incButton.addActionListene r(new java.awt.event.ActionListe ner() {
public void actionPerformed(java.awt.e vent.Actio nEvent evt) {
incButtonActionPerformed(e vt);
}
});
buttonPanel.add(incButton) ;
southPanel.add(buttonPanel );
getContentPane().add(south Panel, java.awt.BorderLayout.SOUT H);
pack();
}
private void incButtonActionPerformed(j ava.awt.ev ent.Action Event evt) {
counter++;
updateInfo();
}
private void decButtonActionPerformed(j ava.awt.ev ent.Action Event evt) {
counter--;
updateInfo();
}
/** Exit the Application */
private void exitForm(java.awt.event.Wi ndowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
// Set the system's Look & Feel
try {
UIManager.setLookAndFeel(U IManager.g etSystemLo okAndFeelC lassName() );
}
catch (Exception e) {}
ButtonTest frame = new ButtonTest();
frame.setLocationRelativeT o(null);
frame.show();
}
// Variables declaration - do not modify
private javax.swing.JPanel buttonPanel;
private javax.swing.JPanel centerPanel;
private javax.swing.JButton decButton;
private javax.swing.JButton incButton;
private javax.swing.JPanel southPanel;
private javax.swing.JLabel theLabel;
// End of variables declaration
}
>> I explained what the reason was
Incorrect. That EscDialog really contained code I wrote myself to capture the Enter key.
If that code wouldn't have been there, pressing <Enter> or <Space bar> would have been exactly the same.
So
>> space will activate the focussed button
>> enter will activate the default button
was not the explanation.
And here's a little demo program to prove:
No matter how I trigger the (default!) <Increment> button the counter always increment once.
ButtonTest.java
------------------
/**
*
* @author zzynx
*/
public class ButtonTest extends javax.swing.JFrame {
private int counter = 1;
/** Creates new form ButtonTest */
public ButtonTest() {
initComponents();
updateInfo();
getRootPane().setDefaultBu
}
private void updateInfo() {
theLabel.setText("Counter is " + counter);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
centerPanel = new javax.swing.JPanel();
theLabel = new javax.swing.JLabel();
southPanel = new javax.swing.JPanel();
buttonPanel = new javax.swing.JPanel();
decButton = new javax.swing.JButton();
incButton = new javax.swing.JButton();
addWindowListener(new java.awt.event.WindowAdapt
public void windowClosing(java.awt.eve
exitForm(evt);
}
});
theLabel.setText("jLabel1"
centerPanel.add(theLabel);
getContentPane().add(cente
southPanel.setLayout(new java.awt.FlowLayout(java.a
decButton.setText("Decreme
decButton.addActionListene
public void actionPerformed(java.awt.e
decButtonActionPerformed(e
}
});
buttonPanel.add(decButton)
incButton.setText("Increme
incButton.addActionListene
public void actionPerformed(java.awt.e
incButtonActionPerformed(e
}
});
buttonPanel.add(incButton)
southPanel.add(buttonPanel
getContentPane().add(south
pack();
}
private void incButtonActionPerformed(j
counter++;
updateInfo();
}
private void decButtonActionPerformed(j
counter--;
updateInfo();
}
/** Exit the Application */
private void exitForm(java.awt.event.Wi
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
// Set the system's Look & Feel
try {
UIManager.setLookAndFeel(U
}
catch (Exception e) {}
ButtonTest frame = new ButtonTest();
frame.setLocationRelativeT
frame.show();
}
// Variables declaration - do not modify
private javax.swing.JPanel buttonPanel;
private javax.swing.JPanel centerPanel;
private javax.swing.JButton decButton;
private javax.swing.JButton incButton;
private javax.swing.JPanel southPanel;
private javax.swing.JLabel theLabel;
// End of variables declaration
}
ASKER
So as I said, I appreciate you both were trying to help me.
But, as Venabili always says, points are for answers.
But, as Venabili always says, points are for answers.
And doing a strack trace of the event being fired as I suggested would have made it obvious where the events whetre getting generated from. Its not always answers that solve problems :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Muy recomendantion appears to have been ignored. My suggestion to check the stack trace would have pointed immediately to the cause of the problem.