Last active
November 2, 2022 11:42
-
-
Save omidzk/3ceeebf948a210def5ca6fb5f39e14ff to your computer and use it in GitHub Desktop.
Demo of selection of Nodes by mouse click in JavaFx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.scene.paint.Color; | |
import javafx.scene.shape.Circle; | |
public class MyCircle extends Circle implements SelectableNode { | |
public MyCircle(double centerX, double centerY, double radius) { | |
super(centerX, centerY, radius); | |
} | |
@Override | |
public boolean requestSelection(boolean select) { | |
return true; | |
} | |
@Override | |
public void notifySelection(boolean select) { | |
if(select) | |
this.setFill(Color.RED); | |
else | |
this.setFill(Color.BLACK); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This interface is based on jfxtras-labs <a href="https://github.com/JFXtras/jfxtras-labs/blob/8.0/src/main/java/jfxtras/labs/scene/control/window/SelectableNode.java">SelectableNode</a> | |
*/ | |
public interface SelectableNode { | |
public boolean requestSelection(boolean select); | |
public void notifySelection(boolean select); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.BorderPane; | |
import javafx.stage.Stage; | |
public class SelectionDemo extends Application { | |
@Override | |
public void start(Stage primaryStage) { | |
Scene scene = new Scene(createPane(), 400, 400); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
private Parent createPane() { | |
BorderPane root = new BorderPane(); | |
SelectionHandler selectionHandler = new SelectionHandler(root); | |
root.addEventHandler(MouseEvent.MOUSE_PRESSED, selectionHandler.getMousePressedEventHandler()); | |
MyCircle c1 = new MyCircle(40, 40, 20); | |
MyCircle c2 = new MyCircle(40, 100, 20); | |
MyCircle c3 = new MyCircle(40, 160, 20); | |
root.getChildren().addAll(c1, c2, c3); | |
return root; | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.event.EventHandler; | |
import javafx.scene.Node; | |
import javafx.scene.Parent; | |
import javafx.scene.input.MouseEvent; | |
public class SelectionHandler { | |
private Clipboard clipboard; | |
private EventHandler<MouseEvent> mousePressedEventHandler; | |
public SelectionHandler(final Parent root) { | |
this.clipboard = new Clipboard(); | |
this.mousePressedEventHandler = new EventHandler<MouseEvent>() { | |
@Override | |
public void handle(MouseEvent event) { | |
SelectionHandler.this.doOnMousePressed(root, event); | |
event.consume(); | |
} | |
}; | |
} | |
public EventHandler<MouseEvent> getMousePressedEventHandler() { | |
return mousePressedEventHandler; | |
} | |
private void doOnMousePressed(Parent root, MouseEvent event) { | |
Node target = (Node) event.getTarget(); | |
if(target.equals(root)) | |
clipboard.unselectAll(); | |
if(root.getChildrenUnmodifiable().contains(target) && target instanceof SelectableNode) { | |
SelectableNode selectableTarget = (SelectableNode) target; | |
if(!clipboard.getSelectedItems().contains(selectableTarget)) | |
clipboard.unselectAll(); | |
clipboard.select(selectableTarget, true); | |
} | |
} | |
/** | |
* This class is based on jfxtras-labs | |
* <a href="https://github.com/JFXtras/jfxtras-labs/blob/8.0/src/main/java/jfxtras/labs/scene/control/window/Clipboard.java">Clipboard</a> | |
* and | |
* <a href="https://github.com/JFXtras/jfxtras-labs/blob/8.0/src/main/java/jfxtras/labs/util/WindowUtil.java">WindowUtil</a> | |
*/ | |
private class Clipboard { | |
private ObservableList<SelectableNode> selectedItems = FXCollections.observableArrayList(); | |
public ObservableList<SelectableNode> getSelectedItems() { | |
return selectedItems; | |
} | |
public boolean select(SelectableNode n, boolean selected) { | |
if(n.requestSelection(selected)) { | |
if (selected) { | |
selectedItems.add(n); | |
} else { | |
selectedItems.remove(n); | |
} | |
n.notifySelection(selected); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public void unselectAll() { | |
List<SelectableNode> unselectList = new ArrayList<>(); | |
unselectList.addAll(selectedItems); | |
for (SelectableNode sN : unselectList) { | |
select(sN, false); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment