Witam,
tworzę w konstruktorze pole tekstowe. W konstruktorze mogę się odwołać do jego zawartości. Jeśli jednak chcę się odwołać do zawartości tego pola z pozycji menu, nie jest to już możliwe. W jaki sposób mogę się odwołać do wartości pola tekstowego?
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
//import java.awt.TextField;
public class NameOfMyClass extends JFrame implements WindowListener, ActionListener, MouseListener {
public NameOfMyClass() {
super("My application");
setLayout(null);
//...
JTextField textFieldNr1 = new JTextField();
getContentPane().add(txtTextFieldNr1);
textFieldNr1.setBounds(horizontalValue,verticalValue,heightValue,widthValue);
textFieldNr1.setText(stringDefaultDirectoryAndFile);
stringFieldNr1 = textFieldNr1.getText(); //tutaj mogę pobrać wartość pola tekstowego
setVisible(true);
}
public void actionPerformed(ActionEvent evnt) {
String cmdCommand = evnt.getActionCommand();
//...
if (menuPosition.equals(cmdCommand)) {
try {
//...
stringFieldNr1 = textFieldNr1.getText(); //a tutaj już nie mogę jej pobrać, bo kod nie wie, gdzie znajduje się to pole tekstowe (!!!)
BufferedReader buffReader = new BufferedReader(new InputStreamReader(new FileInputStream(stringFile), stringEncoding));
//...
}
}
}
}
Pozdrawiam!