Link to home
Start Free TrialLog in
Avatar of jonathanjeffrey
jonathanjeffrey

asked on

Java Swing... How to get text from a JTextField

Hello,

I have several JTextFields.  I type some values into them, and then click a button, which performs a calculation.

The problem is, to get it to work i have to hit "enter" in each textfield before clicking the button, otherwise my program doesnt read in the values from the text boxes.

What do i need to do with the textboxes so that I am not required to hit the "enter" key?
I am currently using the
.getText() function to get the value from the textbox.

Thanks,
Jonathan.
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

You shouldn't have to do that. Can you post your code?
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For example, if you slap this code into a JPanel, the fourth textfield will display correctly the values in the others.

private JTextField jtf1 = new JTextField("Hello ");
private JTextField jtf2 = new JTextField("Good-");
private JTextField jtf3 = new JTextField("Buy");
private JTextField jtf4 = new JTextField();
private JPanel jp1 = new JPanel();
private JButton jb = new JButton("push me");

public void actionPerformed(ActionEvent e){

jtf4.setText(jtf1.getText()+jtf2.getText()+jtf3.getText());

}
Avatar of sciuriware
sciuriware

:)
So what happened to this idea then : >> ... and then click a button<<