Trying to change jlabel text from another class

I posted this question this morning and I was wondering if someone could clarify this for me.

I have a JFrame form that I made using netbeans.

I have a button and a label... I set the event handler so that when the button is pushed it calls a constructor from another class

ie:

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {

tryme but = new tryme();

but.buttonPressed();

}

The variable names are just for testing purposes.

What I want to do is modify the label from the buttonPressed() method. How would I go about doing this?

I was told that I need to use "PropertyChange"... The problem is that I need an example of how to use it...

[723 byte] By [Techameda] at [2007-11-27 6:23:44]
# 1

> What I want to do is modify the label from the buttonPressed() method.

I guess you mean to change the label of the button whose press led to this method being called.

(1) Get a reference to the button in question. This may be as simple asJButton theButton = (JButton)evt.getSource();

provided jButoon1MouseClicked() is not called as an action handler for any other components.

(2) Pass this reference to the tryme butbut.buttonPressed(theButton);

(3) Have the buttonPressed() method modify the button's label.public void buttonPressed(JButton button) {

// other stuff

button.setLabel("Changed!");

}

(It might be a good idea to change your tryme class so that it starts with a capital letter: Tryme, and to change jButton1EtcEtc to be shorter and more descriptive. Import java.awt.event.MouseEvent so that the signature of the method is readable. It is improtant to have readable code.)

[Edit]D@mn, I've just read more closely. You have a button *and* and label, not a button *with* a label.

The solution is similar, except that you will have to have a reference to this label as an instance member of the class containing the jButton1EtcEtc() method. Pass that reference to buttonPressed() and have buttonPressed invoke JLabel's setText() method.

pbrockway2a at 2007-7-12 17:42:00 > top of Java-index,Java Essentials,New To Java...
# 2

Oh of course... If I am putting together a real application I will use proper syntax and more descriptive names..

I have 2 files though... I wanted to keep everything separate.The JFrame is just the gui part of it... I want all my calculations done from other classes.

Like.... lets say that one class was called cars (first thing that came to my head) and the second class was called gui ... I want to change the button label in the gui class.

Techameda at 2007-7-12 17:42:00 > top of Java-index,Java Essentials,New To Java...
# 3

Another thing would be to have Cars implement ActionListener, and add it as an ActionListener to the button.

class GUI extends JFrame {

public GUI() {

JButton btn = new JButton("Button");

Cars cars = new Cars();

btn.addActionListener(cars);

}

}

class Cars implements ActionListener {

public void actionPerformed(ActionEvent e) {

if (e.getSource() instanceof JButton) {

((JButton)e.getSource()).setText("Other Text");

}

}

}

BinaryDigita at 2007-7-12 17:42:00 > top of Java-index,Java Essentials,New To Java...