1

I am trying to save the last chosen file (by the user using JFilechooser) so that the next time the program runs, the file will be automatically opened.

public void actionPerformed(ActionEvent evt) {
    JFileChooser fileopen = new JFileChooser();
    FileFilter filter = new FileNameExtensionFilter("xml files", "xml");
    fileopen.addChoosableFileFilter(filter);
    
    int ret = fileopen.showDialog(null, "Open file");
    if (ret == JFileChooser.APPROVE_OPTION) {
        File file = fileopen.getSelectedFile();
        xmlSetUp(file);
        //add save file for next use
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Azza
  • 23
  • 6

1 Answers1

2

Only the accepted answer to the duplicate question mentions the Java Preferences API, but it doesn't contain any example code. The below code displays a JFrame that contains a JTextField that displays the path to the selected file as well as a JButton. When you activate the JButton, it displays a JFileChooser. Once you select a file, that selection is saved as a [java] preference. The next time you run the same application, the JTextField will initially display the path that was saved in the preferences.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.prefs.Preferences;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Remember implements ActionListener, Runnable {
    private static final String  CHOOSE = "Choose";
    private static final String  LAST_FILE_CHOSEN = "LAST_FILE_CHOSEN";

    private JFrame  frame;
    private JTextField  textField;
    private String  savedFile;

    @Override
    public void actionPerformed(ActionEvent event) {
        String actionCommand = event.getActionCommand();
        switch (actionCommand) {
            case CHOOSE:
                selectFile();
                break;
            default:
                JOptionPane.showMessageDialog(frame,
                                              actionCommand,
                                              "UNHANDLED",
                                              JOptionPane.WARNING_MESSAGE);
        }
    }

    @Override
    public void run() {
        showGui();
    }

    private JButton createButton(String text, int mnemonic, String tooltip) {
        JButton button = new JButton(text);
        button.setMnemonic(mnemonic);
        button.addActionListener(this);
        return button;
    }

    private JPanel createButtonsPanel() {
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.add(createButton(CHOOSE, KeyEvent.VK_C, "Select file."));
        return buttonsPanel;
    }

    private JPanel createTopPanel() {
        JPanel topPanel = new JPanel();
        JLabel label = new JLabel("Selected File");
        topPanel.add(label);
        textField = new JTextField(20);
        Preferences prefs = Preferences.userNodeForPackage(getClass());
        savedFile = prefs.get(LAST_FILE_CHOSEN, "");
        textField.setText(savedFile);
        topPanel.add(textField);
        return topPanel;
    }

    private void selectFile() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        File initial = new File(savedFile);
        if (initial.exists()) {
            fileChooser.setCurrentDirectory(initial.getParentFile());
        }
        if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
            File f = fileChooser.getSelectedFile();
            String path = f.getAbsolutePath();
            textField.setText(path);
            if (!savedFile.equals(path)) {
                Preferences prefs = Preferences.userNodeForPackage(getClass());
                prefs.put(LAST_FILE_CHOSEN, path);
            }
        }
    }

    private void showGui() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createTopPanel(), BorderLayout.PAGE_START);
        frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Remember());
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41