Olegatorapp
@Olegatorapp
Java/Android dev

Как правильно вызвать окно в Java FX?

Добрый вечер.
Не могу понять, какова суть для вызова дочерних окон при использовании java fx. Есть вспомогательный класс, который должен был отвечать за вызов вспомогательного окна, но при load() всё время выбивает ошибку:
Caused by: java.lang.IllegalStateException: Location is not set.

Вот сам метод:
InfoForm.class
public class InfoForm {


    public void showInfoForm() {
        Label secondLabel = new Label("Info");

//        StackPane secondaryLayout = new StackPane();
//        secondaryLayout.getChildren().add(secondLabel);
        FXMLLoader loader = new FXMLLoader(getClass().getResource("test.fxml"));
        loader.setRoot(this);
        Pane root = null;
        try {
            root = loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene secondScene = new Scene(root, 230, 100);

        Stage newWindow = null;
        newWindow.setTitle("Second Stage");
        newWindow.setScene(secondScene);
        newWindow.initModality(Modality.WINDOW_MODAL);
        newWindow.show();
    }
}

И сам fxml:
FXML
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>


<?import javafx.scene.control.Label?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.TextField?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1">
   <children>
     <GridPane layoutX="3.0" layoutY="-1.0" prefHeight="200.0" prefWidth="287.0">
       <columnConstraints>
         <ColumnConstraints hgrow="SOMETIMES" maxWidth="94.0" minWidth="10.0" prefWidth="69.0" />
         <ColumnConstraints hgrow="SOMETIMES" maxWidth="131.0" minWidth="10.0" prefWidth="131.0" />
       </columnConstraints>
       <rowConstraints>
         <RowConstraints maxHeight="73.0" minHeight="10.0" prefHeight="65.0" vgrow="SOMETIMES" />
         <RowConstraints maxHeight="73.0" minHeight="10.0" prefHeight="65.0" vgrow="SOMETIMES" />
         <RowConstraints maxHeight="81.0" minHeight="10.0" prefHeight="74.0" vgrow="SOMETIMES" />
         <RowConstraints maxHeight="67.0" minHeight="10.0" prefHeight="61.0" vgrow="SOMETIMES" />
       </rowConstraints>
       <opaqueInsets>
         <Insets />
       </opaqueInsets>
       <children>
         <Label text="IP/MAC" />
         <Label text="Name" GridPane.rowIndex="1" />
         <Label text="Сhannel" GridPane.rowIndex="2" />
         <Label text="Max.speed" GridPane.rowIndex="3" />
         <ComboBox prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
         <TextField GridPane.columnIndex="1" />
         <TextField GridPane.columnIndex="1" GridPane.rowIndex="1" />
         <TextField GridPane.columnIndex="1" GridPane.rowIndex="3" />
       </children>
     </GridPane>
   </children>
</Pane>

Вот файл .fxml лежит внутри папки resource, которая помечена соответствующим маркером. Пробовал писать полный путь к файлу /media/hdd/Documents/diplom/resources/test.fxml - не помогло
  • Вопрос задан
  • 922 просмотра
Решения вопроса 1
jsdevel
@jsdevel
Java разработчик. Хороший парень, наверное.
Попробуй.
@Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("mainframe.fxml")); //смотрит уже в resources
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setScene(new Scene(fxmlLoader.load(), 1100, 700));
        primaryStage.show();
    }
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
libalex
@libalex
Backend / Android Developer (Java, Kotlin)
Если FXML лежит в папке ресурсов, то попробуйте InfoForm.class.getResource("/test.fxml")
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы