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: Button question

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

    Default Button question

    I have a button, start, that starts a countdown timer. While the timer is counting down the button stays pressed. I need to be able to press another button, stop, while the timer is counting down.


  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 question

    You need to post an SSCCE that demonstrates the problem, and clearly define what the problem actually is because - while I might be the only one - I do not even know what the question is.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Button question

    import javax.swing.*;

    import java.awt.*;
    import java.awt.event.*;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;

    public class MicrowaveWindow extends JFrame implements KeyListener{

    // Declaring variables
    private JPanel east = null;
    private JPanel south = null;
    private static JPanel north = null;
    private JLabel screen = null;
    public static JPanel center = new JPanel();
    public static String secString = null;
    public static String minString = null;
    public static int stopping;

    // Constructing the window
    public MicrowaveWindow() {
    setTitle("Microwave Interface");
    setSize(600,400);
    setVisible(true);

    east = new JPanel();
    add(east, BorderLayout.EAST);
    screen = new JLabel("");
    east.add(screen);
    east.setLayout(new GridLayout(4,3));

    south = new JPanel();
    add(south, BorderLayout.SOUTH);
    south.add(screen);

    north = new JPanel();
    add(north, BorderLayout.NORTH);
    north.add(screen);

    center.setLayout(new GridLayout(4, 3));
    add(center);
    center.setBackground(Color.LIGHT_GRAY);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.addKeyListener(this);


    // Constructing buttons
    JButton[] nButtons = new JButton[10];
    for(int i = 0; i < nButtons.length; i++) {
    nButtons[i] = new JButton("" + i);
    nButtons[i].addActionListener(new NumAction());
    nButtons[i].addKeyListener(this);
    }
    for(int i = 0; i < 9; i++) {
    east.add(nButtons[i + 1]);

    }

    JButton stop = new JButton("Stop");
    east.add(stop);

    east.add(nButtons[0]);

    final JButton start = new JButton("Start");
    east.add(start);

    JButton open = new JButton("Open");
    south.add(open);


    // Key press events

    start.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae) {
    System.out.println("Start");
    center.setBackground(Color.YELLOW);
    start.setEnabled(true);
    Timer(minutes, seconds);

    }
    });


    stop.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae) {
    System.out.println("Stop");
    center.setBackground(Color.LIGHT_GRAY);
    timeLabel.setText(sdf.format(0000));
    q="0";w="0";e="0";r="0";
    }
    });

    open.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae) {
    System.out.println("Open");
    center.setBackground(Color.LIGHT_GRAY);
    }
    });
    }

    //unnecessary code
    public void keyPressed(KeyEvent ke) {}
    public void keyReleased(KeyEvent ke) {}

    public void keyTyped(KeyEvent ke) {
    addNum("" + ke.getKeyChar());
    }


    // NumAction class

    private class NumAction implements ActionListener {
    public void actionPerformed(ActionEvent event) {
    String text = event.getActionCommand();
    addNum(text);
    }
    }

    // Reading in the numbers
    public static String q="0";
    public static String w="0";
    public static String e="0";
    public static String r="0";
    public static int seconds=0;
    public static int minutes=0;
    public static JLabel timeLabel = new JLabel();
    static SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");

    public void addNum(String text) {


    String clockset = screen.getText();
    clockset += text;
    r=e;
    e=w;
    w=q;
    q=clockset;

    timeLabel.setText(r+e+":"+w+q);



    int rInt = Integer.parseInt(r);
    int eInt = Integer.parseInt(e);
    int wInt = Integer.parseInt(w);
    int qInt = Integer.parseInt(q);
    seconds = wInt*10 + qInt;
    minutes = rInt*10 + eInt;

    }

    // Running the timer method

    // Main methods
    public static void main(String[] args) {
    JFrame f = new MicrowaveWindow();
    f.setVisible(true);
    north.add(timeLabel);
    timeLabel.setText(sdf.format(0000));

    }


    public static void Timer(int counter2, int counter){
    for(int i = 0; counter>0; i++){

    counter--;

    secString = Integer.toString(counter);
    minString = Integer.toString(counter2);

    if(counter<=9){
    secString = "0" + secString;
    }
    if(counter2<=9){
    minString = "0" + minString;
    }
    System.out.print(minString);
    System.out.println(secString);


    // countdown display


    timeLabel.setText(minString + ":" + secString);


    if(counter<=0){
    counter = 60;
    counter2--;
    if(minString.equals("00") && secString.equals("00")){
    break;
    }
    }

    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {

    e.printStackTrace();
    }
    }
    timeLabel.setText(sdf.format(0000));
    q="0";w="0";e="0";r="0";
    center.setBackground(Color.LIGHT_GRAY);
    }











    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

    }
    }

  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 question

    For future reference, please wrap your code in the code tags (see the forum rules for instructions)

    Swing is single threaded...all updates, painting, etc...occurs on this one thread called the EDT. Your code attempts to perform computation and timer tasks on the EDT (from within the actionPerformed method), and until this is complete no Swing component will be updated. If you wish to perform long running tasks, use another Thread (requires management to dispatch updates to the EDT) or a Swing Timer.

    See Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

Similar Threads

  1. Cancel button ..??
    By smasm in forum Loops & Control Statements
    Replies: 3
    Last Post: November 5th, 2011, 06:12 AM
  2. Quick beginner radio button question
    By smarmy_cheauffeur in forum AWT / Java Swing
    Replies: 2
    Last Post: October 17th, 2011, 08:20 AM
  3. disable a button ?
    By dime111 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 28th, 2011, 02:58 PM
  4. Thread Sleep, Timer, Button Question
    By tabutcher in forum Java Theory & Questions
    Replies: 1
    Last Post: May 1st, 2010, 02:54 AM
  5. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 08:21 PM