JavaFX - brak responsywnosci. Podmiana plików FXML

0

Cześć,

Próbuje załadować plik FXML, który podmienia już istniejący. Jednak po jego załadowaniu apka nie jest responsywna. Nie zachowuję się tak jakbym chciał.

Podrzucam kod i zdjęcia włączonej apki(Bez bibliotek). Proszę o pomoc.

main.java

@Override
public void start(Stage primaryStage) {
    try {
        Parent root = FXMLLoader.load(getClass().getResource("fxml1.fxml"));

          Stage  stage = primaryStage;
            Scene scene = new Scene(root, 300, 275);

            stage.setTitle("FXML Welcome");
            stage.setScene(scene);
            stage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

Controller

public class fxmlTESTcontroller {

	@FXML
	private Button changeScene1;

	@FXML
	private Button ChangeScene2;

	@FXML
	private VBox change;




	@FXML
	void changeSceneMethod(ActionEvent e) throws IOException
	{
		change.getChildren().clear();
		change.getChildren().add(FXMLLoader.load(getClass().getResource("fxml2.fxml")));
	}

	@FXML
	void changeScene2Method(ActionEvent e)
	{

	}
}

fxml1

<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.fxmlTESTcontroller">
   <children>
      <BorderPane prefHeight="300.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <left>
            <ListView maxWidth="200.0" BorderPane.alignment="CENTER" />
         </left>
         <center>
            <VBox fx:id="change" prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
               <children>
                  <GridPane VBox.vgrow="ALWAYS">
                    <columnConstraints>
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                      <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    </columnConstraints>
                    <rowConstraints>
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    </rowConstraints>
                     <children>
                        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Text" />
                        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Text" GridPane.columnIndex="1" />
                        <TextField GridPane.columnIndex="1" GridPane.rowIndex="1" />
                        <TextArea prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Text" GridPane.rowIndex="1" />
                        <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Text" GridPane.rowIndex="2" />
                     </children>
                  </GridPane>
                  <HBox>
                     <children>
                        <Pane HBox.hgrow="ALWAYS" />
                        <Button fx:id="changeScene1" mnemonicParsing="false" onAction="#changeSceneMethod" text="Button" HBox.hgrow="ALWAYS" />
                     </children>
                  </HBox>
               </children>
            </VBox>
         </center>
      </BorderPane>
   </children>
</AnchorPane>

fxml2

<VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.fxmlTESTcontroller">
   <children>
      <GridPane VBox.vgrow="ALWAYS">
        <columnConstraints>
          <ColumnConstraints hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="ALWAYS" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="ALWAYS" />
        </rowConstraints>
         <children>
            <Button mnemonicParsing="false" text="Button" GridPane.columnIndex="1" />
            <Label text="Label" />
            <Label text="Label" GridPane.rowIndex="1" />
            <Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <Label text="Label" GridPane.rowIndex="2" />
            <TextArea GridPane.columnIndex="1" GridPane.hgrow="ALWAYS" GridPane.rowIndex="2" GridPane.vgrow="ALWAYS" />
         </children>
      </GridPane>
      <HBox VBox.vgrow="ALWAYS">
         <children>
            <Pane HBox.hgrow="ALWAYS" />
            <Button fx:id="ChangeScene2" mnemonicParsing="false" onAction="#changeScene2Method" text="Button" HBox.hgrow="ALWAYS" />
         </children>
      </HBox>
   </children>
</VBox>

Oraz zdjęcia apki

  1. user image Zdjęcie apki z zaladowanym fxml1 (Responsywne)
  2. user image Zdjecie apki z podmienionym fxml1 na fxml2 (Jak widzicie przycisk nie przykleja sie do dołu)
1

Witam. Kiedyś się bawiłem z zamianą, widzę że podmieniasz całość , dobry sposobem by było np zamknąć w jednym Pane czy AnchorPane prawą stronę czyli z tego pierwszego ten Button i TextArea i nadać temu ID. Gdy wywołasz funkcję changeSceneMethod tylko podmieniasz zawartość tego Pane/AnchorPane i jest łatwiej. Jeżeli miał byś problem to pisz na PW.

1 użytkowników online, w tym zalogowanych: 0, gości: 1