How to check a property of a textfield located in a constructor to an Action Listener
I have a Constructor:
Code:
Main() {
JLabel label = new JLabel();
JTextField textField = new JTextField("HahAHah",0);
JButton mainButton = new JButton("Do stuff");
FlowLayout layout = new FlowLayout();
mainButton.addActionListener(this);
int X;
int Y;
X = 300; Y = 100;
setSize(new Dimension(X,Y));
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(100,100);
setLayout(layout);
add(mainButton);
add(textField);
}
And a Action Listener:
Code:
@Override
public void actionPerformed(ActionEvent e) {
int randNum = new Random().nextInt(10) + 1;
printwithnewline(randNum);
}
How can I check text from the constructor in mu Action Listener? When I try to textField.getText() in the action listener, it says textField is not defined.
Re: How to check a property of a textfield located in a constructor to an Action List
So declare it as an instance field :(think):
db
Re: How to check a property of a textfield located in a constructor to an Action List
Nevermind I figured it out...
Re: How to check a property of a textfield located in a constructor to an Action List
Quote:
Originally Posted by
DarrylBurke
So declare it as an instance field :(think):
db
That's exactly what I've done :D