Here is my code :
Button start = new Button("Start");
start.setPrefHeight(100d);
start.setMaxWidth(Double.MAX_VALUE);
start.setOnMouseClicked(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent arg0) {
//ss.startServer();
System.out.println("adfasddasfrwgafsdasdf");
}
});
The message does not get printed on the console at eclipse! I want the event to trigger when the mouse is released. What am I doing wrong?
Upon furthet investagation I realised that if I have another mouseReleased event on another node , it will fire more events!... why is that? For example , here is the code I am using, when I press the start button , it prints Start and Reload!
start.setOnMouseReleased(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent arg0) {
ss.startServer();
System.out.println("Start");
arg0.consume();
}
});
Button stop = new Button("Stop");
stop.setPrefHeight(100d);
stop.setMaxWidth(Double.MAX_VALUE);
stop.setOnMouseReleased(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
sc.stopServer();
System.out.println("Stop");
event.consume();
}
});
Button reload = new Button("Reload");
reload.setPrefHeight(100d);
reload.setMaxWidth(Double.MAX_VALUE);
start.setOnMouseReleased(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent some) {
sc.reloadServer();
System.out.println("Reload");
some.consume();
}
});
Stop button works properly! Anyway , if the other piece of code works , here it is(http://pastebin.com/4f55R2gN)
2 Answers 2
You typo'd your code. You set your MouseReleased handler on the start object instead of reload.
Button reload = new Button("Reload");
reload.setPrefHeight(100d);
reload.setMaxWidth(Double.MAX_VALUE);
start.setOnMouseReleased(new EventHandler<MouseEvent>(){
Comments
Try this
button.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent arg0) {
System.out.println("button click and mouse released");
}
});
The below works for me fine.
public class FxmlSample extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws IOException{
JFrame frame = new JFrame();
JFXPanel jfxPanel = new JFXPanel();
Pane pane = new Pane();
VBox vBox = new VBox();
Button button = new Button("Click me");
button.setMinSize(50.0, 50.0);
button.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent arg0) {
System.out.println("11111111111111");
}
});
Button button_2 = new Button("Click me 2");
button_2.setMinSize(50.0, 50.0);
button_2.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent arg0) {
System.out.println("2222222222222");
}
});
vBox.setPadding(new Insets(20));
vBox.getChildren().addAll(button, button_2);
pane.getChildren().addAll(vBox);
Scene scene = new Scene(pane);
jfxPanel.setScene(scene);
frame.add(jfxPanel);
frame.setVisible(true);
frame.setMinimumSize(new Dimension(700, 700));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
1 Comment
Explore related questions
See similar questions with these tags.