public class FileChooser extends JFrame {
public FileChooser() {
super("Select File");
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(Box.createVerticalGlue());
final JLabel label = new JLabel("The selected file");
label.setAlignmentX(CENTER_ALIGNMENT);
panel.add(label);
panel.add(Box.createRigidArea(new Dimension(10, 10)));
JButton button = new JButton("Select the file");
button.setAlignmentX(CENTER_ALIGNMENT);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
label.setText(file.getName());
}
}
});
panel.add(button);
panel.add(Box.createVerticalGlue());
getContentPane().add(panel);
setPreferredSize(new Dimension(260, 220));
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void fileCh() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
new FileChooser();
}
});
}
}