Why does `jLabel1 = jLabel2` not change the displayed value?

advertisements

I have two JLabels created and added one of them to my JFrame but when I assign the other one to the added one the JFrame doesn't update

add(jLabel1);
jLabel1 = jLabel2;

how can I fix this?


In this line:

add(jLabel1);

You're adding to the GUI the object that jLabel1 is referring to. You are not adding the jLabel1 variable itself to the GUI.

In this line:

jLabel1 = jLabel2;

You're changing the object that jLabel1 is referring to. This has absolutely no effect on the original object that was added to the GUI.

The solution is as per camickr's answer (1+)