Results 1 to 20 of 23
- 04-07-2009, 04:07 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
checking for an event during an event
Say you click a button which loops a process, but you want to terminate that process when you click the button again, or press some key.
So for example, I registered the listener object with the button. When I press the button, it triggers an ActionEvent and goes to the actionPerformed method wherein it loops some process. I want to make it so that the loops ends if the user decides to press a key(or something else that triggers an event).
How do you do that?Last edited by infinity; 04-07-2009 at 04:14 AM.
-
have the button set a boolean that the loop checks, and then bails out if the boolean is true.
- 04-07-2009, 04:21 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
No.
I have a way to terminate the loop if the key is pressed, I don't need a boolean value.
The problem is, if I press the key during the loop(during the initial event), KeyEvent isn't fired.
What component am I supposed to register the keylistener object to?Last edited by infinity; 04-07-2009 at 04:27 AM.
-
I generally don't use key listeners unless an absolute necessity. Usually I use key binding instead, and I bind it to whatever happens to be the most logical component at the time.
And yes, I still recommend that the keybinding change a boolean and that this bump you out of the loop.
- 04-07-2009, 04:40 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
It can be anything, it doesn't have to be a key.
How can I check for an event during an event?
...
I have an instance variable(an integer which the loops relies on) which gets changed if the KeyEvent is fired. I DON'T need a boolean.
-
Since you're asking the question, how do you know for sure what you need or don't need?
Even for loops can contain either boolean conditions or boolean-induced breaks. For instance:
But I think that you need to give more detail and some code here for better answers. We can't begin to guess what's in your code at present. Perhaps you shouldn't be using a for loop here, but rather a SwingWorker background thread or a Swing Timer. But again without more information, all we can do is guess.Java Code:boolean quit = false; for (int i = 0; i > MAX && !quit; i++) { //.... }Last edited by Fubarable; 04-07-2009 at 04:47 AM.
- 04-07-2009, 04:49 AM #7
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
I'm not asking for help on terminating a loop.
IF the KeyEvent is fired, it will change the instance variable, and thus end the loop in the ongoing ActionEvent(by the button).
Part of the problem is that I don't know what component to register the KeyListener object to.
-
Again, you probably don't want to use a KeyListener but rather use key bindings. The sun tutorials will give a decent explanation why you should do this. Other than that, I'm lost as I have no idea really what your main question is. Sorry.
I think that your best bet here is to show us your code. We don't want to see all of it, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem, in other words, an SSCCE (Short, Self Contained, Correct (Compilable), Example). For more info on SSCCEs please look here:
Short, Self Contained, Correct Example
Again, if the code is compilable and runnable more people will be able to help you.
-
Also, what is this loop that's within the button's action listener doing, both in a coding sense and in a general program structure sense?
- 04-07-2009, 05:03 AM #10
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
I don't know what key binding is
Java Code:// instance variable private boolean exit; ... //inside some method JButton x = new JButton("x"); x.addActionListener(new act()); ... public class act implements ActionListener{ public void actionPerformed(ActionEvent event){ while(!exit) process(); } } ... public class keys implements KeyListener{ public void keyPressed(KeyEvent k){ if(k.getKeyCode()==KeyEvent.VK_Q) exit=true; } public void keyReleased(KeyEvent k){} public void keyTyped(KeyEvent k){} }Last edited by infinity; 04-07-2009 at 05:06 AM.
-
the Sun Swing tutorials will answer this for you. Again, search the tutorial (go to the "Really Big Index" and search on binding) and have a look. It will open your eyes.
this code isn't compilable, making it hard to fully analyze.Java Code:// instance variable private boolean exit; ...
This looks to be a loop that will lock up the EDT, the Event Dispatch Thread, causing your app to lock up. do you notice that when the loop is triggered, your app becomes completely unresponsive to button presses and whatnot? If so, you really should look to doing this loop in either a SwingWorker background thread or use a Swing Timer, both as suggested above.Java Code:public class act implements ActionListener{ public void actionPerformed(ActionEvent event){ while(!exit) process(); } }
- 04-07-2009, 05:15 AM #12
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
yep, it locks up my app
i'll check the stuff out, thanks
-
again, what is the process method doing? what is the goal of all of this?
- 04-07-2009, 05:24 AM #14
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
it's a bot
the process moves the mouse at certain locations, clicks, and repeats
-
Use a Swing Timer then.
-
For instance, here is my SSCCE:
Note that this is a fully compilable runnable program to demonstrate an issue, an SSCCE.Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JComponent; import javax.swing.Timer; public class ShowTimer { public static final int TIMER_DELAY = 200; private JPanel mainPanel = new JPanel(); Timer swingTimer; public ShowTimer() { JButton toggleProcess = new JButton("Toggle Process"); toggleProcess.addActionListener(new ToggleBtnListner()); mainPanel.add(toggleProcess); } public void process() { System.out.println("process active"); } public JComponent getComponent() { return mainPanel; } private class ToggleBtnListner implements ActionListener { public void actionPerformed(ActionEvent e) { if (swingTimer != null && swingTimer.isRunning()) { swingTimer.stop(); swingTimer = null; } else { swingTimer = new Timer(TIMER_DELAY, new ActionListener() { public void actionPerformed(ActionEvent e) { process(); } }); swingTimer.start(); } } } private static void createAndShowUI() { JFrame frame = new JFrame("ShowTimer"); frame.getContentPane().add(new ShowTimer().getComponent()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
- 04-08-2009, 12:54 AM #17
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
i haven't taken any formal classes on swing, i learned what i know from a book
i'm not too familiar with what you're doing
is that threading?
-
than you're one up on me as I've taken no formal classes nor gone through a book.
No, that's using a Swing Timer object. You can learn more about it by going through the appropriate Sun tutorial: How to Use Swing Timers (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Other Swing Features)i'm not too familiar with what you're doing
is that threading?
- 04-08-2009, 02:22 AM #19
Well technically it is threading, though not explicitly. Any time you use a listener or a SwingTimer you are writing multi-threaded code. To be able to use these correctly and efficiently you need to know about how threading works. Fortunately, Sun provides hand tutorials, available via some of the links already given.
-
Similar Threads
-
Key event
By ivvgangadhar in forum Threads and SynchronizationReplies: 1Last Post: 12-11-2008, 09:27 AM -
MouseDragged Event
By Preethi in forum New To JavaReplies: 1Last Post: 03-04-2008, 05:09 AM -
Event
By nt5515 in forum New To JavaReplies: 0Last Post: 02-15-2008, 04:44 PM -
Listener for SWT event
By Java Tip in forum Java TipReplies: 0Last Post: 01-08-2008, 09:04 AM -
SWT Event Handling
By Java Tip in forum Java TipReplies: 0Last Post: 12-30-2007, 12:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks