How do I add a file browser inside my Java application?

五迷三道 提交于 2019-12-06 04:43:22

问题


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

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

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?


回答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.

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

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.




回答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.



来源:https://stackoverflow.com/questions/796743/how-do-i-add-a-file-browser-inside-my-java-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!