Results 1 to 5 of 5
  1. #1
    jperson is offline Member
    Join Date
    Jan 2010
    Posts
    61
    Rep Power
    0

    Default problem in my button

    i want my buttom excute action when i press ENTER_KRY
    how can i do this ????

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Is this a Swing application, and are you using a JButton? Or is this AWT? SWT? Other? Details can mean a lot I'm afraid. If Swing, then you can set the root pane's default button via its setDefaultButton(JButton defaultButton) method.

    Perhaps if you tell us more, we can tell you more. Much luck!

  3. #3
    jperson is offline Member
    Join Date
    Jan 2010
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Is this a Swing application, and are you using a JButton? Or is this AWT? SWT? Other? Details can mean a lot I'm afraid. If Swing, then you can set the root pane's default button via its setDefaultButton(JButton defaultButton) method.

    Perhaps if you tell us more, we can tell you more. Much luck!

    thank you for your reply

    my application is swing app using JBuuton , please give me a small example. :confused:

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jperson View Post
    thank you for your reply

    my application is swing app using JBuuton , please give me a small example. :confused:
    Read about the ActionListener interface; you have to implement that interface and register it at your JButton. The JButton will call it when it is pressed.

    kind regards,

    Jos

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jperson View Post
    thank you for your reply

    my application is swing app using JBuuton , please give me a small example. :confused:
    Have at it.
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class SwingFu {
    
      private static void createAndShowUI() {
        JButton nonDefaultBtn = new JButton("Not Default Button");
        JButton defaultBtn = new JButton("Default Button");
        final JTextField textfield = new JTextField(20);
        textfield.setEditable(false);
        textfield.setFocusable(false);
        
        ActionListener actionlistener = new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            textfield.setText(e.getActionCommand() + " pressed");
          }
        };
        nonDefaultBtn.addActionListener(actionlistener);
        defaultBtn.addActionListener(actionlistener);
        
        
        JPanel panel = new JPanel();
        panel.add(nonDefaultBtn);
        panel.add(defaultBtn);
        panel.add(textfield);
        
        JFrame frame = new JFrame("Main App");
        
        // ***** HERE *****
        // Get the JFrame's JRootPane
        // and Set the default button
        frame.getRootPane().setDefaultButton(defaultBtn);    
        
        frame.getContentPane().add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowUI();
          }
        });
      }
    }

Similar Threads

  1. Problem w/ ActionListener on a button
    By qwertyuiop23 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2009, 06:25 AM
  2. Problem with mozilla back button.
    By shivakumari in forum Java Servlet
    Replies: 3
    Last Post: 05-07-2009, 04:19 AM
  3. problem with back button of the browser.
    By shivakumari in forum Java Servlet
    Replies: 2
    Last Post: 03-25-2009, 07:41 AM
  4. problem on button
    By udayforu_1986 in forum Java Applets
    Replies: 1
    Last Post: 08-03-2008, 01:57 PM
  5. java applet button problem .. :( plz help
    By i4gotmyid in forum New To Java
    Replies: 0
    Last Post: 04-05-2008, 09:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •