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

Thread: Need help with buttons

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

    Default Need help with 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! Have a nice day!


  2. #2
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Need help with buttons

    how come instead of making the class action you dont just do button.addActionListener(this); etc then use the getsource() method in the actionPerformed method to see which button got clicked?

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

    Default Re: Need help with buttons

    How do i do that? :O

  4. #4
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Need help with buttons

    in your actionPerformed method make an object

    Object source = e.getSource();

    then do some if statements using that object to determine which button is clicked ad inside the if statements put the code that should run when each button is clicked

    since you have all your frame stuff in the main method then just keep everything how you have it and just make the object in the actionperformed method etc

Similar Threads

  1. Swing buttons
    By zerocos in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 5th, 2013, 08:36 AM
  2. Help with Java buttons
    By Nameless _1 in forum AWT / Java Swing
    Replies: 2
    Last Post: June 18th, 2011, 03:16 PM
  3. [SOLVED] Buttons not working..
    By khms in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 2nd, 2010, 06:01 PM
  4. JRadio buttons
    By chronoz13 in forum AWT / Java Swing
    Replies: 0
    Last Post: November 29th, 2009, 01:08 AM
  5. Need more buttons!
    By GotWankel? in forum AWT / Java Swing
    Replies: 0
    Last Post: October 5th, 2009, 01:08 AM

Tags for this Thread