Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-20-2008, 10:58 AM
Member
 
Join Date: Aug 2008
Posts: 7
Rep Power: 0
SomeGuyOverThere is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-20-2008, 02:54 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-20-2008, 07:01 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
For more see Lesson: Writing Event Listeners and How to Write an Action Listener.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-20-2008, 11:46 PM
Member
 
Join Date: Aug 2008
Posts: 7
Rep Power: 0
SomeGuyOverThere is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-20-2008, 11:52 PM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 571
Rep Power: 2
fishtoprecords is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-21-2008, 02:16 AM
Member
 
Join Date: Aug 2008
Posts: 7
Rep Power: 0
SomeGuyOverThere is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-21-2008, 10:30 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Guarded Blocks
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);
    }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
waiting for a file Fleur New To Java 2 06-23-2008 09:18 PM
can you help me with mouse pressed method please? java_fun2007 New To Java 4 05-22-2008 11:23 PM
how to display a sum of all previously pressed numbers in JTextField? all eyes New To Java 2 03-30-2008 09:38 PM
when muse pressed the background change pcman Java Applets 1 03-18-2008 12:51 AM
key pressed event kavithas New To Java 7 12-10-2007 03:01 PM


All times are GMT +2. The time now is 07:49 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org