Why should I avoid using PropertyValueFactory in JavaFX?

TL;DR: You should avoid PropertyValueFactory and similar classes because they rely on reflection and, more importantly, cause you to lose helpful compile-time validations (such as if the property actually exists). Replace uses of PropertyValueFactory with lambda expressions. For example, replace: nameColumn.setCellValueFactory(new PropertyValueFactory<>(“name”)); With: nameColumn.setCellValueFactory(data -> data.getValue().nameProperty()); (assumes you’re using Java 8+ and you’ve defined the … Read more

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

Java 8 U40 TextFormatter (JavaFX) to restrict user input only for decimal number

Please see this example: DecimalFormat format = new DecimalFormat( “#.0” ); TextField field = new TextField(); field.setTextFormatter( new TextFormatter<>(c -> { if ( c.getControlNewText().isEmpty() ) { return c; } ParsePosition parsePosition = new ParsePosition( 0 ); Object object = format.parse( c.getControlNewText(), parsePosition ); if ( object == null || parsePosition.getIndex() < c.getControlNewText().length() ) { return … Read more

JavaFx, Problems with @FXML

Your problem is in import in Controller class: import java.awt.*; Should be: import javafx.scene.control.TextField; The programs tries to convert java.awt.TextField to javafx.scene.control.TextField I assume you’d like to use javafx, not AWT TextField Note: don’t trust the imports which IDE gives you 🙂

JavaFX ImageView without any smoothing

In JavaFX 2.2 ImageView is always going to do some smoothing regardless of the smooth hint you provide to the ImageView. (Based on testing using Java 7u15 and Windows 7 with an ATI HD4600 graphics card). Perhaps it is a bug that ImageView will always smooth the Image, but the documentation doesn’t really specify exactly … Read more

JavaFX-11 with VSCode

I’m going to run the HelloFX sample for Eclipse from the OpenJFX samples. After I open the sample with VSCode, I see the reported error: [Java] The import javafx cannot be resolved [268435846]. This obviously means that JavaFX classes are not resolved, and even if there is an entry in the .classpath file: <classpathentry kind=”con” … Read more

JavaFX correct scaling

I created a sample app to demonstrate one approach to performing scaling of a node in a viewport on a scroll event (e.g. scroll in and out by rolling the mouse wheel). The key logic to the sample for scaling a group placed within a StackPane: final double SCALE_DELTA = 1.1; final StackPane zoomPane = … Read more

Is @FXML needed for every declaration?

The @FXML annotation enables an FXMLLoader to inject values defined in an FXML file into references in the controller class. In other words, if you annotate your timerLabel with @FXML, then it will be initialized by the FXMLLoader when the load() method is called by an element in the FXML file with fx:id=”timerLabel”. As others … Read more