File Explorer using Java - how to go about it?
Asked Answered
P

2

8

I am set to create a file explorer using Java. The aim is to emulate the behavior of the default explorer as closely as possible, whatever may be the underlying OS.

I have done NO GUI programming in Java.

I have looked-up Swing, SWT and JFace, and I am beginning my project with this tutorial: http://www.ibm.com/developerworks/opensource/library/os-ecgui1/

I would like to know your opinions about the best approach to tackle this problem. If you could comment on complexity of coding, portability and OS-independence, and efficiency, it would be great.

Is there anything else I should know? Do some other ways exist?

Thanks a lot!


Thanks for the answers and replies.

Looks like I will choose Swing to implement the file explorer. What gives me the creeps is the thought that there would be nothing to mimic the default explorer view... Could you please provide some pointers about it? Do I get list of files, get icons and then arrange them in a grid fashion on the screen to show the default explorer view?

Paver answered 26/5, 2010 at 15:56 Comment(3)
Does "default explorer" here refer to the default of the platform/window manager? Because that would mean that you need to detect these things and use a different implementation for different platforms. I would start by googling if someone has already done something like this.Moat
yes, I mean exactly that by default explorer. I have googled; it is easy to find the directory tree structure code, but the explorer window with the icons laid out in grid fashion, no luck so far. :-|Paver
Cross-posted to groups.google.com/group/comp.lang.java.gui/browse_frm/thread/…Utah
D
6

You would be better off using Swing. You need different versions of SWT and JFace for different operating systems.

The best approach is to start off simple, and add to what you have as you learn more.

To get you started, you need a JFrame with two JPanel children.

You'll need to add a JMenuBar to the JFrame. JMenu items are added to the JMenuBar. JMenuItem items are added to the JMenu.

Oracle's Swing Overview will help you add more Swing components to your project.

Depilatory answered 26/5, 2010 at 16:10 Comment(3)
Pardon me if I am wrong, but I could see only JFileChooser in Swing, and it can display the open/save dialogue box; but I want something similar to a file explorer. So far, I haven't been able to see a way of doing that using JFileChooser.Paver
@user299988: You're going to have to build a file explorer from lots of Swing components. I gave you a list of components to start with in my answer.Depilatory
"Orcale's Swing Overview", hehehe, not used to that, should have been "Sun's Swing Overview" :PEpicrisis
U
4

I'd start with How to Use File Choosers, but the example in org.netbeans.swing.outline.Outline, discussed here, is appealing.

Addendum: @Gilbert Le Blanc raises an excellent point about the ease & portability of Swing. In contrast, SWT requires slightly more effort to deploy, but some users prefer the greater fidelity of org.eclipse.swt.widgets.FileDialog, as shown here.

Addendum: I notice that FileDialog displays a more native-looking window, as seen here. You might try it on your target platform(s).

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** @see https://stackoverflow.com/questions/2914733 */
public class FileDialogTest {

    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(0, 1));
        frame.add(new JButton(new AbstractAction("Load") {

            @Override
            public void actionPerformed(ActionEvent e) {
                FileDialog fd = new FileDialog(frame, "Test", FileDialog.LOAD);
                fd.setVisible(true);
                System.out.println(fd.getFile());
            }
        }));
        frame.add(new JButton(new AbstractAction("Save") {

            @Override
            public void actionPerformed(ActionEvent e) {
                FileDialog fd = new FileDialog(frame, "Test", FileDialog.SAVE);
                fd.setVisible(true);
                System.out.println(fd.getFile());
            }
        }));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
Utah answered 26/5, 2010 at 16:9 Comment(1)
I've added an example using java.awt.FileDialog, which may suit your needs better.Utah

© 2022 - 2024 — McMap. All rights reserved.