I am very new JavaFX, started learning it yesterday. Spent the whole day reading through the documentation, but learned nothing...
Here is what I want to do, make a simple JavaFX application that creates a circle. On click its stroke turns orange (some color). On unlicked (click on anything other than that circle), the stroke turns (some color) white.
Here is my pseudo code what I have so far. I want to make a separate class that creates a circle and handle the events (important).
public class Something extends Application {
@Override
public void start(Stage primaryStage) {
MyCircle c1 = new MyCircle();
c1.setCircle(20, 0, 0);
TilePane root = new TilePane();
root.getChildren().add(c1.getCircle());
//Some kind of mouse event listener, not sure which one should be
c1.getCircle().addEventListener(); //pseudo code
Scene scene = new Scene(root, 400, 400);
primaryStage.setTitle("Circle");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This the class that should create a circle and handle all the events, such as, mouse click, mouse location, click and drag and stuff like that.
public class MyCircle implements EventHandler{
Circle circle = new Circle();
public void setCircle(int radius, int x, int y){
circle.setRadius(radius);
position(x,y);
circle.setStrokeWidth(3);
circle.setStroke(Color.valueOf("white"));
}
public Circle getCircle(){
return circle;
}
public void position(int x, int y){
circle.setTranslateX(x);
circle.setTranslateY(y);
}
public void selected(){
circle.setStroke(Color.valueOf("orange"));
}
public void unselected() {
circle.setStroke(Color.valueOf("white"));
}
@Override
public void handle(Event event) {
if (event == MOUSE_CLICKED){ //pseudo code
selected();
}
else if(event == MOUSE_UNCLICKED){ //pseudo code
unselected();
}
}
}
Since I am very new to JavaFX, I'd much appreciate explanation as well. Thanks!
EDIT: This is my pseudo code, and I want to convert it into an actual working code. I am not sure how would I do that. Any help would be appreciated.
ANOTHER EDIT: Everything is a code except 3 marked places. Please look for my comment Psuedo Code
within the code, where I need help to change the pseudo code into an actual code.
Handling selection status of a Node requires some knowledge not available within the Node. You'll going to need to know whether the mouse event happened somewhere else (e.g. other Nodes or the root Pane), so you may have to pass questionable arguments to it.
In general, it's not a good idea to delegate mouse event handling to MyCircle
. Instead it's better to specify the selection behavior in that class and delegate selection handling to a separate helper class which has enough knowledge to handle the problem. I created this gist to show how this task can be done.
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);
}
}
I borrowed and modified an interface from jfxtras-labs to represent a selectable Node. It's good to have this interface to differentiate between selectable nodes and unselectable ones:
/**
* 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);
}
classes who wish to be selectable must implement this interface and specify their selection behavior in implementation of notifySelection method:
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);
}
}
And finally the selection handler class:
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);
}
}
}
}