Results 1 to 7 of 7
- 01-02-2011, 04:21 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
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;
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!
- 01-02-2011, 05:42 PM #2
Senior Member
- Join Date
- Aug 2010
- Posts
- 127
- Rep Power
- 0
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.
- 01-02-2011, 06:46 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
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?
- 01-02-2011, 08:43 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
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.
- 01-02-2011, 11:26 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
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.
- 01-03-2011, 02:11 AM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
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.
- 01-03-2011, 08:08 AM #7
Member
- Join Date
- Sep 2010
- Posts
- 30
- Rep Power
- 0
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
-
Mouse Click changes Color of Rectangle
By DocIcarus in forum New To JavaReplies: 1Last Post: 11-23-2010, 02:53 PM -
change color on mouse click
By hannerz06 in forum New To JavaReplies: 3Last Post: 03-31-2010, 10:46 PM -
Using an EMG signal to create a mouse click
By cmc419 in forum New To JavaReplies: 1Last Post: 03-27-2009, 06:38 PM -
mouse click do not work after repaint
By nobody in forum Java 2DReplies: 8Last Post: 12-07-2008, 05:43 PM -
mouse click alert
By amir in forum AWT / SwingReplies: 1Last Post: 08-05-2008, 11:42 PM
Bookmarks