Results 1 to 4 of 4
  1. #1
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default 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);
            }
    Java Code:
    class ButtonListener extends RunGUI implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Name Bob to Jim")) {
            name = "Jim";
        }
    }
    I've attempted to "update" inside the ButtonListener class>actionPerformed()>if statement section. With things like repaint, revalidate the frame and panel1.

    Help please!
    Thanks in advance

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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");

  3. #3
    keo
    keo is offline Member
    Join Date
    May 2010
    Posts
    34
    Rep Power
    0

    Default

    Thank you! I was able to change a label as well as an image.. Hopefully it'll suit the rest of my java project =]!

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You're welcome, glad to have helped. Please mark your thread solved with the thread tools at the top.

Similar Threads

  1. Dynamic variable name based on other variable
    By nadissen in forum Eclipse
    Replies: 4
    Last Post: 05-06-2011, 07:22 PM
  2. How to update with dynamic variable?
    By mr_anderson in forum JDBC
    Replies: 9
    Last Post: 08-09-2010, 09:40 AM
  3. Replies: 11
    Last Post: 07-07-2010, 07:02 AM
  4. Replies: 0
    Last Post: 02-06-2008, 03:07 PM
  5. Using sql:update tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-14-2008, 12:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •