JavaFX hover property - java

I have a GridPane which is populated by rectangles. What I want to do is to display a new pane while hovering over a particular rectangle(member of the grid pane). Lets have an example with this VBox code example below. How could I make it display on hover ?
Rectangle r = new Rectangle(RECTANGLE_SIZE, RECTANGLE_SIZE);
r.hoverProperty().addListener((observable) -> {
r.setFill(Color.BLACK);
VBox box = new VBox();
Button x = new Button("Test");
box.getChildren().add(x);
});
the set fill works properly

In your example you need to specify Node which would be parent of VBox. For now you are always creating new VBox, but never add it to current scene graph. Try this:
r.hoverProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean show) -> {
if (show) {
VBox box = new VBox();
Button x = new Button("Test");
box.getChildren().add(x);
parent.getChildren().add(box);
} else {
parent.getChildren().clear();
}
});

Related

How to set an action to occur on pressing of any keyboard key in JavaFX?

So I want to do a title Menu for a video game project for college. I want to display a message press any key to continue... then when the user pressed any key, including the mouse, a method would run to set the stage to the next menu.
I posted all the relevant code bellow but short version is:
BackgroundImangeDisplayPane extends display pane and adds a background image.
TitleCard extends BackgroundImangeDisplayPane adds a VBox and 2 labels to the VBox
I'm using public void start(Stage primaryStage) throws Exception as the main, I set the setOnActionxxx methods here
I have tried using the set on action method on root and Vbox and non of them work... when I click nothing happens... But when I resize the window The root.setOnActionXXX "activates".
If I write the setOnAction methods on the TitleCard class It kind of works but then I cant switch the stage.
I will post the code bellow as well an explanation of the Scene structure its not to complicated:
// this will be the borderpane for every scene it recives a backgund
//images that will be present in every menu
public BackgroundImangeDisplayPane() {
try {
stream = new FileInputStream(imagePath.toString());
Image image = new Image(stream);
ImageView imageView = new ImageView();
imageView.setImage(image);
imageView.setFitWidth(1920);
imageView.setPreserveRatio(true);
this.getChildren().add(imageView);
BackgroundSize backgoundSize = new BackgroundSize(AUTO, AUTO, true, true, true, true);
BackgroundImage backgroundImage = new BackgroundImage(image, NO_REPEAT, NO_REPEAT, CENTER, backgoundSize);
Background background = new Background(backgroundImage);
this.setBackground(background);
} catch (Exception e) {
}
}
//This extends `BackgroundImangeDisplayPane` and places on top of it a A Vbox with two lables: the title and "press any key to continue..."
// it then adds styles to the labels
public class TitleCard extends BackgroundImangeDisplayPane {
Label title = new Label("Boats & Docks"); // lable 1
Label subtitle = new Label("Press any key to continue ..."); label2
public TitleCard(){
super();
VBox vbox = new VBox();
vbox.getChildren().add(title);
vbox.getChildren().add(subtitle);
this.setCenter(vbox);
this.setAlignment(vbox, Pos.CENTER);
vbox.setAlignment(Pos.CENTER);
title.setFont(new Font(170)); // set to Label
title.setTextFill(Color.SNOW);
title.setEffect(new DropShadow());
subtitle.setFont( new Font (30));
}
}
...
//Works as the "main" in javaFX
private Stage primaryStage;
#Override
public void start(Stage primaryStage) throws Exception {
TitleCard root = new TitleCard();
/*BasicMenu menu = new BasicMenu(5);
menu.ButtonSetOnAction(0, e -> changeScene() );
BackgroundImangeWithCustomMenu background = new
BackgroundImangeWithCustomMenu(menu,50,50);
root.setCenter(background);*/
Button b = new Button();
b.setOnAction(e -> changeSceneToLoginMenu());
System.out.println(root.getChildren().get(1).getClass());
root.getChildren().get(1).setFocusTraversable(true);
root.getChildren().get(1).setOnMouseClicked(e -> changeSceneToLoginMenu());
root.getChildren().get(1).setOnKeyPressed(e -> changeSceneToLoginMenu());
root.getChildren().get(0).setOnMouseClicked(e -> changeSceneToLoginMenu());
root.getChildren().get(0).setOnKeyPressed(e -> changeSceneToLoginMenu());
/*
root.setOnMouseClicked(e -> changeSceneToLoginMenu());
root.setOnKeyReleased(e -> changeSceneToLoginMenu());
root.setOnKeyPressed(e -> changeSceneToLoginMenu());
*/
Scene scene = new Scene(root, 1280, 720);
primaryStage.setScene(scene);
primaryStage.show();
this.primaryStage = primaryStage;
}
As proposed by James in comments, when a key is pressed on the scene, navigate to the next scene (or replace the root in the current scene, and remove the key press handler).
scene.setOnKeyPressed(e -> navigateToNextScene());
I managed to find a very simple working solution but I don't fully undestand why it does work. I Noticed that if I set the handler in the same class the node was instanciated the handler would work fine But if I tried to get the node with a method to the main fuction via root.getChildren().get(1) and then cast it to the VBox element the handler would not work.
As a solution I made the VBox a field and wrote a setter method for the VBox event Handler in the TitleCard Class. This fixed the problem.
I marked the code added as solution code with comments
public class TitleCard extends BackgroundImangeDisplayPane {
Label title = new Label("Boats & Docks"); // lable 1
Label subtitle = new Label("Press any key to continue ..."); label2
VBox vbox = new VBox; // solution code
public TitleCard(){
super();
VBox vbox = new VBox();
vbox.getChildren().add(title);
vbox.getChildren().add(subtitle);
this.setCenter(vbox);
this.setAlignment(vbox, Pos.CENTER);
vbox.setAlignment(Pos.CENTER);
title.setFont(new Font(170)); // set to Label
title.setTextFill(Color.SNOW);
title.setEffect(new DropShadow());
subtitle.setFont( new Font (30));
}
// Solution Code
public void setVBoxHandler(EventHandler<? super MouseEvent> value){
vbox.setOnMouseClicked(value);
}
}
Then I set the handler in the start method:
public void start(Stage primaryStage) throws Exception {
TitleCard root = new TitleCard();
VBox vBox =(VBox) root.getChildren().get(1);
root.setVBoxHandler(e->changeSceneToLoginMenu() ); // solution Code
Scene scene = new Scene(root, 1280, 720);
primaryStage.setScene(scene);
primaryStage.show();
this.primaryStage = primaryStage;
}
public void changeSceneToLoginMenu() {
System.out.println("It finally worked");
Scene currentScene = new Scene(new Group(),100,100); // just a demo
primaryStage.setScene(currentScene);
}
notes: The type of value on the method setVBoxHandler(EventHandler<? super MouseEvent> value) will depend on the setOnXXX method used. For example I tested and this soultion also works for buttons just need to change the type to EventHandler<ActionEvent> value.
Some coments on the question posted links on "how to use handlers", this posts used anomimous classes. I belived This way is outdated. I used lambdas in the code the end result is the same but more redable code
Just for reference if future readers are using anomimous classes the solution would be the same just change the way you set up the handler:
// lambdas
setVBoxHandler( e -> System.out.println("Code run if mouse is clicked "));
// anomimous classes
setVBoxHandler(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event){
System.out.println("Code run if mouse is clicked ");
}
});

How can I add a Node to an HBox inside of a Tab JavaFX?

I have a GUI that I've created, and I would like to add a ProgressIndicatorwhen the application is doing something in the background. I've created a Tab in the constructor similar to the following:
public class myGUI {
Tab myTab;
myGUI() {
myTab = new Tab("My Tab");
HBox view = new HBox();
VBox left = new VBox();
BorderPane right = new BorderPane();
/*A lot of other things are declared that go in left and right*/
view.getChildren().addAll(left, right);
myTab.setContent(view);
}
...
Later on, I have a button press that starts the application doing a background task, and I would like to add a ProgressIndicator to the center of the BorderPane. I tried something like the following:
private void handleMyAction(MouseEvent e) {
myTab.getContent().getChildren().get(1).setCenter(new ProgressIndicator(-1.0f));
}
I would think that this works, however, getContent returns a Node, and I cannot call getChildren on that Node. How can I access the BorderPane to add another Node without making the BorderPane a field in my class?
Just make the border pane an instance variable:
public class MyGUI {
private Tab myTab;
private BorderPane right ;
MyGUI() {
myTab = new Tab("My Tab");
HBox view = new HBox();
VBox left = new VBox();
right = new BorderPane();
/*A lot of other things are declared that go in left and right*/
view.getChildren().addAll(left, right);
myTab.setContent(view);
}
private void handleMyAction(MouseEvent e) {
right.setCenter(new ProgressIndicator(-1.0f));
}
}

JavaFX: How to make the underlaying Node accessable?

I have a Pane and a VBox inside of a StackPane. I added the Pane first and the VBox on top of it. The VBox includes several kes that in turn have Buttons as children. I use the normal Pane as a "canvas" to position Lines on it. The Lines as well as the Buttons need to be interactive. So by clicking on them they shall for example change their color.
But at the moment the Pane and its Line objects are shown but they are covered by the VBox so I can not interact with them but only with the Buttons.
How can I provide that I can interact with the Line as well, though they are in a lower layer of the StackPane?
You can set the pickOnBoundsProperty of your container Nodes (VBox and HBox) to false.
Defines how the picking computation is done for this node when
triggered by a MouseEvent or a contains function call. If pickOnBounds
is true, then picking is computed by intersecting with the bounds of
this node, else picking is computed by intersecting with the geometric
shape of this node.
As result, "transparent" areas of the HBox and the VBox will not register the click event.
Example:
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Pane pane = new Pane();
pane.setStyle("-fx-background-color:red;");
StackPane sp = new StackPane();
VBox vbox = new VBox();
HBox hbox = new HBox();
hbox.setSpacing(30);
for (int i = 0; i< 5; i++) {
Button b = new Button("Button");
b.setOnAction(e -> System.out.println("Button clicked"));
hbox.getChildren().add(b);
}
vbox.getChildren().add(hbox);
sp.getChildren().addAll(pane, vbox);
Line line = new Line(10, 10, 500, 10);
line.setStrokeWidth(3);
pane.getChildren().add(line);
line.setOnMouseClicked(e -> {
System.out.println("Line Clicked!");
});
// Set pickOnBounds to vbox and hbox
vbox.setPickOnBounds(false);
hbox.setPickOnBounds(false);
root.setCenter(sp);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
They are covered cause the VBOX is in front of the Pane.
First Way:
You can setVisible(false) the VBox so the Pane can be accessible and then setVisible(true) the VBox again.
Second Way:
You can use methods called toBack(); and toFront(); and bring a Node back or front to the hierarchy:
vBox.toBack(); //the the vBox goes back to the hierarchy,it is like zOrder in html
and then use:
vBox.toFront(); //to bring the vBox again in front.
Finally:
You can somewhere provide a ToggleButton that when is pressed the VBox is appearing and when is not pressed the VBox is disappeared.

Get a child node to be displayed inside an Anchor Pane in Java FX?

Just for testing I want a Button which is a Node so far to be inside a TitledPane (I know it contains an AnchorPane).
This is the general code I'm trying for the method:
#FXML private void alterarTitledPane(MouseEvent event) {
if (event.isShiftDown()) {
// Here is where I code for adding a button inside TitledPane
}
if (!titledPane.isExpanded()) {
titledPane.setExpanded(true);
titledPane.setText("Expandido");
}
else {
titledPane.setExpanded(false);
titledPane.setText("No expandido");
}
}
These are the two alternatives I've tried for achieving it:
Alternative 1
((AnchorPane)titledPane.getContent()).getChildren().add(button2);
button2.toFront();
button2.setVisible(true);
Alternative 2
Group a = new Group(button2);
((AnchorPane)titledPane.getContent()).getChildren().add(a);
button2.toFront();
button2.setVisible(true);
Got not results so far...
for add node in pane first of all you have to initalize it...
try this....its work.
AnchorPane anch = new AnchorPane();
Button btn = new Button("anshul");
anch.getChildren().add(btn);
Anchorpane pane=new Anchorpane();
Label lbl_user=new Label("Username");
Textfield txt=new Textfield();
AnchorPane.setTopAnchor(lbl_user, 10.0);
AnchorPane.setRightAnchor(lbl_user 10.0);
AnchorPane.setTopAnchor(txt, 40.0);
AnchorPane.setRightAnchor(txt,10.0);
panegetChildren().addAll(lbl_user,txt);

Expanding Accordion with new Titled Pane

I want to refill an Accordion with a new collection of TitledPane, but I've found a problem expanding nodes in the accordion.
I guess that it's an issue of the JavaFX library. Because it also happen with the sample provided in the manual for Accordion and TitledPane from Oracle. I take the original Example 22-5, and change it like this:
public void start(Stage stage) {
stage.setTitle("TitledPane");
Scene scene = new Scene(new Group(), 800, 250);
scene.setFill(Color.GHOSTWHITE);
final Label label = new Label("Initial");
// --- Accordion
final Accordion accordion = new Accordion ();
for (int i = 0; i < imageNames.length; i++) {
images[i] = new
Image(getClass().getResourceAsStream(imageNames[i] + ".jpg"));
pics[i] = new ImageView(images[i]);
tps[i] = new TitledPane(imageNames[i],pics[i]);
}
accordion.getPanes().addAll(tps);
accordion.expandedPaneProperty().addListener(new
ChangeListener<TitledPane>() {
public void changed(ObservableValue<? extends TitledPane> ov,
TitledPane old_val, TitledPane new_val) {
if (new_val != null) {
label.setText("expanded: " +accordion.getExpandedPane().getText());
}
}
});
HBox hbox = new HBox(10);
Button buttonClear = new Button("Clear and refill");
buttonClear.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
for (int i = 0; i < imageNames.length; i++) {
tps[i] = new TitledPane(imageNames[i]+" refill",pics[i]);
}
accordion.getPanes().clear();
accordion.getPanes().addAll(tps);
accordion.getPanes().get(0).setExpanded(true);
}
});
hbox.setPadding(new Insets(20, 0, 0, 20));
hbox.getChildren().setAll(label, accordion, buttonClear);
Group root = (Group)scene.getRoot();
root.getChildren().add(hbox);
stage.setScene(scene);
stage.show();
}
When I change the expanded TitledPane, the label takes its value. And I also added a button, that clear all the TitledPane in the Accordion, create a new ones and set the first TitledPane as Expanded.
So when I press the button, the accordion it's refilled, the label says that the First TitledPane it's expanded but the last one it's the one who seems selected, but without the image. This occurs the second time we press this button, or if we previously select one of the TitledPane.
And this thing it's precisely what I want to do. Clear all the TitledPane and refill it with new TitledPane, and it's giving the same weird result.
How can I clean all the TitledPane, create a new ones and set as expanded the first TitledPane without this side effects that I explain before?
Thanks for reading!

Categories