• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField getText() no Enter pressed  RSS feed

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
call textfield.getText()method on actionlistener of the button.
 
Pres Brawner
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
});
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!