0

I have a method which brings up a transparent window overlay so I can indicate corner points of a rectangular onscreen area via clicks.

public Point getClickPoint(){ JFrame frame = new JFrame(""); MyMouseListener mouseL = new MyMouseListener(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setUndecorated(true); makeTranslucent(frame, Float.valueOf(0.40f)); frame.setSize(toolkit.getScreenSize()); frame.setVisible(true); frame.addMouseListener(mouseL); while(!mouseL.done){ try { Thread.sleep(4); } catch (InterruptedException e) { e.printStackTrace(); } } } 

When I call this in a normal way it works fine, but if I call it by a button press, then it hangs, doesn't register clicks and eventually freezes.

 Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { getClickPoint(); } } 

Is this something to do with the fact this is called originally by an action listener?

0

1 Answer 1

3

You are blocking the Event Dispatch Thread. Since the actionPerformed method will be called on the EDT, your while loop in getClickPoint will prevent the EDT from processing any events (including the mouse events you are waiting for), causing your program to become unresponsive.

If you need to perform time expensive tasks (such as blocking), take a look at SwingWorker.

Sign up to request clarification or add additional context in comments.

3 Comments

this is job for Swing Timer, exclusivelly
So I made a class called GetPointWorker which extends SwingWorker, whose doInBackground() task is as above. My method now instantiates a new GetPointWorker and calls doInBackground(), but it still freezes. Am I structuring this terribly wrongly?
@skelly You don't call doInBackground directly. You call execute, and doInBackground will eventually be called on a separate Thread.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.