Reputation: 121
I'm currently having problems with accessing and changing the text of jTextField from another class.
I'm building an application which has two jTextFields elements in first UI. This two elements serve as boxes for the selected date in the second UI.
When a user clicks on the jTextField (first UI) it opens the second UI with a calendar, from which a user selects a date. When user selects a date and confirms it, the selected date appears in the jTextField as text in the first UI.
I'm having problems in the last bit. I want to fire the update method for the jTextField (in the first UI which is in the different class) from the class in which I have the second UI.
Upvotes: 0
Views: 144
Reputation: 9938
The MVC pattern is perfect for that. Otherwise, you can add in the constructor of your second class a parameter with the field object. Updating should work. Example (it's been a long time that I have coded in Java, so I may put wrong things) :
public MyFrame1() {
//...
JTextField dateField = new JTextField();
//...
dateField.addClickListener(new EventListener(){
new MyFrame2(dateField);
});
}
public MyFrame2(dateField) {
//...
// When updating :
dateField.setText(text);
}
Upvotes: 0
Reputation: 208994
If it's not too much code if your second frame, you could just make it a modal JDialog
instead of that second GUI and have it as an inner class of the main GUI class
public class MainGUI extends JFrame {
private MyDialog dialog;
public MainGIU(){
dialog = new MyDialog(this, true);
// some action sets the dialog visible
}
private class MyDialog extends JDialog {
public MyDialog(Frame frame, boolean modal){
super(frame, modal);
}
}
}
With the above setup, you can access any field from the MainGUI
inside the MyDialog
. But as Vin243 noted, this is the perfect example of when to use the MVC pattern
Upvotes: 1