Results 1 to 7 of 7
  1. #1
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default Using a while loop to wait for mouse click -> inefficient?

    I am still in the design portion of a program I want to make for fun, and am brainstorming how to handle a certain aspect, waiting for a mouseclick.

    Basically, I want the user to be able to enter a value, hit enter or a button, then click on a panel in a certain area, and I want both the value and the mouse click event to go to a certain method.

    It's obviously easy to pass the value (most likely in a JTextField) to the method, but associating a mouseclick with it too is what's getting me.

    One way I though of doing it is with a method that returns the next mouse event. It would have some instance variable, a mouseClick event, that is made null. Then when the mouse listener picks up on something, set the mouse event to that. If I did this, how would this while statement work?

    Java Code:
    while (mouseEvent1 == null)
    {}
    return mouseEvent1;
    Basically I want the while to loop and loop until a mouse event is recorded. Is this a good way to do this? Would it cause trouble / a lot of cycles or just 1 since it's not seeing mouseEvent1 changing?

    I know this is a pretty abstract question and I could try it out now, but I'm not sure if I'll be able to detect the poor style of this approach.

    Thanks very much!

  2. #2
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    I can confirm that such a constantly running while-loop is one of the must ugly pieces of code around. When the mouse is clicked, a mouse event is generated and send to the corresponding mouseListener methode. In that methode you can call the right methode to do the work, possibly in another thread.

  3. #3
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    thanks, that's what I thought. The problem is I will have a few different methods that require a mouse click with them, so a general waitForClick() type method would be ideal, so I could attach it to these methods.

    I think I will use some type of boolean structure, the first method will turn on some boolean and then when the mouse click happens, it will look for which boolean is true, then the mouse listener will call the appropriate methods and reset the booleans.

    I saw some things about multiple threads, Swing Worker, etc for handling this type of event management. Any other ideas how one would properly tackle this problem?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    See How to write a mouse listener. There shouldn't be a need for a while loop to listen for mouse events when its already taken care of for you as described in the link.

  5. #5
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    Well I understand using a mouse listener or adapter, the challenge at hand is linking it with another action.

    For example I have 2 text fields, let's say A and B. I want the user to be able to enter something in either of them, hit enter, THEN click an area and I want both the value from the corresponding text field (A or B, depends on which they hit enter on) AND the mouse event to go to a certain method.

    From the beginning I've been doing this with booleans, and it's not too bad with just a few different possibilities (text fields) but with more than that I don't think this is a good solution. I am curious to see how others would go about doing this.

    Ideally I would love a method like nextMouseClick(). So when my action listener for the text field gets triggered, I can pass that value along with nextMouseClick() (that would return a mouse event) to the correct method. Instead now I will use the text field action listener to switch a boolean, then when the mouse listener gets triggered, it will check which booleans are true, and then call the correct method. Will work for sure, but it doesn't seem like the best way.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    I think I'm clear on what you want to do, and still encourage you to use a MouseListener. Alternative to using booleans, you could just add the listener when the action listener is fired and remove when the click is performed
    Java Code:
    JTextField field1 = new JTextField();
    JPanel mousePanel = new JPanel();
    MouseListener panelMouseListener = new MouseAdapter(){
        public void mousePressed(MouseEvent e){
            mousePanel.removeMouseListener(this);//prevent from being fired until the action listener is fired again
            //do something else
        }
    }
    ....
    field1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            mousePanel.addMouseListener(panelMouseListener);//add the listener to listen for mouse pressed events
            //do something else
        }
    }
    Last edited by doWhile; 01-03-2011 at 02:14 AM.

  7. #7
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    thanks doWhile, that's an interesting approach. Let me tell you just a bit more about my application and you can hopefully tell me if you think your approach will work.

    Let's say both text fields, A and B, should call some method A or B, respectively, and both of these methods want the value from the respective text field, and the next mouse event to occur from a press on the panel.

    I was going to have the same listener for the text fields, with 2 if statements to determine the invoking field. The correct boolean will then be set true, let's say fieldATrue. Then the mouse listener will also have 2 if statements, checking which field was true. It can then call the appropriate method with the field data and the mouse event.

Similar Threads

  1. Mouse Click changes Color of Rectangle
    By DocIcarus in forum New To Java
    Replies: 1
    Last Post: 11-23-2010, 02:53 PM
  2. change color on mouse click
    By hannerz06 in forum New To Java
    Replies: 3
    Last Post: 03-31-2010, 10:46 PM
  3. Using an EMG signal to create a mouse click
    By cmc419 in forum New To Java
    Replies: 1
    Last Post: 03-27-2009, 06:38 PM
  4. mouse click do not work after repaint
    By nobody in forum Java 2D
    Replies: 8
    Last Post: 12-07-2008, 05:43 PM
  5. mouse click alert
    By amir in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2008, 11:42 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
  •