I have two class, first is my main class, and second class in my edit frame class.
public class RecordTableGUI extends JFrame implements ActionListener {
String newName;
public RecordTableGUI(String newReceivedName) {
newName = newReceivedName;
System.out.println("new name in new constructor : " + newName); //prints new name correctly
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == editButton) {
Object oldName = table.getValueAt(table.getSelectedRow(), 1);
System.out.println("old name: " + oldName); // prints old name correctly
this.setVisible(false);
new UpdateGUI(String.valueOf(oldName));
System.out.println("new name in problem area: " + newName); // why null?
}
}
}
My second class(UpdateGUI) gives oldName in it's constructor and after edit it, When i click to okButton
, it send newName to my first Class.
My second Class:
public class UpdateGUI extends JFrame implements ActionListener {
String oldName, newName;
public UpdateGUI(String oldname) {
oldName = oldname;
....
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
newName = tf.getText(); //tf is JTextfield
new RecordTableGUI(newName);
this.setVisible(false);
}
}
My problem is that Why newName is null?
Update:
public class RecordTableGUI extends JFrame implements ActionListener {
public RecordTableGUI(String newReceivedName) {
setNewName(newReceivedName);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == editButton) {
Object oldName = table.getValueAt(table.getSelectedRow(), 1);
System.out.println("old name: " + oldName);
RecordTableGUI recordObject = new RecordTableGUI();
UpdateGUIDialog updDialog = new UpdateGUIDialog(String.valueOf(oldName), recordObject);
}
}
UpdateGUIDialog Class:
public class UpdateGUIDialog extends JDialog implements ActionListener {
RecordTableGUI recordtablegui;
public UpdateGUIDialog(String old, RecordTableGUI recordGUI) {
oldName = old;
recordtablegui = recordGUI;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
newName = tf.getText();
recordtablegui.setNewName(newName);
this.dispose();
}
}
}
Output:
old name:james //prints correctly
new name: null //prints null
new name in set method: rrr //prints correctly
I need to print rrr
instead of null.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…