So i have this code here, and it quite literally doesn't work and i just don't know why. It doesn't click, doesn't print out anything. My goal is to make the buttons function as switches between two methods of clicking. Right mouse button clicks, and Left mouse button clicks. Can anyone tell me why this doesn't function?
if (rbRightClickRadioButton.isSelected()) {
System.out.println("RMB Clicker");
Robot clicker = null;
try {
clicker = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
clicker.mousePress(InputEvent.BUTTON2_DOWN_MASK);
clicker.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
Thread.sleep(delay);
try {
clicker = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
clicker.mousePress(InputEvent.BUTTON2_DOWN_MASK);
clicker.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
} else if (rbRightClickRadioButton.isSelected()) {
System.out.println("LMB Clicker");
Robot clicker = null;
try {
clicker = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
clicker.mousePress(InputEvent.BUTTON2_DOWN_MASK);
clicker.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
Thread.sleep(delay);
try {
clicker = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
clicker.mousePress(InputEvent.BUTTON2_DOWN_MASK);
clicker.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
If you want a button to work as a switch between two options, I would rather use checkbox than radiobutton. The main problem your code has right now is the simple fact that you check the same expression in your if statements.
You should either check if the other radiobutton is selected in your else if statement or just assume that if the rbRightClickRadioButton is not selected, then the other one is, and you don't use else if just a simple else.
Related
I have a grid Pane which I want to make visible. I want to then pause the program for 2 seconds and make the grid invisible again. For some reason the grid becomes visible after the thread.sleep that I use in my program.
This all happens inside a button click event.
I tried moving around the thread.sleep, putting them in a new method and using multiple sleep but nothing worked.
gameGrid.setVisible(true)
gameGrid.setVisible(false)
Button event:
public void handleButtonGo(ActionEvent Event) throws IOException { //On go button press
boolean validation = true;
try {
gameGrid.setVisible(true);
placeShips();
}catch (Exception e){
labelwarning.setText(e.getMessage()); //on error the program will stop trying to place ships and refresh any ships placed so far.
validation = false;
//gameGrid.getChildren().clear();
//BoardSetup();
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
gameGrid.setVisible(false);
}
}
The grid is displayed for a millisecond after the thread.sleep.
Use PauseTransition.
public void handleButtonGo(ActionEvent Event) throws IOException { //On go button press
boolean validation = true;
try {
gameGrid.setVisible(true);
placeShips();
}catch (Exception e){
labelwarning.setText(e.getMessage()); //on error the program will stop trying to place ships and refresh any ships placed so far.
validation = false;
//gameGrid.getChildren().clear();
//BoardSetup();
PauseTransition wait = new PauseTransition(Duration.seconds(2));
wait.setOnFinished((e) -> {
/*YOUR METHOD*/
gameGrid.setVisible(false);
});
wait.play();
}
}
So I have this FormatedTextField
JFormattedTextField myFtf = new JFormattedTextField();
which has the following mask, placed in my application constructor
try {
myFtf.setFormatterFactory(
new DefaultFormatterFactory(
new MaskFormatter("###.###.###-##")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
Then, I have a two radio buttons, which should be changing the mask formatter in myFtf.
I have tried the following:
private radioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
myFtf.setFormatterFactory(
new DefaultFormatterFactory(
new MaskFormatter("###.###.###-##")));
} catch (Exception e) {
e.printStackTrace();
}
}
private void radioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
myFtf.setFormatterFactory(
new DefaultFormatterFactory(
new MaskFormatter("##.###.###/####-##")));
} catch (Exception e) {
e.printStackTrace();
}
}
Which works fine, until I try to change their masks when there is input within the text field. In case there is, it doesn't change the mask anymore. Here are a couple of prints:
OK scenario:
img a:
switching radio buttons gives me this:
img b:
Buggy scenario:
img c:
switching radio buttons gives me this:
img d:
I was expecting img d to be exactly like img a
How can I dynamically change its mask correctly?
Change your action listeners to this:
private radioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
myFtf.setFormatterFactory(
new DefaultFormatterFactory(
new MaskFormatter("###.###.###-##")));
myFtf.setText("");
} catch (Exception e) {
e.printStackTrace();
}
}
private void radioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
myFtf.setFormatterFactory(
new DefaultFormatterFactory(
new MaskFormatter("##.###.###/####-##")));
myFtf.setText("");
} catch (Exception e) {
e.printStackTrace();
}
}
That should clear the text fields.
Good luck!
I got it working correctly! All I needed to do was adding a
myFtf.setValue(null);
after setting the new formatter factory. myFtf.setText("") wasn't working as expected, but it was a close shot! :-)
I want to control mouse by robot class. Just move to left or right. Meanwhile I run eclipse with administrator status. Here is my code below.
public class ControlMouse {
public static void main(String[] args) {
try {
Robot robot = new Robot();
Thread.sleep(1000);
robot.mouseMove(0, 0);
Thread.sleep(1000);
robot.delay(1000); // this one is the same...
robot.mouseMove(100, 100);
} catch (Exception e) {
System.out.println("e = " + e.toString());
} catch (Error e) {
System.out.println("e = " + e.toString());
}
}
}
But it seems doesn't work and no error or exception message. Dose any one may give me some advice? Thank you. The development environment is window 7. And I expect I can see the mouse cursor or traces will change. But I can’t.
You will have to put some delay and then check. without delay / sleep, sometimes GUI elements cannot be handled properly. You might even miss the event.
class ControlMouse {
public static void main(String[] args) {
try {
Robot robot = new Robot();
Thread.sleep(1000);
robot.mouseMove(0, 0);
Thread.sleep(1000);
robot.mouseMove(100, 100);
} catch (Exception e) {
System.out.println("e = " + e.toString());
} catch (Error e) {
System.out.println("e = " + e.toString());
}
}
}
The above code will work. It will move to 0,0 and then to 0,100
I have a jbutton which I want, as soon as it's clicked to make an infinite loop of a mouseMove by a robot class. Then, to be stopped when it is clicked again. Problem is in my code when I press it for the first time, it causes the system to freeze and nothing happens when I click it again. I use:
boolean go = false
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
go = !go;
if (go)
jb.setText("Stop!");
else
jb.setText("Start!");
try {
Robot robot = new Robot();
while (go) {
robot.mouseMove(500, 500);
robot.delay(1000);
robot.mouseMove(500, 400);
}
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
Swing is a single threaded environment. This means that if anything should block this thread, Swing will be unable to respond to new events, including paint requests and it will appear that you UI has been frozen...because it has...
Take a look at Concurrency in Swing
The simplest solution might be to spawn a new thread and run your loop within it...
Start by writing a Runnable that handels the work you want done...
public class MouseRunner implements Runnable {
#Override
public void run() {
try {
Robot robot = new Robot();
while (go) {
robot.mouseMove(500, 500);
robot.delay(1000);
robot.mouseMove(500, 400);
}
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
Then when the user first clicks the button, create a Thread and start it...
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
go = !go;
if (go) {
jb.setText("Stop!");
Thread t = new Thread(new MouseRunner());
t.start();
} else {
jb.setText("Start!");
}
}
});
Beware, your go variable is likely going to need to made volatile
I have read and understood how the Robot class in java works. Only thing I would like to ask, is how do I press and release the mouse button inside an if statement. For example I would to make a click only if (and right after) the space button is pressed/released. I would use the code:
try {
Robot robot = new Robot();
if (/*insert my statement here*/) {
try {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (InterruptedException ex) {}
}
} catch (AWTException e) {}
Unfortunately, there isn't a way to directly control hardware (well, in fact there is, but you would have to use JNI/JNA), this means that you can't simply check if a key is pressed.
You can use KeyBindings to bind the space key to an action, when the spacebar is pressed you set a flag to true, when it's released you set that flag to false. In order to use this solution, your application has to be a GUI application, this won't work with console applications.
Action pressedAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
spaceBarPressed = true;
}
};
Action releasedAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
spaceBarPressed = false;
}
};
oneOfYourComponents.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "pressed");
oneOfYourComponents.getInputMap().put(KeyStroke.getKeyStroke("released SPACE"), "released");
oneOfYourComponents.getActionMap().put("pressed", pressedAction);
oneOfYourComponents.getActionMap().put("released", releasedAction);
Then, use
try {
Robot robot = new Robot();
if (spaceBarPressed) {
try {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (InterruptedException ex) {
//handle the exception here
}
}
} catch (AWTException e) {
//handle the exception here
}
As GGrec wrote, a better way to do it would be to execute your mouse press directly when the keyboard event is fired:
Action pressedAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (InterruptedException ex) {
//handle the exception here
}
}
};
My suggestion is that you listen for the keyboard event, and when you receive it, you execute your code without the if statement. Add the listener to your canvas, or whatever.
Careful not to recreate the Robot class each time.
new KeyAdapter() {
#Override
public void keyReleased(final KeyEvent e) {
if (e.keyCode == KeyEvent.VK_SPACE)
try {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (InterruptedException ex) {
}
}
}