Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

java - Initialize variable with constructor

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...