Integrate fxml file using JFXPanel in swing

viper

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

James_D

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());
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Integrate fxml file using JFXPanel in swing

From Dev

Using custom Swing classes in FXML

From Dev

JFXPanel in Netbeans Swing Builder

From Dev

Thread.exit() line:not available error in integrating javafx into swing using jfxpanel

From Dev

Embed JavaFX Stage inside JFXPanel (Swing Embed)

From Dev

javafx JFXPanel embeded in swing application crash in mac

From Dev

Embedding a JFXPanel in a Swing application fails on add/remove

From Dev

Embedding a Swing content into a JavaFX application with FXML file and scene builder

From Dev

get FXML file nodes using java code

From Dev

get FXML file nodes using java code

From Dev

Cant launch application using JAVAFX FXML file

From Dev

JFXPanel inside Swing component hangs application on JFXPanel construction (os x & JDK 1.8)

From Dev

Prevent JavaFX thread from dying with JFXPanel Swing interop?

From Dev

Custom object drag-and-drop from embedded FX (JFXPanel) to Swing

From Java

JavaFX14 - problem running an FXML file using maven

From Dev

Constants in an FXML file

From Dev

Failed to load fxml file

From Dev

getHostServices().showDocument() in a FXML File

From Dev

Kotlin FXML file controller

From Dev

How to integrate Antivirus Commands in Java Swing

From Dev

Using custom Controls in fxml

From Dev

Calendar control using FXML

From Dev

error in login form wrote using java swing (with database file)

From Dev

Cocoapods no integrate in Pod file

From Dev

How to integrate SVG file?

From Java

For loop in FXML file || How to send data from controller to fxml file?

From Dev

For loop in FXML file || How to send data from controller to fxml file?

From Dev

Unable to find fxml file in Javafx

From Dev

set the content of an anchorPane with fxml file

Related Related

  1. 1

    Integrate fxml file using JFXPanel in swing

  2. 2

    Using custom Swing classes in FXML

  3. 3

    JFXPanel in Netbeans Swing Builder

  4. 4

    Thread.exit() line:not available error in integrating javafx into swing using jfxpanel

  5. 5

    Embed JavaFX Stage inside JFXPanel (Swing Embed)

  6. 6

    javafx JFXPanel embeded in swing application crash in mac

  7. 7

    Embedding a JFXPanel in a Swing application fails on add/remove

  8. 8

    Embedding a Swing content into a JavaFX application with FXML file and scene builder

  9. 9

    get FXML file nodes using java code

  10. 10

    get FXML file nodes using java code

  11. 11

    Cant launch application using JAVAFX FXML file

  12. 12

    JFXPanel inside Swing component hangs application on JFXPanel construction (os x & JDK 1.8)

  13. 13

    Prevent JavaFX thread from dying with JFXPanel Swing interop?

  14. 14

    Custom object drag-and-drop from embedded FX (JFXPanel) to Swing

  15. 15

    JavaFX14 - problem running an FXML file using maven

  16. 16

    Constants in an FXML file

  17. 17

    Failed to load fxml file

  18. 18

    getHostServices().showDocument() in a FXML File

  19. 19

    Kotlin FXML file controller

  20. 20

    How to integrate Antivirus Commands in Java Swing

  21. 21

    Using custom Controls in fxml

  22. 22

    Calendar control using FXML

  23. 23

    error in login form wrote using java swing (with database file)

  24. 24

    Cocoapods no integrate in Pod file

  25. 25

    How to integrate SVG file?

  26. 26

    For loop in FXML file || How to send data from controller to fxml file?

  27. 27

    For loop in FXML file || How to send data from controller to fxml file?

  28. 28

    Unable to find fxml file in Javafx

  29. 29

    set the content of an anchorPane with fxml file

HotTag

Archive