File Explorer using Java - how to go about it?

后端 未结 2 872
长情又很酷
长情又很酷 2021-01-12 11:22

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.

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-12 11:35

    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);
        }
    }
    

提交回复
热议问题