JavaFX加载FXML的性能问题

时间:2016-10-11 20:45:24

标签: javafx-8 fxml

我有一个主文件有切换按钮,单击其中一个按钮我启动了一个新的FXMLLoader,它有大约10个SVG,15个Textfield和6个微调器。另外它有CSS来相应地渲染节点...它可以很好地加载,没有错误或任何问题,但在显示场景之前需要一两秒钟。

我想这是因为节点数量同时被初始化。有没有办法在节点开始初始化之前显示场景?

注意:我的项目要求我在点击时导航到不同的场景。

选择它的ToggleButton

   if(settings.isSelected()){
        Stage stage = (Stage) mainWrapper.getScene().getWindow();
        Parent root = FXMLLoader.load(getClass().getResource("/fxmlFiles/settings.fxml"));
        stage.setScene(new Scene(root, Screen.getPrimary().getVisualBounds().getWidth(),
                Screen.getPrimary().getVisualBounds().getHeight()));
        stage.centerOnScreen();
        //stage.setMaximized(true);
        stage.show();
    }

fxmlFile

    <FlowPane xmlns:fx="http://javafx.com/fxml"
    fx:controller="controllers.motelInfoController"
    stylesheets="/cssFiles/motelInfo.css"
    fx:id="content">

    <VBox fx:id="mainWrapper">
    <VBox fx:id="validationWrapper">
        <Label fx:id="validationLabel" visible="false"/>
    </VBox>
    <VBox fx:id="generalInfo">
    <HBox> <!--First Row-->
            <HBox>
            <Group>
                <SVGPath fx:id="iconMotelName" scaleX="0.05" scaleY="0.05" fill="white"/>
            </Group>
            <Group>
                <TextField fx:id="miName" promptText="Motel Name"/>
            </Group>
            </HBox>
            <HBox>
                <Group>
                    <TextField fx:id="miFranchiseName" promptText="Franchise Name"/>
                </Group>
            </HBox>
        </HBox>
    <HBox> <!--Second Row-->
            <HBox>
                <Group>
                    <SVGPath fx:id="iconMotelAddress" scaleX="0.05" scaleY="0.05" fill="white"/>
                </Group>
                <Group>
                    <TextField fx:id="miAddress" promptText="Street Name"/>
                </Group>
            </HBox>
            <HBox>
                <Group>
                <TextField fx:id="miCity" promptText="City/Town Name"/>
                </Group>
            </HBox>
        </HBox>
    <HBox><!--Third Row-->
            <HBox style="-fx-spacing: 25px;">
                <Label></Label> <!--Empty label for purpose of leaving icon gap since N/A here-->
                <Group>
                    <TextField fx:id="miState" promptText="State"/>
                </Group>
            </HBox>
            <HBox>
                <Group>
                    <TextField fx:id="miZipCode" promptText="Zip Code"/>
                </Group>
            </HBox>
            <HBox>
                <Group>
                    <SVGPath fx:id="iconMotelContact" scaleX="0.05" scaleY="0.05" fill="white"/>
                </Group>
                <Group>
                    <TextField fx:id="miContact" promptText="Contact no."/>
                </Group>
            </HBox>
        </HBox>
    <HBox> <!--Fourth Row-->
            <HBox>
                <Group>
                    <SVGPath fx:id="iconMotelFax" scaleX="0.05" scaleY="0.05" fill="white"/>
                </Group>
                <Group>
                    <TextField fx:id="miFax" promptText="Fax no."/>
                </Group>
            </HBox>
            <HBox>
                <Group>
                    <SVGPath fx:id="iconMotelEmail" scaleX="0.05" scaleY="0.05" fill="white"/>
                </Group>
                <Group>
                    <TextField fx:id="miEmail" promptText="Email id"/>
                </Group>
            </HBox>
            <HBox>
                <Group>
                    <SVGPath fx:id="iconMotelNoOfRooms" scaleX="0.05" scaleY="0.05" fill="white"/>
                </Group>
                <Group>
                    <TextField fx:id="miNOR" promptText="No. of Rooms"/>
                </Group>
            </HBox>
        </HBox>
    <HBox style="-fx-spacing: 50px"> <!--Fifth Row-->
            <HBox>
                <Group>
                    <SVGPath fx:id="iconMotelCheckInTime" scaleX="0.05" scaleY="0.05" fill="white"/>
                </Group>
                <Group>
                    <Spinner fx:id="hourSpinnerCIT"/>
                </Group>
                <Group>
                    <Spinner fx:id="minSpinnerCIT"/>
                </Group>
                <Group>
                    <Spinner fx:id="secSpinnerCIT"/>
                </Group>
            </HBox>
            <HBox>
                <Group>
                    <SVGPath fx:id="iconMotelCheckOutTime" scaleX="0.05" scaleY="0.05" fill="white"/>
                </Group>
                <Group>
                    <Spinner fx:id="hourSpinnerCOT"/>
                </Group>
                <Group>
                    <Spinner fx:id="minSpinnerCOT"/>
                </Group>
                <Group>
                    <Spinner fx:id="secSpinnerCOT"/>
                </Group>
            </HBox>
        </HBox>
    </VBox>
    <VBox fx:id="roomOccupancy">
    <HBox> <!--First Row-->
            <Label fx:id="labelMaxGuestSelection"
                   text="Maximum Allowed Guest in individual Rooms. Please state the max count in adjacent cells"/>
            <VBox>
                <HBox>
                    <Group>
                        <SVGPath fx:id="iconMotelSingleBed" scaleX="0.05" scaleY="0.05" fill="white"/>
                    </Group>
                    <Group>
                        <TextField fx:id="miSB" promptText="Single Bed"/>
                    </Group>
                </HBox>
                <HBox>
                    <Group>
                        <SVGPath fx:id="iconMotelTwoBeds" scaleX="0.05" scaleY="0.05" fill="white"/>
                    </Group>
                    <Group>
                        <TextField fx:id="miTB" promptText="Two Beds"/>
                    </Group>
                </HBox>
                <HBox>
                    <Group>
                        <SVGPath fx:id="iconMotelfamilyRoom" scaleX="0.05" scaleY="0.05" fill="white"/>
                    </Group>
                    <Group>
                        <TextField fx:id="miFR" promptText="Family Room"/>
                    </Group>
                </HBox>
            </VBox>
        </HBox>
    <HBox> <!--Second Row-->
            <Label fx:id="labelMaxGuestCharged"
                    text="Rate Charged per Guest after max. allowed guest exceeds in a room"/>
            <HBox>
                <Group>
                    <SVGPath fx:id="iconMotelGuestExceedRate" scaleX="0.05" scaleY="0.05" fill="white"/>
                </Group>
                <Group>
                    <TextField fx:id="miGER" promptText="Rate/Guest Exceeded"/>
                </Group>
            </HBox>
        </HBox>
    </VBox>
    <VBox fx:id="submissionWrapper">
    <Group>
    <Button fx:id="submit" text="UPDATE" onAction="#saveData"/>
    </Group>
    </VBox>
    </VBox>

    </FlowPane>

FXML控制器

@Override
public void initialize(URL location, ResourceBundle resources) {

    //Load the data if the motelInfo file exist
    File checkTemp = new File("motelDetails.ser");
    if(checkTemp.exists()){
        readDataFromFile();
    }

    //Getting Values for the spinner
    for(int i=0;i<24;i++){
        hourList.add(String.format("%02d", i));
    }
    final SpinnerValueFactory<String> hourValuesCIT = new SpinnerValueFactory.ListSpinnerValueFactory<>(hourList);
    final SpinnerValueFactory<String> hourValuesCOT = new SpinnerValueFactory.ListSpinnerValueFactory<>(hourList);
    for(int i=0; i<60; i++){
        minList.add(String.format("%02d", i));
        secList.add(String.format("%02d", i));
    }
    final SpinnerValueFactory<String> minValuesCIT = new SpinnerValueFactory.ListSpinnerValueFactory<>(minList);
    final SpinnerValueFactory<String> minValuesCOT = new SpinnerValueFactory.ListSpinnerValueFactory<>(minList);
    final SpinnerValueFactory<String> secValuesCIT = new SpinnerValueFactory.ListSpinnerValueFactory<>(secList);
    final SpinnerValueFactory<String> secValuesCOT = new SpinnerValueFactory.ListSpinnerValueFactory<>(secList);

    styleSpinner(hourSpinnerCIT, hourValuesCIT);
    styleSpinner(hourSpinnerCOT, hourValuesCOT);
    styleSpinner(minSpinnerCIT, minValuesCIT);
    styleSpinner(minSpinnerCOT, minValuesCOT);
    styleSpinner(secSpinnerCIT, secValuesCIT);
    styleSpinner(secSpinnerCOT, secValuesCOT);


} 

1 个答案:

答案 0 :(得分:5)

看来你有两个问题:

  1. 在阻止更新的UI线程上运行长操作(SimpleDateFormat sdf = new SimpleDateFormat("HH:MM"); String dateString = sdf.format(TimeObject.getTargetTime()); String msgToPring = "Message "+dateString;
  2. 在FXMLoader.load()中完成所有操作,在完成
  3. 之前不允许绘制任何内容

    我建议通过在不同的线程中运行加载并在发生时显示加载屏幕来修复这两件事。见下一个例子:

    readDataFromFile()