Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 553
ranendra Avatar asked Dec 08 '22 07:12

ranendra


1 Answers

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
    }
});
like image 146
jjnguy Avatar answered Dec 09 '22 20:12

jjnguy