JavaFX How to set scene background image

One of the approaches may be like this: 1) Create a CSS file with name “style.css” and define an id selector in it: #pane{ -fx-background-image: url(“background_image.jpg”); -fx-background-repeat: stretch; -fx-background-size: 900 506; -fx-background-position: center center; -fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0); } 2) Set the id of the most top control (or any control) in … Read more

JavaFX Panel inside Panel auto resizing

After hours of searching and testing finally got it just after posting the question! You can use the “AnchorPane.topAnchor, AnchorPane.bottomAnchor, AnchorPane.leftAnchor, AnchorPane.rightAnchor” fxml commands with the value “0.0” to fit/stretch/align the child elements inside a AnchorPane. So, these commands tell to child element to follow its parent while resizing. My updated code Main.fxml <?xml version=”1.0″ … Read more

JavaFX 2 Automatic Column Width

If your total number of columns are pre-known. You can distribute the column widths among the tableview’s width: nameCol.prefWidthProperty().bind(personTable.widthProperty().divide(4)); // w * 1/4 surnameCol.prefWidthProperty().bind(personTable.widthProperty().divide(2)); // w * 1/2 emailCol.prefWidthProperty().bind(personTable.widthProperty().divide(4)); // w * 1/4 In this code, the width proportions of columns are kept in sync when the tableview is resized, so you don’t need to … Read more

JavaFX open new window

If you just want a button to open up a new window, then something like this works: btnOpenNewWindow.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { Parent root; try { root = FXMLLoader.load(getClass().getClassLoader().getResource(“path/to/other/view.fxml”), resources); Stage stage = new Stage(); stage.setTitle(“My New Stage Title”); stage.setScene(new Scene(root, 450, 450)); stage.show(); // Hide this current window (if this is … Read more

AutoComplete ComboBox in JavaFX

First, you’ll have to create this class in your project: import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.EventHandler; import javafx.scene.control.ComboBox; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; public class FxUtilTest { public interface AutoCompleteComparator<T> { boolean matches(String typedText, T objectToCompare); } public static<T> void autoCompleteComboBoxPlus(ComboBox<T> comboBox, AutoCompleteComparator<T> comparatorMethod) { ObservableList<T> data = comboBox.getItems(); comboBox.setEditable(true); comboBox.getEditor().focusedProperty().addListener(observable -> { if (comboBox.getSelectionModel().getSelectedIndex() < … Read more

JavaFX TabPane: How to set the selected tab

The SelectionModelis the right approach. You can get the default from your TabPane or assign your own implementation by using setSelectionModel(…). The default model should be good enough for the beginning. SingleSelectionModel<Tab> selectionModel = tabPane.getSelectionModel(); Once you stored it in some local variable, you have different options to select a tab. selectionModel.select(tab); //select by object … Read more

How perform task on JavaFX TextField at onfocus and outfocus?

I thought it might be helpful to see an example which specifies the ChangeListener as an anonymous inner class like scottb mentioned. TextField yourTextField = new TextField(); yourTextField.focusedProperty().addListener(new ChangeListener<Boolean>() { @Override public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue) { if (newPropertyValue) { System.out.println(“Textfield on focus”); } else { System.out.println(“Textfield out focus”); } … Read more

Javafx 2 click and double click

Yes you can detect single, double even multiple clicks: myNode.setOnMouseClicked(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent mouseEvent) { if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){ if(mouseEvent.getClickCount() == 2){ System.out.println(“Double clicked”); } } } }); MouseButton.PRIMARY is used to determine if the left (commonly) mouse button is triggered the event. Read the api of getClickCount() to conclude that there maybe multiple click … Read more

Why am I getting java.lang.IllegalStateException “Not on FX application thread” on JavaFX?

The user interface cannot be directly updated from a non-application thread. Instead, use Platform.runLater(), with the logic inside the Runnable object. For example: Platform.runLater(new Runnable() { @Override public void run() { // Update UI here. } }); As a lambda expression: // Avoid throwing IllegalStateException by running from a non-JavaFX thread. Platform.runLater( () -> { … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)