Mega Code Archive

 
Categories / Java Tutorial / Swing
 

Using JFileChooser

import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; public class JFileChooserTest {   public static void main(String[] args) {     JFrame.setDefaultLookAndFeelDecorated(true);     JDialog.setDefaultLookAndFeelDecorated(true);     JFrame frame = new JFrame("JComboBox Test");     frame.setLayout(new FlowLayout());     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     JButton button = new JButton("Select File");     button.addActionListener(new ActionListener() {       public void actionPerformed(ActionEvent ae) {         JFileChooser fileChooser = new JFileChooser();         int returnValue = fileChooser.showOpenDialog(null);         if (returnValue == JFileChooser.APPROVE_OPTION) {           File selectedFile = fileChooser.getSelectedFile();           System.out.println(selectedFile.getName());         }       }     });     frame.add(button);     frame.pack();     frame.setVisible(true);   } }