Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextField getText() no Enter pressed

 
Pres Brawner
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
 
selvas kumars
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
 
Dean Reedy
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();
}
});
 
Tim Tully
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