Results 1 to 7 of 7
- 08-20-2008, 09:58 AM #1
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
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.
- 08-20-2008, 01:54 PM #2
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.
- 08-20-2008, 06:01 PM #3
For more see Lesson: Writing Event Listeners and How to Write an Action Listener.
- 08-20-2008, 10:46 PM #4
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
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?
- 08-20-2008, 10:52 PM #5
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.
- 08-21-2008, 01:16 AM #6
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
- 08-21-2008, 09:30 PM #7
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
-
waiting for a file
By Fleur in forum New To JavaReplies: 2Last Post: 06-23-2008, 08:18 PM -
can you help me with mouse pressed method please?
By java_fun2007 in forum New To JavaReplies: 4Last Post: 05-22-2008, 10:23 PM -
how to display a sum of all previously pressed numbers in JTextField?
By all eyes in forum New To JavaReplies: 2Last Post: 03-30-2008, 08:38 PM -
when muse pressed the background change
By pcman in forum Java AppletsReplies: 1Last Post: 03-17-2008, 11:51 PM -
key pressed event
By kavithas in forum New To JavaReplies: 7Last Post: 12-10-2007, 02:01 PM
Bookmarks