?
Solved

Pressing <Enter> differs from pressing <Space Bar> on a focused button

Posted on 2006-05-31
19
Medium Priority
?
435 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:zzynx
  • 7
  • 6
  • 4
  • +1
18 Comments
 
LVL 2

Expert Comment

by:appxpete
ID: 16798209
Can you post some of your code.  Specifically the code that processes the Next button event.
0
 
LVL 92

Expert Comment

by:objects
ID: 16798258
space will activate the focussed button
enter will activate the default button

add some debug to detemine what events are getting fired
0
 
LVL 37

Author Comment

by:zzynx
ID: 16798284
Nothing special in there:

    nextFinishButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextFinishButtonActionPerformed(evt);
            }
        });

    private void nextFinishButtonActionPerformed(java.awt.event.ActionEvent evt) {
        if ( nextFinishButton.getText().equals(
                UserEnvironment.getDefault().getTranslation("next") ) )
            next();     // <<<<<<< Is performed twice when <Enter> is pressed. Once when space bar is pressed
        else
            finish();
    }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 16798338
try setting the default button

and post the stack traces for the three events getting fired.

0
 
LVL 2

Expert Comment

by:appxpete
ID: 16798458
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 nextFinishButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.err.println("nextFinishButtonActionPerformed() event="+evt);
        if ( nextFinishButton.getText().equals(
                UserEnvironment.getDefault().getTranslation("next") ) )
            next();     // <<<<<<< Is performed twice when <Enter> is pressed. Once when space bar is pressed
        else
            finish();
    }
0
 
LVL 37

Author Comment

by:zzynx
ID: 16798567
>> 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 nextFinishButtonActionPerformed()

I get:

When <Enter> is pressed:
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next,when=1149083188829,modifiers=
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next,when=1149083188829,modifiers=
When <Space> is pressed:
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next,when=1149083210790,modifiers=
When <Enter> is pressed:
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next,when=1149083215397,modifiers=
ID = 1001 paramString = ACTION_PERFORMED,cmd=Next,when=1149083215397,modifiers=

>> try setting the default button
How? It's a JPanel

>> and post the stack traces for the three events getting fired.
three events?
0
 
LVL 2

Expert Comment

by:appxpete
ID: 16798613
You need to get the Root Pane from your JFrame to set the default button.

JRootPane.setDefaultButton(javax.swing.JButton)
0
 
LVL 92

Expert Comment

by:objects
ID: 16798622
> 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

0
 
LVL 2

Expert Comment

by:appxpete
ID: 16798653
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.
0
 
LVL 37

Author Comment

by:zzynx
ID: 16798716
>> 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
0
 
LVL 37

Author Comment

by:zzynx
ID: 16799101
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:

        DefaultKeyboardFocusManager kfMgr = new DefaultKeyboardFocusManager();
        Component compFocused = kfMgr.getFocusOwner();

        if (compFocused instanceof JButton) {
            ((JButton)compFocused).doClick();
        }

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.
0
 
LVL 92

Expert Comment

by:objects
ID: 16802614
> 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.

0
 
LVL 92

Expert Comment

by:objects
ID: 16803139
I explained what the reason was, and suggested diagnostics to determine the cause.
0
 
LVL 37

Author Comment

by:zzynx
ID: 16805228
>> 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().setDefaultButton(incButton);
    }
   
    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.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        theLabel.setText("jLabel1");
        centerPanel.add(theLabel);

        getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER);

        southPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        decButton.setText("Decrement");
        decButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                decButtonActionPerformed(evt);
            }
        });

        buttonPanel.add(decButton);

        incButton.setText("Increment");
        incButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                incButtonActionPerformed(evt);
            }
        });

        buttonPanel.add(incButton);

        southPanel.add(buttonPanel);

        getContentPane().add(southPanel, java.awt.BorderLayout.SOUTH);

        pack();
    }

    private void incButtonActionPerformed(java.awt.event.ActionEvent evt) {
        counter++;
        updateInfo();
    }

    private void decButtonActionPerformed(java.awt.event.ActionEvent evt) {
        counter--;
        updateInfo();
    }
   
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent 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(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {}
       
        ButtonTest frame = new ButtonTest();
        frame.setLocationRelativeTo(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
   
}
0
 
LVL 37

Author Comment

by:zzynx
ID: 16805234
So as I said, I appreciate you both were trying to help me.
But, as Venabili always says, points are for answers.
0
 
LVL 92

Expert Comment

by:objects
ID: 16805255

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 :)

0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 16876751
Closed, 500 points refunded.
ee_ai_construct
Community Support Moderator
replacement part #xm34
0
 
LVL 92

Expert Comment

by:objects
ID: 16883961
Muy recomendantion appears to have been ignored. My suggestion to check the stack trace would have pointed immediately to the cause of the problem.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question