hi,
how could i display the contents of a folder in the jframe without using jfilechooser, this happen
by clicking browse button and the contents of folder appears in my jframe directly??

Dani AI

Generated

Short answer: pick the view you want (flat list, detailed table, or tree), read the directory entries yourself, and populate a Swing model on the Event Dispatch Thread while doing file I/O off the EDT.

As asked, and following and , here is a practical pattern you can apply. Use File / NIO to get entries, a Swing model (DefaultListModel, DefaultTableModel, or DefaultTreeModel) to hold them, and a SwingWorker to load them so the UI stays responsive. For a simple browse pane a JList is easiest; a JTree is better for hierarchical views (use lazy-loading nodes to avoid walking entire trees).

Example: background loader that fills a DefaultListModel<File> for a JList<File>:

DefaultListModel<File> model = new DefaultListModel<>();
JList<File> list = new JList<>(model);

new SwingWorker<Void, File>() {
  protected Void doInBackground() {
    File dir = new File(path);
    File[] files = dir.listFiles();
    if (files == null) return null; // handle non-dir / permission
    Arrays.sort(files, Comparator.comparing(File::getName, String.CASE_INSENSITIVE_ORDER));
    for (File f : files) publish(f);
    return null;
  }
  protected void process(List<File> chunks) {
    for (File f : chunks) model.addElement(f);
  }
}.execute();

Add a cell renderer to show system icons and names (use FileSystemView.getFileSystemView().getSystemIcon(file)), and add a mouse listener to handle double-clicks (Desktop.getDesktop().open(file) for files, or reload model for directories). For large folders use streaming (NIO Files.list) or pagination, and for live updates look into WatchService to refresh the view when the directory changes.

Troubleshooting tips: always check listFiles() for null (permissions or not-a-directory), avoid blocking the EDT, catch IO/security exceptions, and use a lazy tree model for deep hierarchies so expansions load on demand.

Recommended Answers

All 3 Replies

By reading the content of the folder yourself using the methods available on the File class, and putting the data you get that way into some container which you can put in that frame.

could you please show how to this......

Take a look at JTree and How To Use Trees.
If you don't want to use a JTree then you'll have to decide just how you want to represent that data and how you want to interact with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.