Can't set label text from a Jbutton
Hello all,
I have the following code. I would like to be able to set the text in the label (called label) to something else when a user click the close button.
I am guessing that my action class does not know what label is but I'm not sure how to reference since its in another class.
Thanks!!
VBGuy
Code Java:
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main {
public static void main(String[] args) {
ShowFrame();
}
public static void ShowFrame() {
JFrame MyWindow = new JFrame("Hello World");
MyWindow.setSize(800, 600);
MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyWindow.setLayout(null);
//Label
JLabel label = new JLabel("Hello World!");
label.setBounds(200, 10, 100, 25);
//Text Field
JTextField bbox = new JTextField(16);
bbox.setBounds(10, 10, 100, 25);
//Add Buttons
JButton CloseButton = new JButton("Close");
CloseButton.setBounds(10, 100, 90, 40);
JButton OkButton = new JButton("Ok");
OkButton.setBounds(100, 100, 90, 40);
//Add to Frame
MyWindow.add(CloseButton);
MyWindow.add(bbox);
MyWindow.add(OkButton);
MyWindow.add(label);
//Set Visible
MyWindow.setVisible(true);
bbox.setVisible(true);
CloseButton.setVisible(true);
OkButton.setVisible(true);
//Set Action Listener
CloseButton.addActionListener(new Action());
}
}
Code Java:
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Action implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setText("You Clicked Close!");
}
}
Re: Can't set label text from a Jbutton
Yes, your intuition is correct. Action has no idea what label is. The easiest way to solve this problem is to pass label to it when it's being constructed:
Re: Can't set label text from a Jbutton
Quote:
Originally Posted by
helloworld922
Yes, your intuition is correct. Action has no idea what label is. The easiest way to solve this problem is to pass label to it when it's being constructed:
Thanks Helloworld but I get an error when I try to run the code after your changes.
I get 'cannot find symbol'
Any thoughts?
Thanks!
Re: Can't set label text from a Jbutton
Please copy and paste full text of the error message here.
What symbol?
1 Attachment(s)
Re: Can't set label text from a Jbutton
Thank you both for all the help!!
I have attached a screenshot of the error that now comes up.
I am assuming this is the problem:
If I remove it then I can compile and run (and crash) but it does compile.
Is there no other way to reference the label object (or any object for that matter)?
Thanks!!
Re: Can't set label text from a Jbutton
If you are getting compile errors, you must copy and paste them here. We're not able to see them otherwise.
Re: Can't set label text from a Jbutton
Quote:
Originally Posted by
VBGuy
Is there no other way to reference the label object (or any object for that matter)?
As far as referencing other object, you can pass reference to the class as shown above, or alternatively create an inner class (or inner anonymous class) which has access to the holding class variables (this can be useful in quite a few ways, but at times can create a mess as well)
Code java:
public class MyClass{
JLabel label = new JLabel();
JButton button = new JButton("press here");
////
public MyClass(){
//set up things
button.add(new ButtonListener());//inner class example
button.add( new ActionListener(){///inner anonymous class example
public void actionPerformed(ActionEvent e){
System.out.println("Button was pressed");
MyClass.this.label.setText("Changed");
}
});
///note now 2 action listeners are registered on button
}
/**
* Inner class
*/
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.println("Button was pressed");
MyClass.this.label.setText("Changed");
}
}
}
The error you see above is because you do not pass the label into the constructor of Action
Re: Can't set label text from a Jbutton
Quote:
Originally Posted by
VBGuy
Thanks Helloworld but I get an error when I try to run the code after your changes.
I get 'cannot find symbol'
Any thoughts?
Thanks!
Since the action listener class now has a constructor provided, you must use this constructor.
Code Java:
//Set Action Listener
// this is using the wrong constructor!
// CloseButton.addActionListener(new Action());
// correct constructor
CloseButton.addActionListener(new Action(label));
Re: Can't set label text from a Jbutton
What package does your Action class come from? I can only see an interface in javax.swing.
If you're making your own class, it'd be better not to use one of Sun's