Customize ListView in JavaFX with FXML

I understand your question. There are mainly two ways to set items in a Listview: 1. Create the ObservableList and set the items of the ListView with the ObservableList (listView.setItems(observableList)). 2. Use the setCellFactory() method of the ListView class. You would prefer to use the setCellFactory() method, because this approach simplies the process as well … Read more

How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?

Short version of answer: Use getClass().getResource(…) or SomeOtherClass.class.getResource(…) to create a URL to the resource Pass either an absolute path (with a leading /) or a relative path (without a leading /) to the getResource(…) method. The path is the package containing the resource, with . replaced with /. Do not use .. in the … Read more

How to create a modal window in JavaFX 2.1

In my opinion this is not good solution, because parent window is all time active. For example if You want open window as modal after click button… private void clickShow(ActionEvent event) { Stage stage = new Stage(); Parent root = FXMLLoader.load( YourClassController.class.getResource(“YourClass.fxml”)); stage.setScene(new Scene(root)); stage.setTitle(“My modal window”); stage.initModality(Modality.WINDOW_MODAL); stage.initOwner( ((Node)event.getSource()).getScene().getWindow() ); stage.show(); } Now Your … Read more

ScrollPanes in JavaFX 8 always have gray background

I found the solution in this discussion: https://community.oracle.com/thread/3538169 First I needed this: .scroll-pane > .viewport { -fx-background-color: transparent; } Then I could set the background color to whatever I like. In this case, I’m making all ScrollPane backgrounds transparent: .scroll-pane { -fx-background-color: transparent; }

Loading new fxml in the same scene

Why your code does not work The loader creates a new AnchorPane, but you never add the new pane to a parent in the scene graph. Quick Fix Instead of: content = (AnchorPane) FXMLLoader.load(“vista2.fxml”); Write: content.getChildren().setAll(FXMLLoader.load(“vista2.fxml”)); Replacing the content children with your new vista. The content itself remains in the scene graph, so when you … Read more

Make portion of a text bold in a JavaFx Label or Text

It is possible to use TextFlow container from JavaFX8. Then you can easily add differently styled Text nodes inside it. TextFlow flow = new TextFlow(); Text text1=new Text(“Some Text”); text1.setStyle(“-fx-font-weight: bold”); Text text2=new Text(“Some Text”); text2.setStyle(“-fx-font-weight: regular”); flow.getChildren().addAll(text1, text2); TextFlow container will automatically wrap content Text nodes.

JavaFX8: How to create listener for selection of row in Tableview?

The selectedItem in the selection model is an observable property, so you should be able to achieve this with: tableview1.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> { if (newSelection != null) { tableview2.getSelectionModel().clearSelection(); } }); tableview2.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> { if (newSelection != null) { tableview1.getSelectionModel().clearSelection(); } });