views:

658

answers:

3

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?

+4  A: 

I prefer the following way.

JFileChooser chooser= new JFileChooser();

int choice = choose.showOpenDialog();

if (choice != JFileChooser.APPROVE_OPTION) return;

File chosenFile = chooser.getChosenFile();

// 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
    }
});
jjnguy
Thank you for your reply. I have done this and successfully, i used JFileChooser's open dialog box and copied the selected file to a folder. Now i need to display the content of that file inside a panel. Like explorer or file browser view. I can add openDialog or other dialog using JfileChooser, the only problem there would be of extra buttons. How do i disable them(top menu, button for new folder and so on) other then the file and folder view. Thank you
I would recommend asking that question as another question. It would be much more helpful to others there.
jjnguy
Besides, the question and answer editors offer tons more formatting options.
jjnguy
A: 

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.

Sam Barnum
A: 

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.

willcodejavaforfood
Thank you,I will give it a try.