Results 1 to 11 of 11
Thread: How would I go about doing this?
- 06-23-2010, 08:10 PM #1
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
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:
is executed, as you would expect.Java Code:private void jButtonEnterActionPerformed(java.awt.event.ActionEvent evt) { }
However what I want is, for the user to be able to simple press enter after putting something in the jTextField and
will execute.Java Code:private void jButtonEnterActionPerformed(java.awt.event.ActionEvent evt) { }
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?
- 06-23-2010, 08:56 PM #2
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
- 06-23-2010, 10:33 PM #3
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?
- 06-23-2010, 10:46 PM #4
@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
- 06-24-2010, 01:52 AM #5
making form visible from main classPHP 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"); } } ); } }
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
- 06-24-2010, 02:03 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 06-24-2010, 04:56 AM #7
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
- 06-24-2010, 06:51 AM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Read the section from the Swing tutorial on How to Use Actions.Exactly how is this done?
Another option might be to create a default button for the frame:
Java Code:frame.getRootPane().setDefaultButton(...);
- 06-24-2010, 07:01 AM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
All of this is a bit overboard, isn't it?
You can simply add an ActionListener to the JTextField
Edit: Okay, I see pbrockway already mentioned this.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); } }
- 06-24-2010, 08:35 PM #10
Member
- Join Date
- Jun 2010
- Posts
- 1
- Rep Power
- 0
how about
jButton.doClick();
- 06-24-2010, 08:37 PM #11


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks