0

I'm facing problems when changing a variable that is initiallized in the constructor of another object

JFrame:

public class Window extends JFrame {

    private String ip = "default";
    private String port = "default"; 
    private String nameClient = "default";

    // getters and setters, including setPort ...

    public void setPort(String port) {
        this.port = port;
    }

    public Window() {
        JLabel numPort = new JLabel(port);
        numPort.setBounds(149, 77, 46, 14);
        add(numPort);
    }
}

In the test class:

public class TestWindow {
    public static void main(String[] args){
        String validate = "1234";

        Window tester = new Window();
        tester.setPort(validate);
    }
}

Sorry for the noob question, but I cant understand why the Jlabel doesnt change here. If needed I can post the whole code (trying to make a chat-like swing app)

Thanks

Victor Ide
  • 41
  • 6
  • 1
    post the method Window.setPort()... we need to see what are u doing in there... – ΦXocę 웃 Пepeúpa ツ Apr 11 '16 at 17:44
  • 3
    I assume it's because `setPort` only updates the value of `port` and doesn't update `numPort`. You'll need to post the code you are asking about, though, if you want a non-guess answer. – azurefrog Apr 11 '16 at 17:45
  • My other guess is that you think that updating the value of `port` will somehow update `numPort`, which it won't (see http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – azurefrog Apr 11 '16 at 17:49
  • @azurefrog yes I was thinking it, cause my thoughs were: if I change the JLabel instance paramenter, it would change the value when setting it in the new instance. thanks guys! – Victor Ide Apr 11 '16 at 18:16

3 Answers3

0

In constructor you are setting the value to Label. And this value is based on the current port value. In your test class you create new Window instance with the default port value and only then you change the port value (but it obviously does not affect already created Label).

You should either add the port parameter to the constructor like this:

public Window(String port) {
        JLabel numPort = new JLabel(port);
        numPort.setBounds(149, 77, 46, 14);
        add(numPort);
}

or update your setPort() method:

public void setPort(String port) {
    this.port = port;
    numPort.setText(port);
}
Akceptor
  • 1,914
  • 3
  • 26
  • 31
0

You are updating the value inside Window class but not inside JLabel. Java strings are immutable so your reassignment actually causes your variable on Windows class to point to a new instance of String rather than changing the value of the String.

Try using something like this on your Window class code:

public class Window extends JFrame {

    private String ip = "default";
    private String port = "default"; 
    private String nameClient = "default";
    private JLabel numPort; //converted to a instance variable

    // getters and setters, including setPort ...

    public void setPort(String port) {
        this.port = port;
        numPort.setText(port); 
    }

    public Window() {
        numPort = new JLabel(port);
        numPort.setBounds(149, 77, 46, 14);
        add(numPort);
    }
}
marcellorvalle
  • 1,631
  • 3
  • 17
  • 30
0

I must admit, it is a not recommended to do real work in constructor:

Now, the reason why your window label doesn't change is because when you do the following in main.

 Window tester = new Window();

Your constructor is invoked and your JLabel is already initialized with "default" port

I suggest doing the following update to constructor i.e. pass a constructed JLabel this will give you more control over the input.

public Window(JLabel label) {
    ......
}

Hope this helps.

MSameer
  • 413
  • 2
  • 9