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

ran*_*dra 4 java swing jfilechooser file-handling

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

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

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

jjn*_*guy 10

我更喜欢以下方式.

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.
Run Code Online (Sandbox Code Playgroud)

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

我通常把它从内部JButtonActionListener代码.

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

        // File chooser code goes here usually
    }
});
Run Code Online (Sandbox Code Playgroud)