Results 1 to 4 of 4
Thread: Update a variable on GUI
- 05-03-2011, 06:13 AM #1
Member
- Join Date
- May 2010
- Posts
- 34
- Rep Power
- 0
Update a variable on GUI
I am trying to figure out how to update a GUI change (change a string label). I've tried things like revalidate(), repaint(), removeAll() from google suggestions but still can't apply it properly.
Below is a simplfied version of my program. Some of it may be wrong in this example but: Everything seems to work, when the button is pressed, I can confirm that the String has been changed as well as the button has really been pressed with my real code.
Java Code://import everything public class RunGUI extends JFrame{ public static JFrame frame = new JFrame("Window name"); public static JPanel myPanel = new JPanel(); public static void setUpGUI() { //setup frame: make visible, size etc. frame.add(myPanel); Panel1.setUpPanel1; } }Java Code://This class is a subclass used to edit myPanel from above class //import everything public class Panel1 extends RunGUI{ private static String name = "Bob"; public static void setUpPanel1() { JButton btn1 = new JButton("Name Bob to Jim"); btn1.addActionListener((ActionListener) new ButtonListener()); myPanel.add(btn1); JLabel nameLabel = new JLabel(name); myPanel.add(nameLabel); }I've attempted to "update" inside the ButtonListener class>actionPerformed()>if statement section. With things like repaint, revalidate the frame and panel1.Java Code:class ButtonListener extends RunGUI implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Name Bob to Jim")) { name = "Jim"; } }
Help please!
Thanks in advance
- 05-03-2011, 06:42 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
JLabel has a helpful method called setText, which changes the text to it's argument
Java Code:JLabel someLabel = new JLabel("Hello"); someLabel.setText("Goodbye");
- 05-03-2011, 06:51 AM #3
Member
- Join Date
- May 2010
- Posts
- 34
- Rep Power
- 0
Thank you! I was able to change a label as well as an image.. Hopefully it'll suit the rest of my java project =]!
- 05-03-2011, 06:52 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You're welcome, glad to have helped. Please mark your thread solved with the thread tools at the top.
Similar Threads
-
Dynamic variable name based on other variable
By nadissen in forum EclipseReplies: 4Last Post: 05-06-2011, 06:22 PM -
How to update with dynamic variable?
By mr_anderson in forum JDBCReplies: 9Last Post: 08-09-2010, 08:40 AM -
How do I substitute any variable for a hardcoded variable
By Weazel Boy in forum New To JavaReplies: 11Last Post: 07-07-2010, 06:02 AM -
How do I update a WINDOWS user env variable from my java code ?
By gavman99 in forum Advanced JavaReplies: 0Last Post: 02-06-2008, 02:07 PM -
Using sql:update tag
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 11:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks