Choose a gif file from file chooser dialog in Java

Description

The following code shows how to choose a gif file from file chooser dialog.

Example


//from   www  . j a  v  a2s. co  m
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class Main {

  public static void main(String[] args) {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
      public boolean accept(File f) {
        return f.getName().toLowerCase().endsWith(".gif")|| f.isDirectory();
      }

      public String getDescription() {
        return "GIF Images";
      }
    });

    int r = chooser.showOpenDialog(new JFrame());
    if (r == JFileChooser.APPROVE_OPTION) {
      String name = chooser.getSelectedFile().getName();
      System.out.println(name);
    }
  }
}

The code above generates the following result.

Choose a gif file from file chooser dialog in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer