-2
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 148, 120);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnStart = new JButton("Start");
    btnStart.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 
        {
            while(chckbxNewCheckBox.isSelected()){
            try {

                Robot auto = new Robot();


                auto.delay(2300);
                auto.mouseMove(377, 182);
                auto.mousePress(InputEvent.BUTTON3_DOWN_MASK);
                auto.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
                //
                auto.delay(1000);
                auto.mouseMove(466, 293);
                auto.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                //
                auto.delay(1000);
                auto.mouseMove(1061, 217);
                auto.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                //
                auto.delay(8000);
                auto.mouseMove(601, 493);
                auto.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                //
                auto.delay(60000);
                auto.mouseMove(387, 355);
                auto.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                //
                auto.delay(8000);
                auto.mouseMove(705, 652);
                auto.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                auto.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

            } catch (AWTException e) {
                e.printStackTrace();
            }
            }
        }       });
    btnStart.setBounds(10, 47, 89, 23);
    frame.getContentPane().add(btnStart);

    JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
    chckbxNewCheckBox.setBounds(2, 7, 97, 23);
    frame.getContentPane().add(chckbxNewCheckBox);
}

I'd like some advice on putting the commands for my robot in a loop and the loop only execute when a checkbox is selected. I've tried a few different ways of doing it but none seem to be working. I'm sure I'm missing something simple but I can't seem to find what it was. Although doing it for me would help, explaining it would be great too. I am using eclipse, and the windowbuilder to add items.

Lane will
  • 75
  • 9

2 Answers2

1

Use threads:

You can run your commands in a different thread. The UI will be running in its own thread.

you could write you code in you new thread as ;

Class Robot implements Runnable{
  public void run(){
    while(programRunning)){
    if(checkBox.isSelected())
      {
        //Perform commands.
       }
    else {// you may choose to sleep this thread here as well in case of not selected}
    }
  }

}

programRunning is another variable you can choose so as to keep the loop running even if the checkbox is not selected and the UI is program/application is still running.

Jor El
  • 199
  • 9
0

You can do something like this:

while(CheckBox.isSelected()) {
    // do the stuff
}

Update: I now understand the question. It is good to run long period task in a separate thread, so here you can use SwingWorker to perform the task. If you don't use thread, then the UI became hang by the while loop

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68