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 5 of 5

Thread: button update when clicked

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default button update when clicked

    Hello java programmers!
    I am going to ask a simple question (doesn't seem that simple to me though). I want to make a button change its text whenever clicked and i want that to be immediately shown in my GUI. I have my button and the listener class up and running. It's just this bit that i don't know how to do.
    Thanks in advance.

    P.S. if you feel like posting some code that would be greatly appreciated!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: button update when clicked

    Please post some code which demonstrates what you have done so far. It sounds like you are close to getting what you want, but without code its tough to judge. For what its worth, see the API for JButton for methods on how to set its text.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: button update when clicked

    This is the original button
    JButton b00 = new JButton("  ");
            ActionListener ac = new Counter(b00);
            b00.addActionListener(new Counter(b00));
            panelGrid.add(b00);

    And this is the listener class.

    public class Counter implements ActionListener
        {
            private int count;
            private JButton button;
     
            public Counter(JButton b)
            {
                button = b;
            }
            public void actionPerformed(ActionEvent e)
            {
                count++;
                if (count > 5) {
                    count = 0;
                }
                button = new JButton(""+count);
                System.out.println("CLICKED "+count+" TIMES");
            }
        }

    I am aware that it actually creates a new button with the count value as text but i don't see how can i replace the b00 button with 'button' button.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: button update when clicked

    but i don't see how can i replace the b00 button with 'button' button.
    Has to do with references and what the reference points to at the time you create your GUI. Same thing as the following
    String test1 = "test1";
    String test2 = test1;
    test1 = "test3";
    System.out.println(test1);
    System.out.println(test2);

    Prints
    test3
    test1
    because you've re-assigned the reference, and previous references still point to the original. As I mentioned in my previous post, get familiar with the API, and the fix to your problem is to use the setText function of JButton (see JButton (Java Platform SE 6) )

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    8
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: button update when clicked

    thank you for your time. You helped me a lot

Similar Threads

  1. Update table in database
    By CTheSky in forum JDBC & Databases
    Replies: 4
    Last Post: February 24th, 2011, 02:02 AM
  2. Cannot update data in .txt/.csv file
    By Azriq007 in forum JDBC & Databases
    Replies: 2
    Last Post: October 16th, 2010, 09:16 PM
  3. [SOLVED] Executable .jar file isn’t launched after being double-clicked
    By voltaire in forum Java Theory & Questions
    Replies: 6
    Last Post: May 18th, 2010, 03:37 PM
  4. Cannot update Jlabel in JApplet
    By rin in forum Java Applets
    Replies: 2
    Last Post: April 17th, 2010, 08:21 AM