JavaFx basic operation [personal notes]

 

1. Disable window zooming

primaryStage.setResizable(true);//Disable form scaling

2. Remove the window border (including close, maximize, minimize buttons, etc.)

primaryStage.initStyle(StageStyle.UNDECORATED);

3. Load and use the FXML file

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("fxml/Main.fxml"));//The difference between getClass().getClassLoader().getResource() and getClass().getResource() is not in the root directory

4. Width adaptive binding

menuBar.prefWidthProperty().bind(pane.widthProperty());//MenuBar control width binding and Pane layout width, width scaling with window

5.TableView control binding data

List<Spider> spiderList=new ArrayList<Spider>();
        spiderList.add(new Spider(1,"Baidu","The largest Chinese search engine","https://www.baidu.com"));
        spiderList.add(new Spider(2,"Baidu 2","The largest Chinese search engine 2","https://www.baidu.com2https://www.baidu.comhttps://www.baidu.comhttps://www.baidu.com"));
        spiderList.add(new Spider(3,"Baidu 3","The largest Chinese search engine 3","https://www.baidu.com3"));
        spiderList.add(new Spider(4,"Baidu 4","The largest Chinese search engine 4","https://www.baidu.com4"));
        ObservableList<Spider> observableList= FXCollections.observableList(spiderList);
        tableView.setItems(observableList);

6. The new pop-up window is at the top level and cannot be clicked on the original window

stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();

7. Pop up a new window (directly use the method of this class to pop up a new window)

public class NewStage {
    public static void showStage() {
        Stage stage=new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);
        Pane pane= null;
        try {
            pane = FXMLLoader.load(NewStage.class.getResource("newStage.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene=new Scene(pane,300,150);
        stage.setScene(scene);
        stage.showAndWait();
    }
}

8. Switch to a new scene (the scene set in the window will be switched)

Scene scene1=new Scene(pane,300,150);
Scene scene2=new Scene(pane,300,150);
primaryStage.setScene(scene1);//Switch scenario 1
primaryStage.setScene(scene2);//Switch scenario 2

9.FXML and Controller event binding

<Button layoutX="192.0" layoutY="289.0" mnemonicParsing="false" text="Display notification" onAction="#toastBtn" />
public void toastBtn(ActionEvent event){
        SystemToast.toast();
    }

10. minimization

stage.setIconified(true);

11. full screen

stage.setFullScreen(true);

12. Suspended at the top of all forms

stage.setAlwaysOnTop(true);

To be continued

Posted by bubblewrapped on Fri, 31 Jan 2020 00:34:27 -0800