You are setting the name of the JTextField - fine. You should be able to refer to that:
Version 1:
A FocusListener reacts when you leave the textfield:
public class JFrameExample {
private JFrame frame;
private JButton button;
private final String nameTField = "tField";
private int count;
public JFrameExample() {
count = 0;
}
private void displayGUI() {
frame = new JFrame("JFrame Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0, 1, 2, 2));
button = new JButton("Add JTextField");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
JTextField tfield = new JTextField();
tfield.setName(nameTField + count);
tfield.addFocusListener(new MyFocusListener());
count++;
frame.add(tfield);
frame.getContentPane().validate();
frame.repaint();
}
});
frame.add(button);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JFrameExample().displayGUI();
}
});
}
private class MyFocusListener implements FocusListener{
@Override
public void focusLost(FocusEvent event) {
if(event.getSource() instanceof JTextField){
JTextField txtField = (JTextField)event.getSource();
System.out.println("Hello, I'm TextField " + txtField.getName());
System.out.println("My Value is:" + txtField.getText());
}
}
@Override
public void focusGained(FocusEvent event) { }
}
}
Version 2:
A KeyListener reacts when you type. Benefit: you can refer to the value at any time. Only use this version when you need to refer to it any time (e.g. instant search), cause it fires an event on every button stroke:
public class JFrameExample {
private JFrame frame;
private JButton button;
private final String nameTField = "tField";
private int count;
public JFrameExample() {
count = 0;
}
private void displayGUI() {
frame = new JFrame("JFrame Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0, 1, 2, 2));
button = new JButton("Add JTextField");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
JTextField tfield = new JTextField();
tfield.setName(nameTField + count);
tfield.addKeyListener(new MyKeyListener());
count++;
frame.add(tfield);
frame.getContentPane().validate();
frame.repaint();
}
});
frame.add(button);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JFrameExample().displayGUI();
}
});
}
private class MyKeyListener implements KeyListener{
@Override
public void keyReleased(KeyEvent event) {
if(event.getSource() instanceof JTextField){
JTextField txtField = (JTextField)event.getSource();
System.out.println("Hello, I'm TextField " + txtField.getName());
System.out.println("My Value is:" + txtField.getText());
}
}
@Override
public void keyTyped(KeyEvent e) { }
@Override
public void keyPressed(KeyEvent e) { }
}
}
The code in the Listener is the same.
I hope you see that I've changed the TextField only to be created local in the Actionlistener - that one does not need to be a member. Also the String
nameTField
- that one is not allowed to change at all, so it's supposed to be final to prevent that.
Finally the general Lik to using Listeners -
http://docs.oracle.com/javase/tutorial/uiswing/events/index.html[
^]
I recommend to bookmark that, the Tutorials have pretty much info and are written great.