2

My problem is, that i have a normal JavaFX fxml file, which automatically realigns its elements when i start it normally and resize it. But i when i start it with the JFXPanel suddenly nothing realigns anymore. And i have no idea what the reason could be. (I have to use this method to start my gui, there is no other way)

My code:

final JFXPanel panel = new JFXPanel();
panel.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        super.componentResized(e);
        // Maybe here some code?
    }
});

JFrame frame = new JFrame();

Platform.runLater(() -> {
    try {
        Group root = new Group();
        Scene scene = new Scene(root);

        FXMLLoader loader = new FXMLLoader(getClass().getResource("myGui.fxml"));
        Node node = loader.load();

        root.getChildren().add(node);
        panel.setScene(scene);
    } catch (IOException e) {
        e.printStackTrace();
    }
});

frame.add(panel);
frame.pack();
frame.setVisible(true);

I researched for hours and don't really find a solution to my problem. Maybe i searched wrong? i would be really grateful if someone could help me.

Im a bit suspicious with the Group object "root" but i have no clue what it does. (I have this code from a website, because im inexperienced with JFXPanels so im not entirely sure what every line does)

My whole class:

public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        test.startGui();
    }

    public void startGui() {
        final JFXPanel panel = new JFXPanel();
        panel.setPreferredSize(new Dimension(600, 400));
        panel.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                super.componentResized(e);
                // Maybe here some code?
            }
        });

        JFrame frame = new JFrame();

        Platform.runLater(() -> {
            try {
                Group root = new Group();
                Scene scene = new Scene(root);

                FXMLLoader loader = new FXMLLoader(getClass().getResource("myGui.fxml"));
                Node node = loader.load();

                root.getChildren().add(node);
                panel.setScene(scene);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

And my fxml file

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>


<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <StackPane prefHeight="150.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
         <children>
            <Label text="left" />
         </children>
      </StackPane>
      <StackPane prefHeight="150.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
         <children>
            <Label text="right" />
         </children>
      </StackPane>
   </children>
</HBox>

When you start the gui and resize it the left and right label should go further away. They should have the same distance to the borders of the gui.

Tamino W.
  • 139
  • 1
  • 1
  • 7
  • 3
    Can you provide a [mcve] so that somebody could just copy and paste from your question without any modifications at all and reproduce the issue? – jewelsea Dec 06 '19 at 23:37
  • @jewelsea I added my class and fxml file, thanks for trying to help. – Tamino W. Dec 07 '19 at 10:04

1 Answers1

1

You are adding the JFXPanel to a JFrame which uses BorderLayout by default.
To change the resizing and positioning of the JFXPanel set other layout manager.
For example:

frame.setLayout(new GridBagLayout());

c0der
  • 18,467
  • 6
  • 33
  • 65