0

I've just been trying to learn and messing around with code. And I've come across something I did not expect to happen. I have a JLabel in a MainApp class, I create an ActionListener (HelloListener) which is passed the JLabel. When the button is pressed, the actionPerformed method should update the JLabel to "Hello again!". And it does, but why it does it confuses me.

However, I thought I would have to return the new JLabel? When I pass the HelloListener the JLabel, isn't that JLabel the property of the HelloListener class after it is passed? So when it updates it will only update the one in HelloListener, and I would then have to return it?

Why when I update the JLabel in the HelloListener does it also update in the MainApp class?

Here's the code:

public class MainApp extends JFrame {

    public static void main(String[] args) {

        new MainApp();

    }

    public MainApp() {

        setLayout(new GridLayout (2,1));

        setSize(200,200);

        JLabel jl = new JLabel("Hello!");
        add(jl);

        JButton jb = new JButton("Click me!");
        jb.addActionListener(new HelloListener(jl));
        add(jb);

        setVisible(true);

    }

}

and

public class HelloListener implements ActionListener {

    JLabel jl;

    public HelloListener(JLabel jl) {
        this.jl = jl;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        jl.setText("Hello again!");
    }

}
Tim
  • 1,056
  • 4
  • 17
  • 34
  • Have a look at my [answer to a similar question](http://stackoverflow.com/a/10077580/544963) for instructions on how to do this. If you need further assistance, just ask. :) – fireshadow52 Apr 10 '12 at 12:34

3 Answers3

3

When I pass the HelloListener the JLabel, isn't that JLabel the property of the HelloListener class after it is passed? So when it updates it will only update the one in HelloListener, and I would then have to return it?

This is where the breakdown in your understanding is. When you "pass the JLabel", you are passing a reference to the JLabel (you are actually passing the reference by value, which you should look up as soon as you understand your current issue, because it is very important with Java). The underlying object instance still exists everywhere it did before. So the JLabel jl in the HelloListener instance is just a reference to the same actual JLabel instance that is presented in the gui.

This is fundamental to how Java (and many programming languages) works. If you do

Dog d = new Dog();

and then pass d to a method

walk(d);

and walk looks like

public void walk(Dog dog) {
   dog.setLastWalkTime(now());
   dog.attachLeash(); 
}

dog in the method, and d in the calling scope, both point to the same Dog instance. So as sson as you set the lastWalkTime, the underlying instance is modified. If right after walk you looked at d, you would see the lastWalkTime value you set in the method.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • 1
    thanks. I hope you understand. Look up 'Java pass by value' it is probably the most misunderstood and confusing part of Java for newcomers. – hvgotcodes Apr 10 '12 at 12:45
1

When a function takes an object as an argument, it is being passed a reference to the object. In your code, both MainApp and HelloListener have a reference to the same JLabel instance.

When actionPerformed() is called, the listener modifies a property of the instance. Since the JFrame has access to that same instance, it detects the change and updates the GUI.

unholysampler
  • 17,141
  • 7
  • 47
  • 64
0

Why would you expect to return the new JLabel? (And then what? Remove the old one, add new one, have your window to do the layout again?) The JLabel instance you're passing into the listener is used as a reference to your JLabel, and that's the beauty of it - you're modifying your existing instance.

maksimov
  • 5,792
  • 1
  • 30
  • 38