Java Reference
In-Depth Information
rectangle.strokeProperty().bind(model.strokeProperty());
final VBox vBox = new VBox(rectangle);
final Scene scene = new Scene(vBox);
canvas.setScene(scene);
}
});
The JFXPanel constructor bootstraps the JavaFX runtime system. We set the preferred size to the JFXPanel for it
to be laid out correctly in Swing containers. We then constructed the scene graph on the JavaFX application thread
and bound it to the model, which we changed into a JavaFX bean. Another set of changes that need to be made are
in the ActionListener s of the two JButton s. Modifying the model triggers a change to the JavaFX rectangle, so the
following code needs to be run on the JavaFX application thread:
this.view.changeFillButton.addActionListener(e -> {
Platform.runLater(() -> {
final Paint fillPaint = model.getFill();
if (fillPaint.equals(Color.LIGHTGRAY)) {
model.setFill(Color.GRAY);
} else {
model.setFill(Color.LIGHTGRAY);
}
});
});
The completed Swing JavaFX hybrid program is shown in Listing 7-12.
Listing 7-12. JavaFXSceneInSwingExample.java
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javax.swing.*;
import java.awt.*;
public class JavaFXSceneInSwingExample {
public static void main(final String[] args) {
EventQueue.invokeLater(() -> {
swingMain(args);
});
}
 
Search WWH ::




Custom Search