Reputation: 734
I know How to integrate javafx components in swing using JFXPanel. But its just hard to code all the properties for the components. Moreover, it is also not possible to give css properties to the javafx components. So is is possible to integrate a whole fxml file in the JFXPanel? Thank you
Upvotes: 2
Views: 4509
Reputation: 209684
To give css properties to the javafx components, simply add a stylesheet to the scene you set on the JFXPanel
.
To use FXML to define the components displayed in the JFXPanel
, load the FXML file using an FXMLLoader
as usual, and use the result as the root of the Scene
for the JFXPanel
.
For example:
import java.awt.BorderLayout;
import java.io.IOException;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
private void initSwingComponents() {
JFrame frame = new JFrame("Java FX in Swing");
frame.setLayout(new BorderLayout());
final JFXPanel jfxPanel = new JFXPanel();
frame.add(jfxPanel, BorderLayout.CENTER);
final JPanel swingButtons = new JPanel();
final JButton okButton = new JButton("OK");
okButton.addActionListener(event -> System.out.println("Swing says 'OK'"));
final JButton exitButton = new JButton("Exit");
exitButton.addActionListener(event -> System.exit(0));
swingButtons.add(okButton);
swingButtons.add(exitButton);
frame.add(swingButtons, BorderLayout.SOUTH);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
okButton.requestFocus();
Platform.runLater(() -> initFX(jfxPanel));
}
private void initFX(JFXPanel jfxPanel) {
try {
Parent root = FXMLLoader.load(getClass().getResource("FXComponents.fxml"));
Scene scene = new Scene(root, 250, 150);
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
jfxPanel.setScene(scene);
} catch (IOException exc) {
exc.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
Test test = new Test();
SwingUtilities.invokeLater(() -> test.initSwingComponents() );
}
}
FXComponents.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.Button?>
<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="Controller" spacing="5" alignment="CENTER">
<TextField fx:id="textField" promptText="Enter message here"/>
<Button text="Print message" onAction="#printMessage" />
</VBox>
style.css:
@CHARSET "UTF-8";
.button {
-fx-base: cornflowerblue ;
}
.text-field {
-fx-text-fill: blue ;
-fx-font-size: 18pt ;
}
Controller.java:
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
public class Controller {
@FXML
private TextField textField ;
@FXML
private void printMessage() {
System.out.println(textField.getText());
}
}
Upvotes: 4