O
O
Oleg Mikhailov2018-02-11 20:29:10
JavaFX
Oleg Mikhailov, 2018-02-11 20:29:10

What is the correct way to call a window in Java FX?

Good evening.
Can't figure out what is the gist of calling child windows when using java fx. There is an auxiliary class that should have been responsible for calling the auxiliary window, but when loading () , it always throws an error:
Caused by: java.lang.IllegalStateException: Location is not set.
Here is the method itself:

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

And the fxml itself:
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>

Here is the .fxml file located inside the resource folder, which is marked with the corresponding marker. I tried to write the full path to the file /media/hdd/Documents/diplom/resources/test.fxml - did not help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Talaiko, 2018-03-12
@Olegatorapp

Try.

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

A
Alex, 2018-02-28
@libalex

If the FXML is in the resources folder, then tryInfoForm.class.getResource("/test.fxml")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question