aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTextField getText() no Enter pressed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField getText() no Enter pressed" Watch "JTextField getText() no Enter pressed" New topic
Author

JTextField getText() no Enter pressed

Pres Brawner
Ranch Hand

Joined: Jan 18, 2001
Posts: 92
I've got a form. When the user enters data into the JTextField, and then presses a JButton (not ENTER), I want to get the contents of the JTextfield.
Thanks,
Pres
selvas kumars
Ranch Hand

Joined: Jan 06, 2001
Posts: 115
call textfield.getText()method on actionlistener of the button.
Pres Brawner
Ranch Hand

Joined: Jan 18, 2001
Posts: 92
I'm not sure what you mean by that. I've tried something, and it did not work.
Could you give an example?
Thanks,
Pres
Dean Reedy
Ranch Hand

Joined: Sep 10, 2001
Posts: 89
Here is a example of mine sending the text in the txtName field to the m_data.retrievedata function:
txtName = new JTextField(10);
panel12.add( txtName );
cmdQuery = new JButton("Query");
cmdQuery.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Dean2;
Dean2 = txtName.getText();
m_data.retrieveData( Dean2 );
System.out.println("Error");
m_table.repaint();
}
});
Tim Tully
Greenhorn

Joined: Sep 16, 2001
Posts: 8
add a KeyListener to the JTextField like this:
JTextField f = new JTextField(10);
f.addKeyListener(new KeyListener()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER
{
//get Textfield text here
String text = f.getText();
}
}
//implement keyReleased(KeyEvent)
//implement keyTyped(KeyEvent)
});
Pres Brawner
Ranch Hand

Joined: Jan 18, 2001
Posts: 92
Thanks Tim, but I think that Dean has the rights of it. See the original post. The whole point is to not use ENTER which is the default trigger in a JTextField. The triggering event needs to be when the JButton gets selected by the user.
Thanks to all replies.
Pres
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextField getText() no Enter pressed