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: JButton listener trouble...

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JButton listener trouble...

    Please help... my program is meant to count down to zero then exit, but I can't get the button to refresh when the iteration variable has decreased.

    package tester;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
    public class ButtonAction {
     
    private static void createAndShowGUI() {
     
    JFrame frame1 = new JFrame("CountDown");
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    final int i = 7;
    final JButton button = new JButton(" " + i);
    //Add action listener to button
    button.addActionListener(new ActionListener() {
     
    public void actionPerformed(ActionEvent e)
    {
    i--;
    JButton button = new JButton(" " + i);
    System.out.println("c = " + i);
    if (i==0) System.exit(0);
    }
    }); 
     
    frame1.getContentPane().add(button);
    frame1.pack();
    frame1.setVisible(true);
    }
     
     
    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    }
    });
    }
    }
    Last edited by copeg; May 13th, 2011 at 09:23 AM.


  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: JButton listener trouble...

    Once is enough...I have deleted your other post. Please do not post the same thing more than once. Further, please flank your code with the code tags. To address your question at hand, there isn't a need to continually add new buttons to the GUI, just call setText on the original JButton to set the text.
    Last edited by copeg; May 13th, 2011 at 09:25 AM.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JButton listener trouble...

    Sorry for posting twice and not using the code tags. I used the settext to set the button, but I can't pass the variable i into or out of the ActionPerformed method to get it to count down by one every time it is clicked. How do I make that variable global, or pass it through to be used by the method? Thanks.

    package tester;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
    public class ButtonAction {
     
        private static void createAndShowGUI()  {
     
            JFrame frame1 = new JFrame("CountDown");
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final int i = 7;
            final JButton button = new JButton(" " + i);
            //Add action listener to button                
            button.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e)
                {
                    //i--;
                	button.setText("Test" + i);
                	System.out.println("i = " + i);
                	if (i==0) System.exit(0);
                }			
            });     
            frame1.getContentPane().add(button);
            frame1.pack();
            frame1.setVisible(true);
        } 
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

  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: JButton listener trouble...

    Many different ways - create a class which holds the value as a variable, create a static variable...see Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JButton listener trouble...

    How can I do it specifically with my code. I read your link and I still don't see how to pass the variable into the ActionPerformed method each time so that it remembers it's been pressed a certain number of times. I want it to count down each time it's clicked. Thanks.

  6. #6
    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: JButton listener trouble...

    You don't pass it to the method (in fact you can't, as the method is defined by the ActionListener interface). You create the variable somewhere outside that method - be it a static variable or a member of a class you created - and access that variable inside the method.

Similar Threads

  1. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  2. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  3. Key listener help
    By airsim15 in forum AWT / Java Swing
    Replies: 2
    Last Post: December 13th, 2009, 03:15 PM
  4. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM