0

So I have a button that when pressed needs to write the current mouse position out to a text box until the user presses shift, then it stops and leaves the most recent mouse position as the final text in the text box. Heres what I have done:

First a created the following class.

public class KeyListener extends KeyAdapter {
    private boolean wasPressed = false;
    private int keyCode;

    public KeyListener(int keyCode) {
        this.keyCode = keyCode;
    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("CALLED");
        if(e.getKeyCode() == keyCode)
            wasPressed = true;
    }

    public void setState(boolean state) {
        wasPressed = state;
    }

    public boolean getState() {
        return wasPressed;
    }
}

Then in my "main" class I have the following code.

JButton track1 = new JButton("Track");
KeyListener kl = new KeyListener(KeyEvent.VK_SHIFT);
...
public DisplayFrame() {
    this.addKeyListener(kl);
    track1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event) {
            kl.setState(false);
            while(!kl.getState()) {
                Point p = MouseInfo.getPointerInfo().getLocation();
                topLeft.setText(p.getX() + "," + p.getY());
            }
        }
    });
}

I then of course added the text box to a JPanel and it's displaying everything correctly, however, when I click the Track button nothing happens. I can tell that it is entering the loop, but no text is displayed in the textbox and pressing shift doesn't break the loop.

  • 1. for better help sooner post an SSCCE / MCVE, short, runnable, compilable, 2. `while(!kl.getState()) {` maybe/can creates endless loop? – mKorbel May 31 '16 at 20:55
  • I can try that. And the goal would be to have it stop when its state becomes true, which if it were to work as planned would be once Shift is pressed. – wfish454 Tutorials May 31 '16 at 21:16
  • The while loop is executing within the context of the EDT, which will prevent it from updating the UI. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. It might be better to have a look at [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – MadProgrammer May 31 '16 at 21:21
  • This [example](http://stackoverflow.com/questions/21585121/java-getting-mouse-location-on-multiple-monitor-environment/21592711#21592711) provides an example how to poll `MouseInfo` – MadProgrammer May 31 '16 at 21:23
  • @MadProgrammer Thanks! I managed to fix it using the SwingWorker class. – wfish454 Tutorials May 31 '16 at 22:19

1 Answers1

0

Try to make a new thread within the actionPerformed method like this:

Thread exampleThread = new Thread() {

    public void run() {
        //Do your actions within the new thread
    }
};

//After the thread is made, we start it.
exampleThread.start();

You have to do this because the actionListener runs in a different thread.

eldo
  • 1,327
  • 2
  • 16
  • 27
samikroon
  • 11
  • 3