Again about default button

Discussion in 'Java' started by ffellico@inwind.it, Oct 29, 2005.

  1. Guest

    Yes. Yet I am unable to do it!

    I read many of us asking for this need, but no one of the answers seem
    to solve the problem.

    As a sample I have a frame with only a jTextField and two jButtons
    (jButton1 and jButton2).

    I would like to fire automatically the jButton2 hitting ENTER when I am
    writing in the jTextField.

    Can anyone write for me the code to do this?

    Thank you. Franco.
     
    , Oct 29, 2005
    #1
    1. Advertising

  2. zero Guest

    wrote in news:1130566018.989557.54330
    @g44g2000cwa.googlegroups.com:

    > Yes. Yet I am unable to do it!
    >
    > I read many of us asking for this need, but no one of the answers seem
    > to solve the problem.
    >
    > As a sample I have a frame with only a jTextField and two jButtons
    > (jButton1 and jButton2).
    >
    > I would like to fire automatically the jButton2 hitting ENTER when I am
    > writing in the jTextField.
    >
    > Can anyone write for me the code to do this?
    >
    > Thank you. Franco.
    >
    >


    Just add the same ActionListener to both. That way the same will happen
    when you hit enter on the textfield or press the button. Use a different
    listener (or a way to distinguish the source, eg event.getSource()) for the
    second button.

    ActionListener myActionListener = new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    // ...
    }
    };

    textField.addActionListener(myActionListener);
    button.addActionListener(myActionListener);
    button2.addActionListener(new ActionListener
    {
    // ...
    });
     
    zero, Oct 29, 2005
    #2
    1. Advertising

  3. Guest

    Hi.

    You solved me also this problem ( before you suggest me the solution
    for the statusbar at the bottom of a frame).

    This is a new step for me in learning and using Java. Thank you.
    Franco from Italy.
     
    , Oct 29, 2005
    #3
  4. wrote:
    >
    > You solved me also this problem ( before you suggest me the solution
    > for the statusbar at the bottom of a frame).
    >
    > This is a new step for me in learning and using Java. Thank you.
    > Franco from Italy.
    >


    A more generic solution for this is to use the "defaultButton" property
    of the rootPane on the Window/Frame/Dialog. See the API docs for the
    setDefaultButton method in the class javax.swing.JRootPane.

    BK
     
    Babu Kalakrishnan, Oct 30, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. amit
    Replies:
    0
    Views:
    394
  2. che
    Replies:
    2
    Views:
    501
  3. abcd

    Importing again and again

    abcd, Jun 8, 2006, in forum: Python
    Replies:
    9
    Views:
    330
    Maric Michaud
    Jun 9, 2006
  4. Replies:
    4
    Views:
    407
  5. Ò»Ê×Ê«

    A design problem I met again and again.

    Ò»Ê×Ê«, Apr 1, 2009, in forum: Python
    Replies:
    24
    Views:
    728
    Dennis Lee Bieber
    Apr 4, 2009
Loading...

Share This Page