Results 1 to 3 of 3
  1. #1
    adeeb is offline Member
    Join Date
    May 2008
    Posts
    29
    Rep Power
    0

    Default Regarding Listener

    Hi,
    I used a JButton in an application, which handles a particular function but i can only click it mouse how can i use or activate the enter key. So that i can handle it from keyboard also.
    Thanks and Regards.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    how about KeyListener?

    I forgot what is the "Enter" key ascii code....
    but you can use the KeyEvent's instance to access it's member/method that retrieves all info about the event made...

    The best approach is to have some experiments on it....
    freedom exists in the world of ideas

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

    Default

    It sounds as if you want to set your button as the default button for that window. This can be easily done by calling the JRootPane method, setDefaultButton(...). For example:
    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    class DefaultButtonTest extends JPanel
    {
      private JButton buttonOK = new JButton("OK");
      private JButton buttonCancel = new JButton("Cancel");
      private JTextField textfield = new JTextField(12);
      private JFrame frame;
    
      DefaultButtonTest(JFrame frame)
      {
        this.frame = frame;
        ActionListener buttonActionListener = new ButtonActionListener();
        buttonOK.addActionListener(buttonActionListener);
        buttonCancel.addActionListener(buttonActionListener);
    
        add(textfield);
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
        buttonPanel.add(buttonOK);
        buttonPanel.add(buttonCancel);
        add(buttonPanel);
    
        frame.getRootPane().setDefaultButton(buttonOK);
      }
    
      private class ButtonActionListener implements ActionListener
      {
        @Override
        public void actionPerformed(ActionEvent e)
        {
          String command = e.getActionCommand();
          if (command.equalsIgnoreCase("OK"))
          {
            System.out.println("OK pressed");
            System.out.print("Text in textfield: ");
            System.out.println(textfield.getText());
          }
          else if (command.equalsIgnoreCase("Cancel"))
          {
            frame.dispose();
          }
        }
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("SwingFu1");
        frame.getContentPane().add(new DefaultButtonTest(frame));
        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. add an undo listener to a Jtable
    By christina in forum Advanced Java
    Replies: 12
    Last Post: 01-29-2009, 08:47 AM
  2. Listener for JFrame size change
    By Thez in forum AWT / Swing
    Replies: 10
    Last Post: 02-14-2008, 04:10 PM
  3. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 10:04 AM
  4. Listener collision on game
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 08:48 AM
  5. Adding listener to non-Java object?
    By cruxblack in forum Advanced Java
    Replies: 5
    Last Post: 07-30-2007, 03:19 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
  •