How in to make so that when you open the next file using fileChooser , the file selection dialog box opens in the last directory where the previous file was opened? (That is, so that fileChooser remembers the directory of the last file) wrote this, but when you reopen the file (when you call the handleOpen() function handleOpen() ), it swears, the console is all red, tell handleOpen() where the error is

 String filePathName ="user.home"; @FXML private void handleOpen() { FileChooser fileChooser = new FileChooser(); // Π—Π°Π΄Π°Ρ‘ΠΌ Ρ„ΠΈΠ»ΡŒΡ‚Ρ€ Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠΉ FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter( "PDF files (*.pdf)", "*.pdf"); fileChooser.getExtensionFilters().add(extFilter); fileChooser.setInitialDirectory(new File(System.getProperty(filePathName))); // ΠŸΠΎΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ Π΄ΠΈΠ°Π»ΠΎΠ³ Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ Ρ„Π°ΠΉΠ»Π° File file = fileChooser.showOpenDialog(mainApp.getPrimaryStage()); if (file != null) { filePathName = file.getPath(); mainApp.loadPDFFromFile(file); labelQuantityPage.setText(Integer.toString(mainApp.getQuantityPage())); } } 

    2 answers 2

    The FileChooser has a special method that is responsible for the initial directory . Those. you create a FileChooser and then say

     fileChooser.setInitialDirectory(new File('/path/to/dir')) 

    Accordingly, you should save the directory from the previous FileChooser'a call FileChooser'a

    • and how to save the directory from the previous call? Is it necessary somehow through the registry? - Evgeni Gorohov
    • fileChooser.show () returns the name of the file. You can take it and save it to a variable. And then put this directory in the above method - Andrew Bystrov

    In the FileChooser object FileChooser we select the file. You can get the path to the folder containing the file using the getParent() method:

     @Override public void start(Stage primaryStage) throws Exception { final FileChooser fileChooser = new FileChooser(); fileChooser.setInitialDirectory(new File(System.getProperty("user.dir"))); //... button.setOnAction(event -> { File file = fileChooser.showOpenDialog(primaryStage); if (file != null) { textArea1.clear(); //... // Π·Π°ΠΏΠΎΠΌΠΈΠ½Π°Π΅ΠΌ Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ (послСдний) ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³: fileChooser.setInitialDirectory(new File(file.getParent())); } }); //... } 
    • Welcome to Stack Overflow in Russian! Please try to leave a little more detailed answers. you can add an answer by clicking edit - aleksandr barakin