Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Need help with updating label

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with updating label

    I am trying to update the text on the label through the use of the actionListener, however no matter what i do i can't get it to work.
    Here is the relevent code
        public JPanel panel(){
            JPanel panel = new JPanel();
            panel.add(label());
            panel.setPreferredSize(new Dimension(100,100));
            panel.add(button());
            return panel;
        }
        public JLabel label(){
     
            JLabel label = new JLabel();
            label.setText("Some Updating text: "+i);
     
            return label;
        }
        public JButton button(){
            JButton button = new JButton();
     
            button.setPreferredSize(new Dimension(20,20));
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    i = i+1;
                    label().repaint();
                }
     
            });
            return button;
        }

    Any help on this issue will be appreciated.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: Need help with updating label

    You must set the value of the JLabel if you wish to update it by calling setText

  3. The Following User Says Thank You to copeg For This Useful Post:

    Neacel (January 22nd, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with updating label

    Could you give me some example code on how to do this?

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: Need help with updating label

    Quote Originally Posted by Neacel View Post
    Could you give me some example code on how to do this?

    You already have example code in your label method. Just call the same setText method in your actionPerformed with the new value of i (just changing the value of i does not inherently change the value shown in the JLabel)

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with updating label

      public JButton button(){
            JButton button = new JButton();
     
            button.setPreferredSize(new Dimension(20,20));
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    i = i+1;
                    label().setText("Some Updating text: "+i);
                    label().repaint();
                }
     
            });
            return button;
        }
    This is what i have written, but it still doesn't update

  7. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with updating label

    I got it working... i added

    public JLabel label1;

    then modified the JLabel Method and actionPerformed to include label1.

Similar Threads

  1. Label not showing up.
    By joshft91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 1st, 2011, 03:36 PM
  2. Interaction between two label in animation
    By odiejodie in forum Java Theory & Questions
    Replies: 1
    Last Post: February 16th, 2011, 08:51 AM
  3. how to make widget label cloud at JSP
    By 20GreatWall in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: October 11th, 2010, 12:16 PM
  4. Can't set label text from a Jbutton
    By VBGuy in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 8th, 2010, 11:55 AM
  5. centering a label inside a rectangle
    By Brain_Child in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2009, 09:08 AM