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

Thread: Add button listeners to 4 buttons?

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Add button listeners to 4 buttons?

    Hello! My name is Joel and is kinda new to java programming. I got a problem. I want to make 4 buttons on a Panel that has 4 different timers. When i press the first one i want it to take 5 minutes and then pop up a screen with a text like this "Your eggs are ready". And i want different timers on all of the buttons. I did succed with one button but cant figure out how i can add listeners to any more buttons. And how do i add delay in a while loop?

    package bluered.timer;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Scanner;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import java.io.*;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import sun.audio.*;
    import static javax.swing.JOptionPane.*;
    import javax.swing.JPanel;
     
     
    public class BlueRedTimer {
     
     
        public static void main(String args[]) {
     
     
     
           JFrame frame=new JFrame("Test");
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(300,80);
    		JPanel panel=new JPanel();
    		frame.add(panel);
    		JButton button=new JButton("Timer1");
    		JButton button2=new JButton("Timer2");
    		JButton button3=new JButton("Timer3");
    		JButton button4=new JButton("Timer4");
    		panel.add(button);
    		panel.add(button2);
    		panel.add(button3);
    		panel.add(button4);
    		button.addActionListener(new Action());
                    button2.addActionListener(new Action());
     
     
        }
     
         static class Action implements ActionListener   {
     
            public void actionPerformed(ActionEvent e){
                int Blue=300;   //I want the "blue" timer to be set at 5 minutes. So when i press button timer1 it will start a countdown of 5 minutes and then a screen pops up. 
               while (Blue>0){
               Blue--;
               System.out.println(Blue);
     
               }
               if (Blue==0){
                   JOptionPane.showMessageDialog(null, "Timer one is ready!");}   
            }
        }}

    Thanks for the replies!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,116
    My Mood
    Mellow
    Thanks
    186
    Thanked 658 Times in 645 Posts

    Default Re: Add button listeners to 4 buttons?

    Edit: Just noticed the topic is a duplicate of this one. Please don't post duplicate topics.

    I've showed you how to use the same ActionListener for multiple buttons in the code below, but there are problems with this code I think you should fix:

    1. A Swing application should not be run on the same thread that runs the main() method. You can find out more about proper Swing programming here.

    2. An entire Swing GUI should not be run from the main() method. You may have created this runnable example in a hurry and chose the design for simplicity, but the design shown in the code you posted should not be your typical Swing application design. The main() method should be one or two lines long with the bulk of the code in the primary Swing class, often creating a JFrame, starting with its constructor.

    3. Your next question might be about programming your "timers" to keep track of the actual time you had in mind. For that you'll want to learn about the javax.swing.Timer class.

    4. You might also wonder about having multiple timers running at the same time. I haven't worked out that answer completely. Multiple Timer instances might be sufficient or, if not, then a SwingWorker for each button (or button press) might be appropriate. Let me know if you'd like to work on that.

    Keep coding, and good luck!
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
     
     
    public class BlueRedTimer
    {
        public static void main(String args[])
        {
            JFrame frame=new JFrame("Test");
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300,80);
            JPanel panel=new JPanel();
            frame.add(panel);
            JButton button=new JButton("Timer1");
            JButton button2=new JButton("Timer2");
            JButton button3=new JButton("Timer3");
            JButton button4=new JButton("Timer4");
            panel.add(button);
            panel.add(button2);
            panel.add(button3);
            panel.add(button4);
            button.addActionListener(new Action());
            button2.addActionListener(new Action());
        }
     
    }
     
    final class Action implements ActionListener
    {
        int setTime;
     
        public void actionPerformed(ActionEvent e)
        {
            // if a single ActionListener is to handle multiple events, then
            // add source discrimination
            Object sourceActionCmd = e.getActionCommand();
     
            if ( sourceActionCmd.equals( "Timer1" ) )
            {
                System.out.println( "Button Timer1 was selected.");
     
                setTime = 300;   // I want the "blue" timer to be set at 5 minutes.
                                 // So when i press button timer1 it will start a
                                 // countdown of 5 minutes and then a screen pops up. 
                while (setTime>0)
                {
                    setTime--;
                    System.out.println( setTime );
     
                }
                // the if statement here originally is unnecessary
                JOptionPane.showMessageDialog(null, "Timer one is ready!");
     
            }
            else if ( sourceActionCmd.equals( "Timer2" ) )
            {
                // and so on . . .
                System.out.println( "Button Timer2 was selected.");
            }
            // and so on . . .
        }
    }
    [COLOR="Silver"]
    Last edited by GregBrannon; July 13th, 2013 at 05:54 PM. Reason: Double post removed.

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

    DaHappy (July 13th, 2013)

Similar Threads

  1. Adding add/remove button to add/remove tab
    By JMtrasfiero in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 27th, 2012, 11:24 AM
  2. Replies: 1
    Last Post: July 20th, 2011, 08:03 AM
  3. How to add buttons on this code
    By tarell85 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 18th, 2011, 07:08 AM
  4. Java App - Add, Delete, Reorder elements of the buttons
    By alibm in forum AWT / Java Swing
    Replies: 5
    Last Post: March 7th, 2011, 07:46 AM
  5. Action Listeners and Key Listeners Help
    By xctive in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 08:27 AM

Tags for this Thread