如何在Java应用程序中添加文件浏览器?

时间:2022-06-20 07:26:27

I am new to Java progamming and am building a application that will add, display and remove files from a given folder location.

我是Java progamming的新手,我正在构建一个应用程序,它将添加,显示和删除给定文件夹位置的文件。

I have added files using JFileChooser and know how to delete the files. However I am stuck with the display portion.

我已经使用JFileChooser添加了文件,并知道如何删除文件。但是我被显示器部分困住了。

I want to display the files and folder using different icon inside my application. I tried to add a JFileChooser inside the display panel and disable the button and menu components of the dialog box, but I have not succeeded. Is there any better way to do this?

我想在我的应用程序中使用不同的图标显示文件和文件夹。我试图在显示面板中添加一个JFileChooser并禁用对话框的按钮和菜单组件,但我还没有成功。有没有更好的方法来做到这一点?

3 个解决方案

#1


I prefer the following way.

我更喜欢以下方式。

JFileChooser chooser= new JFileChooser();

int choice = choose.showOpenDialog();

if (choice != JFileChooser.APPROVE_OPTION) return;

File chosenFile = chooser.getSelectedFile();

// You can then do whatever you want with the file.

Calling this code will cause a JFileChooser to popup in its own window.

调用此代码将导致JFileChooser在其自己的窗口中弹出。

I usually call it from within a JButton's ActionListener code.

我通常在JButton的ActionListener代码中调用它。

fileChooseButton.addActionListener( new ActionListener(){
    public void actionPerformed(ActionEvent e){

        // File chooser code goes here usually
    }
});

#2


If you don't need all the flexibility of JFileChooser, you should use java.awt.FileDialog instead. Your OS X users will thank you. FileDialog uses a native file chooser window, while JFileChooser is a swing component, and lacks keyboard shortcuts and other niceties.

如果您不需要JFileChooser的所有灵活性,则应使用java.awt.FileDialog。您的OS X用户会感谢您。 FileDialog使用本机文件选择器窗口,而JFileChooser是一个swing组件,缺少键盘快捷键和其他细节。

#3


I've never fully replicated a file browser. I have displayed files in list/tables using the icon that is provided by your platform. This is rather easy to do with the help of FileSystemView. Use the getSystemIcon(File) method to retrieve the correct icon. Then you can build a JList/JTable of files using this icon.

我从未完全复制过文件浏览器。我使用您的平台提供的图标在列表/表格中显示文件。在FileSystemView的帮助下,这很容易做到。使用getSystemIcon(File)方法检索正确的图标。然后,您可以使用此图标构建JList / JTable文件。

#1


I prefer the following way.

我更喜欢以下方式。

JFileChooser chooser= new JFileChooser();

int choice = choose.showOpenDialog();

if (choice != JFileChooser.APPROVE_OPTION) return;

File chosenFile = chooser.getSelectedFile();

// You can then do whatever you want with the file.

Calling this code will cause a JFileChooser to popup in its own window.

调用此代码将导致JFileChooser在其自己的窗口中弹出。

I usually call it from within a JButton's ActionListener code.

我通常在JButton的ActionListener代码中调用它。

fileChooseButton.addActionListener( new ActionListener(){
    public void actionPerformed(ActionEvent e){

        // File chooser code goes here usually
    }
});

#2


If you don't need all the flexibility of JFileChooser, you should use java.awt.FileDialog instead. Your OS X users will thank you. FileDialog uses a native file chooser window, while JFileChooser is a swing component, and lacks keyboard shortcuts and other niceties.

如果您不需要JFileChooser的所有灵活性,则应使用java.awt.FileDialog。您的OS X用户会感谢您。 FileDialog使用本机文件选择器窗口,而JFileChooser是一个swing组件,缺少键盘快捷键和其他细节。

#3


I've never fully replicated a file browser. I have displayed files in list/tables using the icon that is provided by your platform. This is rather easy to do with the help of FileSystemView. Use the getSystemIcon(File) method to retrieve the correct icon. Then you can build a JList/JTable of files using this icon.

我从未完全复制过文件浏览器。我使用您的平台提供的图标在列表/表格中显示文件。在FileSystemView的帮助下,这很容易做到。使用getSystemIcon(File)方法检索正确的图标。然后,您可以使用此图标构建JList / JTable文件。