Initialize variable with constructor

java, swing

Solution

Java objects are somewhatlike real objects. And `new` does what its name suggests: it creates a new object. Let's take a simple example:

Box box1 = new Box();
Box box2 = new Box();
box1.fillWithCandies(candies);

`box1` is a box filled with candies. `box2` is a different box, which doesn't contain anything, because only `box1` has been filled with candies.

In your code, updateGUI's `actionPerformed()` method creates a new `RecordTableGUI` object, with the new name. That won't change anything to the first one.

If you want updateGUI to modify the existing RecordTableGUI object, it needs to have a reference to this object:

public class updateGUI extends JFrame implements ActionListener {

    private RecordTableGUI recordTableGUIToUpdateWhenOKIsClicked;

    public updateGUI(RecordTableGUI recordTableGUIToUpdateWhenOKIsClicked, ...) {
        this.recordTableGUIToUpdateWhenOKIsClicked = 
            recordTableGUIToUpdateWhenOKIsClicked;
        ...
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == okButton) {
            newName = tf.getText();
            this.recordTableGUIToUpdateWhenOKIsClicked.setNewName(newName);
        }
    }
}

You should practice with simpler examples before using Swing. You should also respect the Java naming conventions. And the `updateGui` class should be a JDialog, not a JFrame.

Problem

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.

Original source

Related problems