Results 1 to 11 of 11
  1. #1
    Collinryans is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default How would I go about doing this?

    I have a jTextField and a jButton. Normally what happens is user enters something in the jTextField and clicks the button and:

    Java Code:
     private void jButtonEnterActionPerformed(java.awt.event.ActionEvent evt) {
    }
    is executed, as you would expect.

    However what I want is, for the user to be able to simple press enter after putting something in the jTextField and

    Java Code:
     private void jButtonEnterActionPerformed(java.awt.event.ActionEvent evt) {
    }
    will execute.

    I am using Netbeans.

    Previously I tried this:

    I added

    Java Code:
        private void jTextFieldInputKeyPressed(java.awt.event.KeyEvent evt) {
    
            if(evt.getKeyCode() == 10) {
              
              
              // same as code jButtonEnterActionPerformed executed here.
             
    
            }
        }

    However I would prefer not to do this is just repeating the same line of codes as another function, and I am sure there is a simpler method. So could someone point me to the right direction?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Java Code:
    public void actionPerfomed(ActionEvent e){
      doStuff();
    }
    
    private void jTextFieldInputKeyPressed(java.awt.event.KeyEvent evt) {
      if(evt.getKeyCode() == 10) {        
        doStuff();
      }
    }
    private void doStuff(){
      //whatever
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    I'm not a Swing programmer, but I think there are Action items of some kind that you can bind to components to trap key presses.

    Where is the jTextFieldInputKeyPressed method connected to anything? Is this an IDE thing?

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    @Norm: you're thinking about key bindings, but in the end you'll end up having duplicate code unless you're calling a method from both places.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    PHP Code:
    package chararray;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    
    public class GUI extends JFrame {
    
        JButton jb;
        JTextField tf;
    
        public GUI(){
    
            super("Title");
            setLayout(new FlowLayout());
    
    
            jb = new JButton("button");
            add(jb);
    
            tf = new JTextField("",11);
            add(tf);
    
    
          jb.addActionListener(
                  new ActionListener(){
                      public void actionPerformed(ActionEvent e){
                            tf.setText("you pressed enter");
                       }
                  }
          );
    
        }
    
    
    }
    making form visible from main class
    PHP Code:
    package chararray;
    
    public class Main {
    
        public static void main(String[] args) {
    
            GUI form = new GUI();
            form.setVisible(true);
            form.setSize(200, 200);
        }
    }
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Wouldn't using an Action instance eliminate the code duplication?

    The button can be created with the constructor that takes an Action argument so that the action gets invoked when the button is clicked. And the text field's addActionListener() is called with the Action as an argument so that it gets invoked when <Enter> is typed into the field.

  7. #7
    Collinryans is offline Member
    Join Date
    May 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    Java Code:
    public void actionPerfomed(ActionEvent e){
      doStuff();
    }
    
    private void jTextFieldInputKeyPressed(java.awt.event.KeyEvent evt) {
      if(evt.getKeyCode() == 10) {        
        doStuff();
      }
    }
    private void doStuff(){
      //whatever
    }
    This is great, it's currently what I'm using right now, thanks a lot.

    Quote Originally Posted by pbrockway2 View Post
    Wouldn't using an Action instance eliminate the code duplication?

    The button can be created with the constructor that takes an Action argument so that the action gets invoked when the button is clicked. And the text field's addActionListener() is called with the Action as an argument so that it gets invoked when <Enter> is typed into the field.
    Exactly how is this done?

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Exactly how is this done?
    Read the section from the Swing tutorial on How to Use Actions.

    Another option might be to create a default button for the frame:

    Java Code:
    frame.getRootPane().setDefaultButton(...);

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    All of this is a bit overboard, isn't it?

    You can simply add an ActionListener to the JTextField
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    
    public class TestTextField {
    	public static void main(String[] args) {
    		final JFrame jf = new JFrame();
    		final JTextField jtf = new JTextField();
    		jtf.addActionListener(
    			new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					JOptionPane.showMessageDialog(jf, jtf.getText());
    				}
    			});
    		jf.getContentPane().add(jtf);
    		jf.pack();
    		jf.setVisible(true);
    	}
    }
    Edit: Okay, I see pbrockway already mentioned this.

  10. #10
    rp.uh is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Default

    how about
    jButton.doClick();

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    If you don't need the button pressed, call the actionPerformed() (or whatever method it calls) method directly.

Posting Permissions

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