1

I am creating a user interface for a pdf reader, and I want to have a list with recent open files. (Not the content of the files, just the name of the file).

This is my picture

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Andrei
  • 11
  • 3
  • Please edit your question to include a [mcve] that shows your current approach and focuses on the just the "put that name in a `textField` part of the problem." – trashgod May 10 '16 at 23:56
  • I hope You can understand now..I'm not that good with english. :( – Andrei May 11 '16 at 00:12

2 Answers2

1

From the snap shared, I assume that you are using JFileChooser for your dialog to open file. Hope this helps!

int result = fileChooser.showOpenDialog(panel);
//where panel is an instance of a Component such as JFrame, JDialog or JPanelwhich is parent of the dialog.
if (result == JFileChooser.APPROVE_OPTION) {
    File selectedFile = fileChooser.getSelectedFile();
    textArea.setText(selectedFile.getName());
}
PeaceIsPearl
  • 329
  • 2
  • 6
  • 19
  • @Andrew may want more than one file in the list. – trashgod May 11 '16 at 04:58
  • The setText, delete the previous text and set the text you are giving to it. The append keep the current text in your text area. textArea.appendText(selectedFile.getName()); Hope it helps! – PeaceIsPearl May 11 '16 at 13:19
1

The example below uses a JFileChooser with setMultiSelectionEnabled(true) to add() one or more files to a List<File> and update() a JTextArea with the current list. As suggested here, you can use Action to maintain a menu or tool bar of recent files.

image

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 * @see https://stackoverflow.com/a/37153404/230513
 * @see https://stackoverflow.com/a/4039359/230513
 */
public class Test {

    private final List<File> recentFiles = new ArrayList<>();
    private final JTextArea textArea = new JTextArea(12, 12);

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        textArea.setBorder(BorderFactory.createTitledBorder("Recent Files"));
        f.add(textArea);
        JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        p.add(new JButton(new AbstractAction("Open") {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc = new JFileChooser(".");
                jfc.setMultiSelectionEnabled(true);
                if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    recentFiles.addAll(Arrays.asList(jfc.getSelectedFiles()));
                    update();
                }
            }
        }));
        f.add(p, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private void update() {
        textArea.setText("");
        for (File file : recentFiles) {
            textArea.append(file.getName() + "\n");
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045