How do I add margin to a JavaFX element using CSS?

Probably really late to the party, but I use another approach which might be helpful for others too. There’s no -fx-margin: 5px; CSS property for JavaFX buttons, but you can workaround the behaviour with a combination of -fx-padding, -fx-border-insets and -fx-background-insets. For example a button with a 5px margin. .button-with-margin { -fx-padding: 5px; -fx-border-insets: 5px; … Read more

Auto fit size column in table view

After 3 years I come back to this problem again, some suggestions are calculating the size of text of data in each cell (it’s complicated depending on font size, font family, padding…) But I realize that when I click on the divider on table header, it’s resized fit to content as I want. So I … 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

JavaFX 2.1: Toolkit not initialized

Found a solution. If I just create a JFXPanel from Swing EDT before invoking JavaFX Platform.runLater it works. I don’t know how reliable this solution is, I might choose JFXPanel and JFrame if turns out to be unstable. public class BootJavaFX { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() … Read more