Swing - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Swing FileChooser

Swing FileChooser

shape Description

Swing FileChooser, Swing component is an object having a graphical representation that can be displayed on the screen and that can interact with the user. Examples of components are the buttons, checkboxes, and scrollbars of a typical graphical user interface. Swing components are fundamental building pieces of an application. Swing FileChooser is a component which gives a functionality to select the data from the system. Following is an example which describes more about this concept.

shape Example

Following is an example for Swing FileChooser from the system. SimpleFileChooser.java [java]package splessons; //SimpleFileChooser.java //A simple file chooser to see what it takes to make one of these work. // import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class SimpleFileChooser extends JFrame { public SimpleFileChooser() { super("SPLesson - File Chooser Test Frame"); setSize(350, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane(); c.setLayout(new FlowLayout()); JButton openButton = new JButton("Open"); JButton saveButton = new JButton("Save"); JButton dirButton = new JButton("Pick Dir"); final JLabel statusbar = new JLabel("Output of your selection will go here"); // Here Create a file chooser that opens up as an Open dialog openButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(true); int option = chooser.showOpenDialog(SimpleFileChooser.this); if (option == JFileChooser.APPROVE_OPTION) { File[] sf = chooser.getSelectedFiles(); String filelist = "nothing"; if (sf.length > 0) filelist = sf[0].getName(); for (int i = 1; i < sf.length; i++) { filelist += ", " + sf[i].getName(); } statusbar.setText("You chose " + filelist); } else { statusbar.setText("You canceled."); } } }); // Create a file chooser that opens up as a Save dialog saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser chooser = new JFileChooser(); int option = chooser.showSaveDialog(SimpleFileChooser.this); if (option == JFileChooser.APPROVE_OPTION) { statusbar.setText("You saved " + ((chooser.getSelectedFile()!=null)? chooser.getSelectedFile().getName():"nothing")); } else { statusbar.setText("You canceled."); } } }); // Create a file chooser that allows you to pick a directory // rather than a file dirButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int option = chooser.showOpenDialog(SimpleFileChooser.this); if (option == JFileChooser.APPROVE_OPTION) { statusbar.setText("You opened " + ((chooser.getSelectedFile()!=null)? chooser.getSelectedFile().getName():"nothing")); } else { statusbar.setText("You canceled."); } } }); c.add(openButton); c.add(saveButton); c.add(dirButton); c.add(statusbar); } public static void main(String args[]) { SimpleFileChooser sfc = new SimpleFileChooser(); sfc.setVisible(true); } }[/java] Create a class that should extend JFrame. [java]public class SimpleFileChooser extends JFrame { public SimpleFileChooser() { super("SPLesson - File Chooser Test Frame"); setSize(350, 200);[/java] Create the container. [java]Container c = getContentPane();[/java] Set the logic at status bar. [java]statusbar.setText("You opened " + ((chooser.getSelectedFile()!=null)? chooser.getSelectedFile().getName():"nothing"));[/java] Output When compile the program following is the output will be displayed. When click on open following output will be displayed.

Summary

shape Points

  • Swing FileChooser - setDefaultCloseOperation(EXIT_ON_CLOSE); to close the operation done user.
  • To create the gap between the components developer will use setAutoCreateContainerGaps(true); .
  • To add the more effectiveness to the application different buttons will be added to the applications.