Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default Waiting for a button to be pressed

    Hi guys, at some point in my code I want to wait for one of two buttons to be pressed on a jpanel. Is there a better way to do this other than having the buttons set a boolean to true and then using a while loop in my code (to continuously check the value of the boolean)? This seems like a really messy solution, if it even works.

    Whats the "real" way? Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Waiting in a loop is NOT the way to do it. The listeners for the buttons can check if the other button has been pressed and act when then by using the boolean as you suggested.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

  4. #4
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    I've looked up action listeners, but I dont think using them in the normal way will solve my problem. Normally you make an event (ie when a button is clicked) and when that event takes place, some code is executed.

    What I'm doing is more like...while a function is executing, in the middle of the function I want execution to stop until a button is pressed. How do I do that?

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    The real way is to use threads and setup a barrier. Have a timer thread sleep, wake up and if done, clear the barrier. Have the button also clear the barrier.

    Implement "OR" logic.

  6. #6
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by fishtoprecords View Post
    The real way is to use threads and setup a barrier. Have a timer thread sleep, wake up and if done, clear the barrier. Have the button also clear the barrier.

    Implement "OR" logic.
    I didn't really know what any of that meant, but I'll check it out. Thanks.

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Guarded Blocks
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class DelayTest implements ActionListener {
        JLabel topLabel;
        JLabel centerLabel;
        Toggle toggle;
    
        public DelayTest() {
            Thread thread = new Thread(toggle = new Toggle());
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        public void actionPerformed(ActionEvent e) {
            String ac = e.getActionCommand();
            toggle.setSignal(ac.equals("STOP"));
        }
    
        private class Toggle implements Runnable {
            boolean waitForSignal = true;
            int count = 0;
        
            public void run() {
                while(true) {
                    try {
                        Thread.sleep(1000);
                    } catch(InterruptedException e) {
                        break;
                    }
                    if(waitForSignal) {
                        stop();
                    }
                    count++;
                    centerLabel.setText(String.valueOf(count));
                }
            }
    
            public void setSignal(boolean signal) {
                waitForSignal = signal;
                if(!waitForSignal) {
                    start();
                }
                topLabel.setText("waitForSignal = " + waitForSignal);
            }
    
            private synchronized void start() {
                notifyAll();
            }
    
            private synchronized void stop() {
                while(waitForSignal) {
                    try {
                        wait();
                    } catch(InterruptedException e) {
                        break;
                    }
                }
            }
        }
    
        private JLabel getFirst() {
            topLabel = new JLabel();
            topLabel.setHorizontalAlignment(JLabel.CENTER);
            return topLabel;
        }
    
        private JPanel getCenter() {
            centerLabel = new JLabel();
            centerLabel.setHorizontalAlignment(JLabel.CENTER);
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(centerLabel, new GridBagConstraints());
            return panel;
        }
    
        private JPanel getLast() {
            String[] ids = { "start", "stop" };
            JPanel panel = new JPanel();
            for(int i = 0; i < ids.length; i++) {
                JButton button = new JButton(ids[i]);
                button.setActionCommand(ids[i].toUpperCase());
                button.addActionListener(this);
                panel.add(button);
            }
            return panel;
        }
    
        public static void main(String[] args) {
            DelayTest test = new DelayTest();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getFirst(), "First");
            f.add(test.getCenter());
            f.add(test.getLast(), "Last");
            f.setSize(300,200);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

Similar Threads

  1. waiting for a file
    By Fleur in forum New To Java
    Replies: 2
    Last Post: 06-23-2008, 08:18 PM
  2. can you help me with mouse pressed method please?
    By java_fun2007 in forum New To Java
    Replies: 4
    Last Post: 05-22-2008, 10:23 PM
  3. Replies: 2
    Last Post: 03-30-2008, 08:38 PM
  4. when muse pressed the background change
    By pcman in forum Java Applets
    Replies: 1
    Last Post: 03-17-2008, 11:51 PM
  5. key pressed event
    By kavithas in forum New To Java
    Replies: 7
    Last Post: 12-10-2007, 02:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •