私は 2 つのクラスを持っています。1 つ目はメイン クラスで、2 つ目は編集フレーム クラスです。
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?
}
}
}
私の2番目のクラス(UpdateGUI)はコンストラクターでoldNameを与え、それを編集した後、クリックするとokButton
、newNameが最初のクラスに送信されます。
私の2番目のクラス:
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);
}
}
私の問題は、なぜ newName が null なのですか?
アップデート:
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 クラス:
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();
}
}
}
出力:
old name:james //prints correctly
new name: null //prints null
new name in set method: rrr //prints correctly
rrr
nullの代わりに印刷する必要があります。