1

I wanted to make a program with JFrame that would allow you to start and stop a JLabel from being updated with the most recent coordinate of the mouse cursor. I can't figure out what I have done wrong. Please help and be respectful. I'm new at Java so I can't help what dumb mistakes I've made.

Here is the code:

import java.awt.* ;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class PrintPos extends JFrame {
    private static final long serialVersionUID = 7526472295622776147L;
    public JButton startBtn;
    public JButton stopBtn;
    public static JLabel posLabelX;
    public static JLabel posLabelY;
    static boolean started=false;

    public static void main(String args[]){
        new PrintPos();
    }


    PrintPos() {

        setLayout(new GridLayout(2,2));

        startBtn = new JButton("Start");
        stopBtn = new JButton("Stop");
        posLabelY = new JLabel("X:");
        posLabelX = new JLabel("Y:");

        add(startBtn);
        add(stopBtn);
        add(posLabelX);
        add(posLabelY);

        setSize(200,150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);

        startBtn.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent a)
          {
              started = true;
              while(started) {
                    PointerInfo inf = MouseInfo.getPointerInfo();
                    Point p = inf.getLocation();
                    posLabelX.setText(String.valueOf(p.x));
                    posLabelY.setText(String.valueOf(p.y));
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        return;
                    }
                    }
          }
        });

        stopBtn.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
              started = false;
          }
        });
    }

        public static void getInfo() {
            while(started) {
                PointerInfo inf = MouseInfo.getPointerInfo();
                Point p = inf.getLocation();
                posLabelX.setText(String.valueOf(p.x));
                posLabelY.setText(String.valueOf(p.y));
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    return;
                }
                }
        }

}
  • You're blocking the event dispatching thread, have a look at [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details and [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) and [How to use Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) for some ideas for solutions and for a runnable example, have a look [here](http://stackoverflow.com/questions/13061122/getting-rgb-value-from-under-mouse-cursor/13061320#13061320) – MadProgrammer Sep 19 '15 at 00:35

1 Answers1

0

You're blocking the event dispatching thread. The EDT is responsible for processing events and paint requests and responding to user input. While your while is running in the ActionListener, the EDT is unable to process any new events and your program will "hang"

Have a look at Concurrency in Swing for more details.

Have a look at Worker Threads and SwingWorker and How to use Swing Timers for some ideas for solutions.

And for a runnable example, have a look here

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Other than your helpful answers, what (if any) simple beginner errors have I made that should be corrected? Any redundancies or incorrect statements that I should change? – Jordan Keller Sep 19 '15 at 00:45
  • Other then blocking the EDT, which is your main problem, it's typically discouraged to extend from `JFrame` itself, you're locking yourself into a single use-case, `JFrame` has a complex enough structure on it's own and you're not really adding any value. `JPanel` might be a better starting point. You should also be starting the UI from within the context of the EDT, see [Initial Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) for more details, this will help prevent any possible issues with thread locks and other issues – MadProgrammer Sep 19 '15 at 02:33