File Dialogs and Icons
Site Admin
· 11 Sep 2026
· 2 views
JFileChooser
JFileChooser is the Swing way to pick files, fully drawn by Java:
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
status.setText(f.getAbsolutePath());
}
JFileChooser Filters
Limit the shown files with a FileNameExtensionFilter:
chooser.setFileFilter(new FileNameExtensionFilter("Images", "png", "jpg", "gif"));Icons
ImageIcon loads images into buttons, labels and menu items:
ImageIcon icon = new ImageIcon("logo.png");
JLabel gif = new JLabel(new ImageIcon("loading.gif"));
JButton b = new JButton("Save", icon);![]()
Alternate File Dialog Display
Depending on the look-and-feel the chooser can render with different detail views, but the API stays the same:



Key Points
- Use JFileChooser for native-free open/save dialogs.
- FileNameExtensionFilter restricts which files appear.
- ImageIcon loads png/gif/jpg for labels, buttons and menus.