JavaFX, MouseEvent problems











up vote
1
down vote

favorite












I am currently making a paint application and have created several tools which are working, but I encountered a problem when trying to create a
"Draw straight line" tool



So I basically draw a line from point A to B and it works, the line is there, however, when I toggle my other tools (Draw circle, rectangle etc) the shapes are being drawn at the same time as the straight-line despite the "Draw Line" button being toggled off.



The code below will allow you to draw straight-lines and you can try toggling on and off the different buttons, the straight line will still be drawn when you drag the cursor across the pane.



Anyone know what kind of mistake I did, and any possible fixes and/or alternate solutions?



(The event handler is there so that I can select the drawn shapes change them later if needed, this code is a stripped-down version of my paint application)



public class DrawLine extends Application {


@Override
public void start(Stage primaryStage) {

ToggleButton lineButton = new ToggleButton ("Draw Line");
ToggleButton Button = new ToggleButton ("Button with no function");

BorderPane pane = new BorderPane();
ToolBar toolbar = new ToolBar();
Scene scene = new Scene(pane, 1200, 800);
pane.setLeft(toolbar);
toolbar.getItems().addAll(lineButton, Button);


// Draw Line
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, me -> {
if(lineButton.isSelected() & me.getButton().equals(MouseButton.PRIMARY) ) {
scene.setOnMousePressed(event -> {
Line line = new Line();

line.setStartX(event.getX());
line.setStartY(event.getY());

scene.setOnMouseDragged(e->{

line.setEndX(e.getX());
line.setEndY(e.getY());
});

pane.getChildren().add(line);

});
}
});


primaryStage.setTitle("Paint App");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String args) {
launch();
}


}










share|improve this question







New contributor




CorvaxEntity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • First of all, please follow java naming convention. Your Button is highlighted as a class because it starts with an upper-case, even though it's a variable.
    – Kartik
    Nov 5 at 2:15










  • @Kartik Appreciate it, however, I do usually follow the naming convention, I just made this code in a hurry and it led to a mistake :)
    – CorvaxEntity
    Nov 5 at 2:19










  • no worries, upvoted
    – Kartik
    Nov 5 at 2:26















up vote
1
down vote

favorite












I am currently making a paint application and have created several tools which are working, but I encountered a problem when trying to create a
"Draw straight line" tool



So I basically draw a line from point A to B and it works, the line is there, however, when I toggle my other tools (Draw circle, rectangle etc) the shapes are being drawn at the same time as the straight-line despite the "Draw Line" button being toggled off.



The code below will allow you to draw straight-lines and you can try toggling on and off the different buttons, the straight line will still be drawn when you drag the cursor across the pane.



Anyone know what kind of mistake I did, and any possible fixes and/or alternate solutions?



(The event handler is there so that I can select the drawn shapes change them later if needed, this code is a stripped-down version of my paint application)



public class DrawLine extends Application {


@Override
public void start(Stage primaryStage) {

ToggleButton lineButton = new ToggleButton ("Draw Line");
ToggleButton Button = new ToggleButton ("Button with no function");

BorderPane pane = new BorderPane();
ToolBar toolbar = new ToolBar();
Scene scene = new Scene(pane, 1200, 800);
pane.setLeft(toolbar);
toolbar.getItems().addAll(lineButton, Button);


// Draw Line
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, me -> {
if(lineButton.isSelected() & me.getButton().equals(MouseButton.PRIMARY) ) {
scene.setOnMousePressed(event -> {
Line line = new Line();

line.setStartX(event.getX());
line.setStartY(event.getY());

scene.setOnMouseDragged(e->{

line.setEndX(e.getX());
line.setEndY(e.getY());
});

pane.getChildren().add(line);

});
}
});


primaryStage.setTitle("Paint App");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String args) {
launch();
}


}










share|improve this question







New contributor




CorvaxEntity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • First of all, please follow java naming convention. Your Button is highlighted as a class because it starts with an upper-case, even though it's a variable.
    – Kartik
    Nov 5 at 2:15










  • @Kartik Appreciate it, however, I do usually follow the naming convention, I just made this code in a hurry and it led to a mistake :)
    – CorvaxEntity
    Nov 5 at 2:19










  • no worries, upvoted
    – Kartik
    Nov 5 at 2:26













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am currently making a paint application and have created several tools which are working, but I encountered a problem when trying to create a
"Draw straight line" tool



So I basically draw a line from point A to B and it works, the line is there, however, when I toggle my other tools (Draw circle, rectangle etc) the shapes are being drawn at the same time as the straight-line despite the "Draw Line" button being toggled off.



The code below will allow you to draw straight-lines and you can try toggling on and off the different buttons, the straight line will still be drawn when you drag the cursor across the pane.



Anyone know what kind of mistake I did, and any possible fixes and/or alternate solutions?



(The event handler is there so that I can select the drawn shapes change them later if needed, this code is a stripped-down version of my paint application)



public class DrawLine extends Application {


@Override
public void start(Stage primaryStage) {

ToggleButton lineButton = new ToggleButton ("Draw Line");
ToggleButton Button = new ToggleButton ("Button with no function");

BorderPane pane = new BorderPane();
ToolBar toolbar = new ToolBar();
Scene scene = new Scene(pane, 1200, 800);
pane.setLeft(toolbar);
toolbar.getItems().addAll(lineButton, Button);


// Draw Line
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, me -> {
if(lineButton.isSelected() & me.getButton().equals(MouseButton.PRIMARY) ) {
scene.setOnMousePressed(event -> {
Line line = new Line();

line.setStartX(event.getX());
line.setStartY(event.getY());

scene.setOnMouseDragged(e->{

line.setEndX(e.getX());
line.setEndY(e.getY());
});

pane.getChildren().add(line);

});
}
});


primaryStage.setTitle("Paint App");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String args) {
launch();
}


}










share|improve this question







New contributor




CorvaxEntity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am currently making a paint application and have created several tools which are working, but I encountered a problem when trying to create a
"Draw straight line" tool



So I basically draw a line from point A to B and it works, the line is there, however, when I toggle my other tools (Draw circle, rectangle etc) the shapes are being drawn at the same time as the straight-line despite the "Draw Line" button being toggled off.



The code below will allow you to draw straight-lines and you can try toggling on and off the different buttons, the straight line will still be drawn when you drag the cursor across the pane.



Anyone know what kind of mistake I did, and any possible fixes and/or alternate solutions?



(The event handler is there so that I can select the drawn shapes change them later if needed, this code is a stripped-down version of my paint application)



public class DrawLine extends Application {


@Override
public void start(Stage primaryStage) {

ToggleButton lineButton = new ToggleButton ("Draw Line");
ToggleButton Button = new ToggleButton ("Button with no function");

BorderPane pane = new BorderPane();
ToolBar toolbar = new ToolBar();
Scene scene = new Scene(pane, 1200, 800);
pane.setLeft(toolbar);
toolbar.getItems().addAll(lineButton, Button);


// Draw Line
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, me -> {
if(lineButton.isSelected() & me.getButton().equals(MouseButton.PRIMARY) ) {
scene.setOnMousePressed(event -> {
Line line = new Line();

line.setStartX(event.getX());
line.setStartY(event.getY());

scene.setOnMouseDragged(e->{

line.setEndX(e.getX());
line.setEndY(e.getY());
});

pane.getChildren().add(line);

});
}
});


primaryStage.setTitle("Paint App");
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String args) {
launch();
}


}







java javafx mouseevent line draw






share|improve this question







New contributor




CorvaxEntity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




CorvaxEntity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




CorvaxEntity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 5 at 2:03









CorvaxEntity

82




82




New contributor




CorvaxEntity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





CorvaxEntity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






CorvaxEntity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • First of all, please follow java naming convention. Your Button is highlighted as a class because it starts with an upper-case, even though it's a variable.
    – Kartik
    Nov 5 at 2:15










  • @Kartik Appreciate it, however, I do usually follow the naming convention, I just made this code in a hurry and it led to a mistake :)
    – CorvaxEntity
    Nov 5 at 2:19










  • no worries, upvoted
    – Kartik
    Nov 5 at 2:26


















  • First of all, please follow java naming convention. Your Button is highlighted as a class because it starts with an upper-case, even though it's a variable.
    – Kartik
    Nov 5 at 2:15










  • @Kartik Appreciate it, however, I do usually follow the naming convention, I just made this code in a hurry and it led to a mistake :)
    – CorvaxEntity
    Nov 5 at 2:19










  • no worries, upvoted
    – Kartik
    Nov 5 at 2:26
















First of all, please follow java naming convention. Your Button is highlighted as a class because it starts with an upper-case, even though it's a variable.
– Kartik
Nov 5 at 2:15




First of all, please follow java naming convention. Your Button is highlighted as a class because it starts with an upper-case, even though it's a variable.
– Kartik
Nov 5 at 2:15












@Kartik Appreciate it, however, I do usually follow the naming convention, I just made this code in a hurry and it led to a mistake :)
– CorvaxEntity
Nov 5 at 2:19




@Kartik Appreciate it, however, I do usually follow the naming convention, I just made this code in a hurry and it led to a mistake :)
– CorvaxEntity
Nov 5 at 2:19












no worries, upvoted
– Kartik
Nov 5 at 2:26




no worries, upvoted
– Kartik
Nov 5 at 2:26












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You only check if the lineButton is selected inside the MOUSE_CLICKED (which is a press-then-release gesture, by the way) handler. Inside this handler you add a MOUSE_PRESSED handler and inside that handler you add a MOUSE_DRAGGED handler. You don't check if the lineButton is selected inside the MOUSE_PRESSED or MOUSE_DRAGGED handlers.



What this all means is that, after the if condition inside the MOUSE_CLICKED handler evaluates to true, you'll have a MOUSE_PRESSED and MOUSE_DRAGGED handler that operate independently of your MOUSE_CLICKED handler. Now, whenever you press any mouse button it will create a Line and add it to the parent. Then the newly added MOUSE_DRAGGED handler will alter the Line.



You're fortunate, in a way, that you're using the onXXX properties instead of using addEventHandler. The properties replace the old EventHandler when set. If that didn't happen (such as with addEventHandler) you'd have many (one more each time) EventHandlers drawing Lines.



You just need to register all the appropriate EventHandlers once and do the logic inside of them.



Here's a small example:



import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.ToolBar;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Main extends Application {

private ToggleGroup toggleGroup;
private ToggleButton lineBtn;

private Group group;
private Line currentLine;

@Override
public void start(Stage primaryStage) {
toggleGroup = new ToggleGroup();
lineBtn = new ToggleButton("Line");

ToggleButton noneBtn = new ToggleButton("None");
toggleGroup.getToggles().addAll(lineBtn, noneBtn);
toggleGroup.selectToggle(noneBtn);

group = new Group();

BorderPane root = new BorderPane(new Pane(group), new ToolBar(lineBtn, noneBtn), null, null, null);
root.getCenter().setOnMousePressed(this::handleMousePressed);
root.getCenter().setOnMouseDragged(this::handleMouseDragged);
root.getCenter().setOnMouseReleased(this::handleMouseReleased);

primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.setTitle("Draw Shape Example");
primaryStage.show();
}

private void handleMousePressed(MouseEvent event) {
if (lineBtn.equals(toggleGroup.getSelectedToggle())
&& event.getButton() == MouseButton.PRIMARY) {
currentLine = new Line(event.getX(), event.getY(), event.getX(), event.getY());
group.getChildren().add(currentLine);
}
}

private void handleMouseDragged(MouseEvent event) {
if (currentLine != null) {
currentLine.setEndX(event.getX());
currentLine.setEndY(event.getY());
}
}

private void handleMouseReleased(MouseEvent event) {
if (currentLine != null
&& currentLine.getStartX() == currentLine.getEndX()
&& currentLine.getStartY() == currentLine.getEndY()) {
// The line has no length, remove it
group.getChildren().remove(currentLine);
}
currentLine = null;
}

}





share|improve this answer























  • Thank you very much, been looking at your code and I feel more confident when it comes to event handlers and it also provided me with a solution, I really appreciate you spending your time helping me, thanks again :)
    – CorvaxEntity
    Nov 5 at 2:41













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






CorvaxEntity is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147393%2fjavafx-mouseevent-problems%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










You only check if the lineButton is selected inside the MOUSE_CLICKED (which is a press-then-release gesture, by the way) handler. Inside this handler you add a MOUSE_PRESSED handler and inside that handler you add a MOUSE_DRAGGED handler. You don't check if the lineButton is selected inside the MOUSE_PRESSED or MOUSE_DRAGGED handlers.



What this all means is that, after the if condition inside the MOUSE_CLICKED handler evaluates to true, you'll have a MOUSE_PRESSED and MOUSE_DRAGGED handler that operate independently of your MOUSE_CLICKED handler. Now, whenever you press any mouse button it will create a Line and add it to the parent. Then the newly added MOUSE_DRAGGED handler will alter the Line.



You're fortunate, in a way, that you're using the onXXX properties instead of using addEventHandler. The properties replace the old EventHandler when set. If that didn't happen (such as with addEventHandler) you'd have many (one more each time) EventHandlers drawing Lines.



You just need to register all the appropriate EventHandlers once and do the logic inside of them.



Here's a small example:



import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.ToolBar;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Main extends Application {

private ToggleGroup toggleGroup;
private ToggleButton lineBtn;

private Group group;
private Line currentLine;

@Override
public void start(Stage primaryStage) {
toggleGroup = new ToggleGroup();
lineBtn = new ToggleButton("Line");

ToggleButton noneBtn = new ToggleButton("None");
toggleGroup.getToggles().addAll(lineBtn, noneBtn);
toggleGroup.selectToggle(noneBtn);

group = new Group();

BorderPane root = new BorderPane(new Pane(group), new ToolBar(lineBtn, noneBtn), null, null, null);
root.getCenter().setOnMousePressed(this::handleMousePressed);
root.getCenter().setOnMouseDragged(this::handleMouseDragged);
root.getCenter().setOnMouseReleased(this::handleMouseReleased);

primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.setTitle("Draw Shape Example");
primaryStage.show();
}

private void handleMousePressed(MouseEvent event) {
if (lineBtn.equals(toggleGroup.getSelectedToggle())
&& event.getButton() == MouseButton.PRIMARY) {
currentLine = new Line(event.getX(), event.getY(), event.getX(), event.getY());
group.getChildren().add(currentLine);
}
}

private void handleMouseDragged(MouseEvent event) {
if (currentLine != null) {
currentLine.setEndX(event.getX());
currentLine.setEndY(event.getY());
}
}

private void handleMouseReleased(MouseEvent event) {
if (currentLine != null
&& currentLine.getStartX() == currentLine.getEndX()
&& currentLine.getStartY() == currentLine.getEndY()) {
// The line has no length, remove it
group.getChildren().remove(currentLine);
}
currentLine = null;
}

}





share|improve this answer























  • Thank you very much, been looking at your code and I feel more confident when it comes to event handlers and it also provided me with a solution, I really appreciate you spending your time helping me, thanks again :)
    – CorvaxEntity
    Nov 5 at 2:41

















up vote
2
down vote



accepted










You only check if the lineButton is selected inside the MOUSE_CLICKED (which is a press-then-release gesture, by the way) handler. Inside this handler you add a MOUSE_PRESSED handler and inside that handler you add a MOUSE_DRAGGED handler. You don't check if the lineButton is selected inside the MOUSE_PRESSED or MOUSE_DRAGGED handlers.



What this all means is that, after the if condition inside the MOUSE_CLICKED handler evaluates to true, you'll have a MOUSE_PRESSED and MOUSE_DRAGGED handler that operate independently of your MOUSE_CLICKED handler. Now, whenever you press any mouse button it will create a Line and add it to the parent. Then the newly added MOUSE_DRAGGED handler will alter the Line.



You're fortunate, in a way, that you're using the onXXX properties instead of using addEventHandler. The properties replace the old EventHandler when set. If that didn't happen (such as with addEventHandler) you'd have many (one more each time) EventHandlers drawing Lines.



You just need to register all the appropriate EventHandlers once and do the logic inside of them.



Here's a small example:



import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.ToolBar;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Main extends Application {

private ToggleGroup toggleGroup;
private ToggleButton lineBtn;

private Group group;
private Line currentLine;

@Override
public void start(Stage primaryStage) {
toggleGroup = new ToggleGroup();
lineBtn = new ToggleButton("Line");

ToggleButton noneBtn = new ToggleButton("None");
toggleGroup.getToggles().addAll(lineBtn, noneBtn);
toggleGroup.selectToggle(noneBtn);

group = new Group();

BorderPane root = new BorderPane(new Pane(group), new ToolBar(lineBtn, noneBtn), null, null, null);
root.getCenter().setOnMousePressed(this::handleMousePressed);
root.getCenter().setOnMouseDragged(this::handleMouseDragged);
root.getCenter().setOnMouseReleased(this::handleMouseReleased);

primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.setTitle("Draw Shape Example");
primaryStage.show();
}

private void handleMousePressed(MouseEvent event) {
if (lineBtn.equals(toggleGroup.getSelectedToggle())
&& event.getButton() == MouseButton.PRIMARY) {
currentLine = new Line(event.getX(), event.getY(), event.getX(), event.getY());
group.getChildren().add(currentLine);
}
}

private void handleMouseDragged(MouseEvent event) {
if (currentLine != null) {
currentLine.setEndX(event.getX());
currentLine.setEndY(event.getY());
}
}

private void handleMouseReleased(MouseEvent event) {
if (currentLine != null
&& currentLine.getStartX() == currentLine.getEndX()
&& currentLine.getStartY() == currentLine.getEndY()) {
// The line has no length, remove it
group.getChildren().remove(currentLine);
}
currentLine = null;
}

}





share|improve this answer























  • Thank you very much, been looking at your code and I feel more confident when it comes to event handlers and it also provided me with a solution, I really appreciate you spending your time helping me, thanks again :)
    – CorvaxEntity
    Nov 5 at 2:41















up vote
2
down vote



accepted







up vote
2
down vote



accepted






You only check if the lineButton is selected inside the MOUSE_CLICKED (which is a press-then-release gesture, by the way) handler. Inside this handler you add a MOUSE_PRESSED handler and inside that handler you add a MOUSE_DRAGGED handler. You don't check if the lineButton is selected inside the MOUSE_PRESSED or MOUSE_DRAGGED handlers.



What this all means is that, after the if condition inside the MOUSE_CLICKED handler evaluates to true, you'll have a MOUSE_PRESSED and MOUSE_DRAGGED handler that operate independently of your MOUSE_CLICKED handler. Now, whenever you press any mouse button it will create a Line and add it to the parent. Then the newly added MOUSE_DRAGGED handler will alter the Line.



You're fortunate, in a way, that you're using the onXXX properties instead of using addEventHandler. The properties replace the old EventHandler when set. If that didn't happen (such as with addEventHandler) you'd have many (one more each time) EventHandlers drawing Lines.



You just need to register all the appropriate EventHandlers once and do the logic inside of them.



Here's a small example:



import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.ToolBar;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Main extends Application {

private ToggleGroup toggleGroup;
private ToggleButton lineBtn;

private Group group;
private Line currentLine;

@Override
public void start(Stage primaryStage) {
toggleGroup = new ToggleGroup();
lineBtn = new ToggleButton("Line");

ToggleButton noneBtn = new ToggleButton("None");
toggleGroup.getToggles().addAll(lineBtn, noneBtn);
toggleGroup.selectToggle(noneBtn);

group = new Group();

BorderPane root = new BorderPane(new Pane(group), new ToolBar(lineBtn, noneBtn), null, null, null);
root.getCenter().setOnMousePressed(this::handleMousePressed);
root.getCenter().setOnMouseDragged(this::handleMouseDragged);
root.getCenter().setOnMouseReleased(this::handleMouseReleased);

primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.setTitle("Draw Shape Example");
primaryStage.show();
}

private void handleMousePressed(MouseEvent event) {
if (lineBtn.equals(toggleGroup.getSelectedToggle())
&& event.getButton() == MouseButton.PRIMARY) {
currentLine = new Line(event.getX(), event.getY(), event.getX(), event.getY());
group.getChildren().add(currentLine);
}
}

private void handleMouseDragged(MouseEvent event) {
if (currentLine != null) {
currentLine.setEndX(event.getX());
currentLine.setEndY(event.getY());
}
}

private void handleMouseReleased(MouseEvent event) {
if (currentLine != null
&& currentLine.getStartX() == currentLine.getEndX()
&& currentLine.getStartY() == currentLine.getEndY()) {
// The line has no length, remove it
group.getChildren().remove(currentLine);
}
currentLine = null;
}

}





share|improve this answer














You only check if the lineButton is selected inside the MOUSE_CLICKED (which is a press-then-release gesture, by the way) handler. Inside this handler you add a MOUSE_PRESSED handler and inside that handler you add a MOUSE_DRAGGED handler. You don't check if the lineButton is selected inside the MOUSE_PRESSED or MOUSE_DRAGGED handlers.



What this all means is that, after the if condition inside the MOUSE_CLICKED handler evaluates to true, you'll have a MOUSE_PRESSED and MOUSE_DRAGGED handler that operate independently of your MOUSE_CLICKED handler. Now, whenever you press any mouse button it will create a Line and add it to the parent. Then the newly added MOUSE_DRAGGED handler will alter the Line.



You're fortunate, in a way, that you're using the onXXX properties instead of using addEventHandler. The properties replace the old EventHandler when set. If that didn't happen (such as with addEventHandler) you'd have many (one more each time) EventHandlers drawing Lines.



You just need to register all the appropriate EventHandlers once and do the logic inside of them.



Here's a small example:



import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.ToolBar;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Main extends Application {

private ToggleGroup toggleGroup;
private ToggleButton lineBtn;

private Group group;
private Line currentLine;

@Override
public void start(Stage primaryStage) {
toggleGroup = new ToggleGroup();
lineBtn = new ToggleButton("Line");

ToggleButton noneBtn = new ToggleButton("None");
toggleGroup.getToggles().addAll(lineBtn, noneBtn);
toggleGroup.selectToggle(noneBtn);

group = new Group();

BorderPane root = new BorderPane(new Pane(group), new ToolBar(lineBtn, noneBtn), null, null, null);
root.getCenter().setOnMousePressed(this::handleMousePressed);
root.getCenter().setOnMouseDragged(this::handleMouseDragged);
root.getCenter().setOnMouseReleased(this::handleMouseReleased);

primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.setTitle("Draw Shape Example");
primaryStage.show();
}

private void handleMousePressed(MouseEvent event) {
if (lineBtn.equals(toggleGroup.getSelectedToggle())
&& event.getButton() == MouseButton.PRIMARY) {
currentLine = new Line(event.getX(), event.getY(), event.getX(), event.getY());
group.getChildren().add(currentLine);
}
}

private void handleMouseDragged(MouseEvent event) {
if (currentLine != null) {
currentLine.setEndX(event.getX());
currentLine.setEndY(event.getY());
}
}

private void handleMouseReleased(MouseEvent event) {
if (currentLine != null
&& currentLine.getStartX() == currentLine.getEndX()
&& currentLine.getStartY() == currentLine.getEndY()) {
// The line has no length, remove it
group.getChildren().remove(currentLine);
}
currentLine = null;
}

}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 5 at 2:43

























answered Nov 5 at 2:23









Slaw

5,0232728




5,0232728












  • Thank you very much, been looking at your code and I feel more confident when it comes to event handlers and it also provided me with a solution, I really appreciate you spending your time helping me, thanks again :)
    – CorvaxEntity
    Nov 5 at 2:41




















  • Thank you very much, been looking at your code and I feel more confident when it comes to event handlers and it also provided me with a solution, I really appreciate you spending your time helping me, thanks again :)
    – CorvaxEntity
    Nov 5 at 2:41


















Thank you very much, been looking at your code and I feel more confident when it comes to event handlers and it also provided me with a solution, I really appreciate you spending your time helping me, thanks again :)
– CorvaxEntity
Nov 5 at 2:41






Thank you very much, been looking at your code and I feel more confident when it comes to event handlers and it also provided me with a solution, I really appreciate you spending your time helping me, thanks again :)
– CorvaxEntity
Nov 5 at 2:41












CorvaxEntity is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















CorvaxEntity is a new contributor. Be nice, and check out our Code of Conduct.













CorvaxEntity is a new contributor. Be nice, and check out our Code of Conduct.












CorvaxEntity is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147393%2fjavafx-mouseevent-problems%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()