Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    infinity is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    have the button set a boolean that the loop checks, and then bails out if the boolean is true.

  3. #3
    infinity is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    infinity is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by infinity View Post
    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:

    Java Code:
    boolean quit = false;
    for (int i = 0; i > MAX && !quit; i++)
    {
      //....
    }
    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.
    Last edited by Fubarable; 04-07-2009 at 04:47 AM.

  7. #7
    infinity is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by infinity View Post
    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.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  10. #10
    infinity is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    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.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by infinity View Post
    I don't know what key binding is
    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.

    Java Code:
    // instance variable
    private boolean exit;
    ...
    this code isn't compilable, making it hard to fully analyze.

    Java Code:
    public class act implements ActionListener{
     public void actionPerformed(ActionEvent event){
      while(!exit)
       process();
     }
    }
    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.

  12. #12
    infinity is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    yep, it locks up my app

    i'll check the stuff out, thanks

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    again, what is the process method doing? what is the goal of all of this?

  14. #14
    infinity is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    it's a bot
    the process moves the mouse at certain locations, clicks, and repeats

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Use a Swing Timer then.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For instance, here is my 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();
          }
        });
      }
    }
    Note that this is a fully compilable runnable program to demonstrate an issue, an SSCCE.

  17. #17
    infinity is offline Member
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default

    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?

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by infinity View Post
    i haven't taken any formal classes on swing, i learned what i know from a book
    than you're one up on me as I've taken no formal classes nor gone through a book.

    i'm not too familiar with what you're doing
    is that threading?
    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)

  19. #19
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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.

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by OrangeDog View Post
    Well technically it is threading, though not explicitly. Any time you use a listener or a SwingTimer you are writing multi-threaded code.
    True, as long as the OP understands that while the Swing Timer runs the timer itself in a background thread, that none of the processes run in the Timer's ActionListener are done in a background thread, that it's all done on the main Swing thread, the EDT.

Page 1 of 2 12 LastLast

Similar Threads

  1. Key event
    By ivvgangadhar in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-11-2008, 09:27 AM
  2. MouseDragged Event
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 03-04-2008, 05:09 AM
  3. Event
    By nt5515 in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 04:44 PM
  4. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM
  5. SWT Event Handling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-30-2007, 12:21 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
  •