How to get Treeview DisclosureNode to right , in Javafx? - javafx

In Javafx the DisclosureNode of Treeview is on left by default. How can I I place the Disclosure Node to Right (float right)..??
Any Help would be appreciated.

You only need to set the Node Orientation to get a right to left behaviour. But be aware of, that all childrens of this TreeView will inherit the orientation as a default.
import javafx.application.Application;
import javafx.geometry.NodeOrientation;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TreeViewRightLeft extends Application {
#Override
public void start(Stage primaryStage) {
TreeItem<String> item = new TreeItem<>("Root Node");
item.setExpanded(true);
item.getChildren().addAll(
new TreeItem<>("Item 1"),
new TreeItem<>("Item 2"),
new TreeItem<>("Item 3")
);
TreeView<String> treeView = new TreeView<>(item);
// Here you can select the orientation.
treeView.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
BorderPane root = new BorderPane(treeView);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("TreeView");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Right to Left
Left to Right (default)

Related

How to stop transition when checkbox is unchecked javafx

So I've made a checkbox that applies a scale transition to a rectangle when checked. But the problem is that the transition keeps going even after I uncheck the checkbox. Any ideas on how to make it stop after un-checking?
checkbox.setOnAction(e -> {
ScaleTransition scaleT = new ScaleTransition(Duration.seconds(5), rectangle);
scaleT.setAutoReverse(true);
scaleT.setCycleCount(Timeline.INDEFINITE);
scaleT.setToX(2);
scaleT.setToY(2);
scaleT.play();
});
To control the animation, you need to define the transistion(with INDEFINITE cycle count) outside the CheckBox listener/action. Then you can just play/pause the animation as you required.
Below is the quick demo:
import javafx.animation.ScaleTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
import javafx.util.Duration;
public class ScaleTransitionDemo extends Application {
#Override
public void start(Stage stage) {
Shape rectangle = new Rectangle(50, 50, Color.BLUE);
ScaleTransition transition = new ScaleTransition(Duration.seconds(1), rectangle);
transition.setDuration(Duration.seconds(1));
transition.setAutoReverse(true);
transition.setCycleCount(Timeline.INDEFINITE);
transition.setToX(3);
transition.setToY(3);
CheckBox checkBox = new CheckBox("Animate");
checkBox.selectedProperty().addListener((obs, old, selected) -> {
if (selected) {
transition.play();
} else {
transition.pause();
}
});
StackPane pane = new StackPane(rectangle);
VBox.setVgrow(pane, Priority.ALWAYS);
VBox root = new VBox(20, checkBox, pane);
root.setPadding(new Insets(10));
Scene scene = new Scene(root, 300, 300);
stage.setScene(scene);
stage.setTitle("Scale transition");
stage.show();
}
public static void main(String[] args) {
launch();
}
}
checking whether checkbox is selected or not with .isSelected() method . In this approach , scaled node will back to xy = 1 scale if checkbox is unchecked , but it will be disabled until transition ends .You can adjust setDuration . I've changed it just for gif recording. This is a single class javafx app you can try .
App.java
public class App extends Application {
#Override
public void start(Stage stage) {
Shape rectangle = new Rectangle(50, 50, Color.BLUE);
ScaleTransition scaleT = new ScaleTransition(Duration.seconds(1), rectangle);
CheckBox checkBox = new CheckBox("scale");
checkBox.setOnAction(e -> {
if (checkBox.isSelected()) {
scaleT.setDuration(Duration.seconds(1));
scaleT.setAutoReverse(true);
scaleT.setCycleCount(Timeline.INDEFINITE);
scaleT.setToX(2);
scaleT.setToY(2);
scaleT.play();
} else {
scaleT.setDuration(scaleT.getCurrentTime());
scaleT.stop();
scaleT.setCycleCount(1);
scaleT.setToX(1);
scaleT.setToY(1);
scaleT.play();
checkBox.setDisable(true);
scaleT.setOnFinished((t) -> {
checkBox.setDisable(false);
});
}
});
var scene = new Scene(new HBox(50, rectangle, checkBox), 640, 480);
stage.setScene(scene);
stage.setTitle("scale transition");
stage.show();
}
public static void main(String[] args) {
launch();
}
}

Align a Hbox to the bottom center inside a StakePanel

I'm learning Javafx layouts and I would like to obtain the following result:
HBox in the bottom center fo a Pane I have tried with the following code but I'm not able to do that
Can you please explain what I'm doing wrong? and How to achieve that?
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TestLayout extends Application {
#Override
public void start(Stage stage) {
initUI(stage);
}
private void initUI(Stage stage) {
Scene scene =null;
HBox hbox = new HBox();
hbox.setStyle("-fx-background-color: #FFFAAA;");
StackPane pane = new StackPane();
hbox.setStyle("-fx-background-color: #AAFAAA;");
hbox.prefWidthProperty().bind(pane.widthProperty().divide(4));
hbox.prefHeightProperty().bind(pane.heightProperty().divide(10));
pane.getChildren().add(hbox);
scene = new Scene(pane, 600, 600);
pane.prefWidthProperty().bind(scene.widthProperty());
pane.prefHeightProperty().bind(scene.heightProperty());
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class AppLauncherTest {
public static void main(String[] args) {
TestLayout.main(args);
}
}
I would Use a VBox or BorderPane as the root node. In this example, I use a VBox. I am assuming more nodes will go into this so I used a StackPane as the top node. This may need to be changed out or some other Pane may need to be added to this. That depends on what you are trying to do with the end product. For the bottom, I used an HBox and I set it's Margins.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Priority;
import javafx.geometry.Insets;
/**
* JavaFX App
*/
public class App extends Application {
#Override
public void start(Stage stage) {
StackPane subRootTop = new StackPane();
VBox.setVgrow(subRootTop, Priority.ALWAYS);
//subRootTop.setStyle("-fx-background-color: yellow;");
HBox subRootBotton = new HBox();
VBox.setVgrow(subRootBotton, Priority.ALWAYS);
subRootBotton.setStyle("-fx-background-color: green;");
subRootBotton.setMaxHeight(150);
VBox.setMargin(subRootBotton, new Insets(40, 40, 40, 40));
VBox root = new VBox(subRootTop, subRootBotton);
root.setStyle("-fx-background-color: red;");
Scene scene = new Scene(root, 600, 600);;
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
The binding of the StackPane is not size shouldn't be done. The scene automatically resizes its root to fill the whole area available. Assuming you want relative sizes, using StackPane is not really a good choice.
If you want the child to have a fixed distance to left right and bottom of the StackPane, you can do so specifying margins and alignment. Make sure the child does not grow to fit the parent though by setting the maxHeight to use the preferred height:
private void initUI(Stage stage) {
HBox hbox = new HBox();
hbox.setStyle("-fx-background-color: #FFFAAA;");
hbox.setMaxHeight(Region.USE_PREF_SIZE);
// could be calculated based on children instead of assigning
// an absolute value
hbox.setPrefHeight(30);
StackPane.setAlignment(hbox, Pos.BOTTOM_CENTER);
StackPane.setMargin(hbox, new Insets(20));
StackPane pane = new StackPane(hbox);
pane.setStyle("-fx-background-color: #AAFAAA;");
Scene scene = new Scene(pane, 600, 600);
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
Maybe you can use an AnchorPane as a base layout instead of the StacKPane. I made a small exmaple how it would look like. Of cause you could keep the StackPane as the base layout and just put the AnchorPane with the HBox onto it, but you should definitly use an AnchorPane for your plan.
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
private void initUI(Stage stage) {
// Create an anchor pane as base layout and set a color:
AnchorPane root = new AnchorPane();
root.setStyle("-fx-background-color: #FFFAAA;");
// Create a second container and set a minimum height and a color;
HBox hbox = new HBox();
hbox.setMinHeight(100d);
hbox.setStyle("-fx-background-color: #AAFAAA;");
// Give the child container a fixed location:
AnchorPane.setBottomAnchor(hbox, 50d);
AnchorPane.setLeftAnchor(hbox, 75d);
AnchorPane.setRightAnchor(hbox, 75d);
// Add the horizontal box to the base anchor pane:
root.getChildren().add(hbox);
stage.setScene(new Scene(root, 600, 600));
stage.show();
}
#Override
public void start(Stage stage) throws Exception {
initUI(stage);
}
public static void main(String[] args) {
launch(args);
}
}
Preview:

how to get KeyEvent handled in javafx

I have tests about KeyEvent in javafx,if I use onKeyPressed() method bound to any kind of pane,it wouldn't work.bound to scene or a button would work fine.I am wondering how can I let pane associated with KeyEvent.
To add key events handlers to pane effectively you need to request focus on pane first.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class PanesKeyEventsApp extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
StackPane stackPane1 = new StackPane();
stackPane1.setPrefSize(200, 200);
stackPane1.setStyle("-fx-background-color: purple;");
StackPane stackPane2 = new StackPane();
stackPane2.setPrefSize(200, 200);
stackPane2.setStyle("-fx-background-color: yellow;");
HBox hBox = new HBox(stackPane1, stackPane2);
Scene scene = new Scene(hBox);
stage.setScene(scene);
stage.show();
stackPane1.setOnMouseClicked(event -> stackPane1.requestFocus());
stackPane2.setOnMouseClicked(event -> stackPane2.requestFocus());
stackPane1.addEventHandler(KeyEvent.KEY_PRESSED, event -> System.out.println("purple key pressed " + event.getCode()));
stackPane2.addEventHandler(KeyEvent.KEY_PRESSED, event -> System.out.println("yellow key pressed " + event.getCode()));
}
}

JavaFX How to 'hide' TitledPane back after expanding

I have TitledPane, which i want to hide back (after expanding - un-expand it) after pressing a button. Is there any way to do it? I didn't find any way :( Thanks!
Just do
titledPane.setExpanded(false);
Complete example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TitledPaneExample extends Application {
#Override
public void start(Stage primaryStage) {
Label label = new Label("Some content");
Button button = new Button("OK");
VBox content = new VBox(10, label, button);
TitledPane titledPane = new TitledPane("Titled Pane", content);
button.setOnAction(e -> titledPane.setExpanded(false));
VBox root = new VBox(titledPane);
Scene scene = new Scene(root, 250, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Label text position

I have a Label with an image and text
final Label label = new Label(labelText);
label.setTextAlignment(TextAlignment.CENTER);
ImageView livePerformIcon = new ImageView(MainApp.class.getResource("/images/Folder-icon.png").toExternalForm());
label.setGraphic(livePerformIcon);
I get this as a visual result:
How I can change the text position? I want to set the text below the Image?
label.setContentDisplay(ContentDisplay.TOP);
Play with this to see the effect of the different alignment settings:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
public class LabelGraphicAlignmentTest extends Application {
#Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
Label label = new Label("Some\ntext");
label.setGraphic(new ImageView(getClass().getResource("/images/Folder-icon.png").toExternalForm()));
label.setMaxWidth(Double.POSITIVE_INFINITY);
label.setMaxHeight(Double.POSITIVE_INFINITY);
label.setStyle("-fx-border-color: blue;");
root.setCenter(label);
ComboBox<ContentDisplay> contentDisplayBox = new ComboBox<>();
contentDisplayBox.getItems().addAll(ContentDisplay.values());
contentDisplayBox.getSelectionModel().select(ContentDisplay.LEFT);
label.contentDisplayProperty().bind(contentDisplayBox.valueProperty());
ComboBox<Pos> alignmentBox = new ComboBox<>();
alignmentBox.getItems().addAll(Pos.values());
alignmentBox.getSelectionModel().select(Pos.CENTER);
label.alignmentProperty().bind(alignmentBox.valueProperty());
ComboBox<TextAlignment> textAlignmentBox = new ComboBox<>();
textAlignmentBox.getItems().addAll(TextAlignment.values());
textAlignmentBox.getSelectionModel().select(TextAlignment.LEFT);
label.textAlignmentProperty().bind(textAlignmentBox.valueProperty());
GridPane ctrls = new GridPane();
ctrls.setHgap(5);
ctrls.setVgap(5);
ctrls.setPadding(new Insets(10));
ctrls.addRow(0, new Label("Content display:"), new Label("Alignment:"), new Label("Text Alignment:"));
ctrls.addRow(1, contentDisplayBox, alignmentBox, textAlignmentBox);
root.setTop(ctrls);
Scene scene = new Scene(root, 600, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I had to center the text of a label which acted like a title. The following code snippet did the trick.
final Label title = new Label("Some text");
title.setMaxWidth(Double.MAX_VALUE);
title.setAlignment(Pos.CENTER);
Good programming :-)
I was able to grab the button from the fxml file:
#FXML Label countDownClockLabel;
Then when the controller was initialized i set the text position:
#FXML
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
countDownClockLabel.setAlignment(Pos.CENTER);
}
You have to import:
import javafx.geometry.Pos;

Resources