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());
}

Swing JFileChooser dialog

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);

Swing ImageIcon demo

Alternate File Dialog Display

Depending on the look-and-feel the chooser can render with different detail views, but the API stays the same:

File chooser view 1

File chooser view 2

File chooser view 3

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.
Share this post:

Comments (0)

Please login or register to comment.