簡體   English   中英

如何從JavaFx中的文件選擇器重置所選路徑

[英]How to reset a selected path from file chooser in JavaFx

我在滑動面板上有一個重置和繪制按鈕。 我從文件選擇器(位於RootLayout類中)中選擇所需的文件,並將文件路徑傳遞給控制器​​類。 然后,它執行一些處理並初始化DataCunstructor類中的字段。 通過單擊“繪制”,TreeTableView將顯示在MainController類中的幻燈片窗格中。 當我單擊我的重置按鈕時,該表將被清除,但是我不知道如何重置所選路徑。 重置后,如果再次單擊“繪制”,則會出現相同的樹表。 如果選擇另一個文件並單擊“繪制”,程序將中斷。 如何將包括路徑在內的所有字段重置為空,並能夠選擇另一個文件並處理該文件?

這是我在MainController類中的繪制和重置:

public void treeTableDraw(ActionEvent event) {

    drawTable();//creates the TreeTableView
    numberOfFunctions= dc.getFuncAll().size(); 
    numberOfOrganizations = dc.getSortedAssignedOrg().size();
    funcLabel.setText(numberOfFunctions+"");//set Lable value
    orgLabel.setText(numberOfOrganizations + "");//set Lable value

}
public void treeTableReset(ActionEvent event){

    funcLabel.setText("0");//reset Label
    orgLabel.setText("0");
    treeTable.getColumns().clear(); //clears columns (TreeTable)
    ///////////////////////////////////////
    //non of the following did the path reset//
    ///////////////////////////////////////
    //dc = new DataConstructor();
    //Controller controller = new Controller();
    //controller.setPath(null);
    RootLayoutController rlc = loader.getController();
    rlc.reset();

}

RootLayout類中的“我的文件選擇器”:

@FXML
private void handleOpen() {
    FileChooser fileChooser = new FileChooser();

    // Set extension filter
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
            "3lgm2 files (*.z3lgm)", "*z3lgm");
    fileChooser.getExtensionFilters().add(extFilter);

    // Show save file dialog
    File file = fileChooser.showOpenDialog(main.getPrimaryStage());
    path = file.toString();
    if (path != null) {

        new Controller(path);

    }
}
public void reset(){
path = null;
}

我在主類的rootlayout中心添加了OverView:

public class Main extends Application {
    private Stage primaryStage;
    private BorderPane rootLayout;

    //private ObservableList<DataConstructor> treeTableData = FXCollections.observableArrayList();

    @Override
    public void start(Stage primaryStage) {

            this.primaryStage = primaryStage;
            this.primaryStage.setTitle("IT-Saturation");
            initRootLayout();
            showOverView();


    }

    private void showOverView() {
        try{
        FXMLLoader loader = new FXMLLoader();

        loader.setLocation(Main.class.getResource("/view/OverView.fxml"));
        AnchorPane overView = (AnchorPane) loader.load();
        rootLayout.setCenter(overView);
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    private void initRootLayout() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("/view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();
            //show scene containing the root layout
            Scene scene = new Scene(rootLayout);
            scene.getStylesheets().add(
                    getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            //gives controller access to main app
            RootLayoutController controller = loader.getController();
            controller.setMainApp(this);
            primaryStage.show();
        } catch (IOException e) {

            e.printStackTrace();
        }
        File file = getFilePath();
        if (file != null) {
            loadDataFromFile(file);
        }


    }

    /**
     * Returns the main stage.
     * @return primaryStage
     */
    public Stage getPrimaryStage() {
        return primaryStage;
    }
    public static void main(String[] args) {
        launch(args);
    }
}

只需在RootLayout類中聲明一個reset()

public class RootLayout {

    private Path path;

    @FXML
    private void handleOpen() {
        ...
        path = file.toString();
    }

    public void reset() {
        path= null;
    }
}

永遠不要使用關鍵字new構造構造函數,始終從FXMLLoader獲取它。

public class MainController {

    ...
    RootLayoutController controller = loader.getController();
    controller.reset();
    ...

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM