< prev index next >

src/demo/share/jfc/FileChooserDemo/FileChooserDemo.java

Print this page




 119         public String toString() {
 120             return name;
 121         }
 122     }
 123     private JButton showButton;
 124     private JCheckBox showAllFilesFilterCheckBox;
 125     private JCheckBox showImageFilesFilterCheckBox;
 126     private JCheckBox showFullDescriptionCheckBox;
 127     private JCheckBox useFileViewCheckBox;
 128     private JCheckBox useFileSystemViewCheckBox;
 129     private JCheckBox accessoryCheckBox;
 130     private JCheckBox setHiddenCheckBox;
 131     private JCheckBox useEmbedInWizardCheckBox;
 132     private JCheckBox useControlsCheckBox;
 133     private JCheckBox enableDragCheckBox;
 134     private JRadioButton singleSelectionRadioButton;
 135     private JRadioButton multiSelectionRadioButton;
 136     private JRadioButton openRadioButton;
 137     private JRadioButton saveRadioButton;
 138     private JRadioButton customButton;
 139     private JComboBox lafComboBox;
 140     private JRadioButton justFilesRadioButton;
 141     private JRadioButton justDirectoriesRadioButton;
 142     private JRadioButton bothFilesAndDirectoriesRadioButton;
 143     private JTextField customField;
 144     private final ExampleFileView fileView;
 145     private final ExampleFileSystemView fileSystemView;
 146     private static final Dimension hpad10 = new Dimension(10, 1);
 147     private static final Dimension vpad20 = new Dimension(1, 20);
 148     private static final Dimension vpad7 = new Dimension(1, 7);
 149     private static final Dimension vpad4 = new Dimension(1, 4);
 150     private static final Insets insets = new Insets(5, 10, 0, 10);
 151     private final FilePreviewer previewer;
 152     private final JFileChooser chooser;
 153 
 154     @SuppressWarnings("LeakingThisInConstructor")
 155     public FileChooserDemo() {
 156         UIManager.LookAndFeelInfo[] installedLafs = UIManager.
 157                 getInstalledLookAndFeels();
 158         for (UIManager.LookAndFeelInfo lafInfo : installedLafs) {
 159             try {
 160                 Class<?> lnfClass = Class.forName(lafInfo.getClassName());
 161                 LookAndFeel laf = (LookAndFeel) (lnfClass.newInstance());
 162                 if (laf.isSupportedLookAndFeel()) {
 163                     String name = lafInfo.getName();
 164                     SupportedLaF supportedLaF = new SupportedLaF(name, laf);
 165                     supportedLaFs.add(supportedLaF);
 166                     if (NIMBUS_LAF_NAME.equals(name)) {
 167                         nimbusLaF = supportedLaF;
 168                     }
 169                 }
 170             } catch (Exception ignored) {
 171                 // If ANYTHING weird happens, don't add this L&F
 172             }
 173         }
 174 
 175         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
 176 
 177         chooser = new JFileChooser();
 178         previewer = new FilePreviewer(chooser);
 179 
 180         // Create Custom FileView
 181         fileView = new ExampleFileView();


 275         group3.add(bothFilesAndDirectoriesRadioButton);
 276         bothFilesAndDirectoriesRadioButton.addActionListener(optionListener);
 277 
 278         singleSelectionRadioButton = new JRadioButton("Single Selection", true);
 279         singleSelectionRadioButton.addActionListener(optionListener);
 280 
 281         multiSelectionRadioButton = new JRadioButton("Multi Selection");
 282         multiSelectionRadioButton.addActionListener(optionListener);
 283 
 284         ButtonGroup group4 = new ButtonGroup();
 285         group4.add(singleSelectionRadioButton);
 286         group4.add(multiSelectionRadioButton);
 287 
 288 
 289         // Create show button
 290         showButton = new JButton("Show FileChooser");
 291         showButton.addActionListener(this);
 292         showButton.setMnemonic('s');
 293 
 294         // Create laf combo box
 295         lafComboBox = new JComboBox(supportedLaFs.toArray());
 296         lafComboBox.setSelectedItem(nimbusLaF);
 297         lafComboBox.setEditable(false);
 298         lafComboBox.addActionListener(optionListener);
 299 
 300         // ********************************************************
 301         // ******************** Dialog Type ***********************
 302         // ********************************************************
 303         JPanel control1 = new InsetPanel(insets);
 304         control1.setBorder(BorderFactory.createTitledBorder("Dialog Type"));
 305 
 306         control1.setLayout(new BoxLayout(control1, BoxLayout.Y_AXIS));
 307         control1.add(Box.createRigidArea(vpad20));
 308         control1.add(openRadioButton);
 309         control1.add(Box.createRigidArea(vpad7));
 310         control1.add(saveRadioButton);
 311         control1.add(Box.createRigidArea(vpad7));
 312         control1.add(customButton);
 313         control1.add(Box.createRigidArea(vpad4));
 314         JPanel fieldWrapper = new JPanel();
 315         fieldWrapper.setLayout(new BoxLayout(fieldWrapper, BoxLayout.X_AXIS));


 712                 if (selected) {
 713                     chooser.setMultiSelectionEnabled(false);
 714                 }
 715             } else if (c == multiSelectionRadioButton) {
 716                 if (selected) {
 717                     chooser.setMultiSelectionEnabled(true);
 718                 }
 719             } else if (c == lafComboBox) {
 720                 SupportedLaF supportedLaF = ((SupportedLaF) lafComboBox.
 721                         getSelectedItem());
 722                 LookAndFeel laf = supportedLaF.laf;
 723                 try {
 724                     UIManager.setLookAndFeel(laf);
 725                     SwingUtilities.updateComponentTreeUI(frame);
 726                     if (chooser != null) {
 727                         SwingUtilities.updateComponentTreeUI(chooser);
 728                     }
 729                     frame.pack();
 730                 } catch (UnsupportedLookAndFeelException exc) {
 731                     // This should not happen because we already checked
 732                     ((DefaultComboBoxModel) lafComboBox.getModel()).
 733                             removeElement(supportedLaF);
 734                 }
 735             }
 736 
 737         }
 738     }
 739 
 740 
 741     private class FilePreviewer extends JComponent implements
 742             PropertyChangeListener {
 743 
 744         ImageIcon thumbnail = null;
 745 
 746         @SuppressWarnings("LeakingThisInConstructor")
 747         public FilePreviewer(JFileChooser fc) {
 748             setPreferredSize(new Dimension(100, 50));
 749             fc.addPropertyChangeListener(this);
 750         }
 751 
 752         public void loadImage(File f) {




 119         public String toString() {
 120             return name;
 121         }
 122     }
 123     private JButton showButton;
 124     private JCheckBox showAllFilesFilterCheckBox;
 125     private JCheckBox showImageFilesFilterCheckBox;
 126     private JCheckBox showFullDescriptionCheckBox;
 127     private JCheckBox useFileViewCheckBox;
 128     private JCheckBox useFileSystemViewCheckBox;
 129     private JCheckBox accessoryCheckBox;
 130     private JCheckBox setHiddenCheckBox;
 131     private JCheckBox useEmbedInWizardCheckBox;
 132     private JCheckBox useControlsCheckBox;
 133     private JCheckBox enableDragCheckBox;
 134     private JRadioButton singleSelectionRadioButton;
 135     private JRadioButton multiSelectionRadioButton;
 136     private JRadioButton openRadioButton;
 137     private JRadioButton saveRadioButton;
 138     private JRadioButton customButton;
 139     private JComboBox<SupportedLaF> lafComboBox;
 140     private JRadioButton justFilesRadioButton;
 141     private JRadioButton justDirectoriesRadioButton;
 142     private JRadioButton bothFilesAndDirectoriesRadioButton;
 143     private JTextField customField;
 144     private final ExampleFileView fileView;
 145     private final ExampleFileSystemView fileSystemView;
 146     private static final Dimension hpad10 = new Dimension(10, 1);
 147     private static final Dimension vpad20 = new Dimension(1, 20);
 148     private static final Dimension vpad7 = new Dimension(1, 7);
 149     private static final Dimension vpad4 = new Dimension(1, 4);
 150     private static final Insets insets = new Insets(5, 10, 0, 10);
 151     private final FilePreviewer previewer;
 152     private final JFileChooser chooser;
 153 
 154     @SuppressWarnings("LeakingThisInConstructor")
 155     public FileChooserDemo() {
 156         UIManager.LookAndFeelInfo[] installedLafs = UIManager.
 157                 getInstalledLookAndFeels();
 158         for (UIManager.LookAndFeelInfo lafInfo : installedLafs) {
 159             try {
 160                 Class<?> lnfClass = Class.forName(lafInfo.getClassName());
 161                 LookAndFeel laf = (LookAndFeel) (lnfClass.getDeclaredConstructor().newInstance());
 162                 if (laf.isSupportedLookAndFeel()) {
 163                     String name = lafInfo.getName();
 164                     SupportedLaF supportedLaF = new SupportedLaF(name, laf);
 165                     supportedLaFs.add(supportedLaF);
 166                     if (NIMBUS_LAF_NAME.equals(name)) {
 167                         nimbusLaF = supportedLaF;
 168                     }
 169                 }
 170             } catch (Exception ignored) {
 171                 // If ANYTHING weird happens, don't add this L&F
 172             }
 173         }
 174 
 175         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
 176 
 177         chooser = new JFileChooser();
 178         previewer = new FilePreviewer(chooser);
 179 
 180         // Create Custom FileView
 181         fileView = new ExampleFileView();


 275         group3.add(bothFilesAndDirectoriesRadioButton);
 276         bothFilesAndDirectoriesRadioButton.addActionListener(optionListener);
 277 
 278         singleSelectionRadioButton = new JRadioButton("Single Selection", true);
 279         singleSelectionRadioButton.addActionListener(optionListener);
 280 
 281         multiSelectionRadioButton = new JRadioButton("Multi Selection");
 282         multiSelectionRadioButton.addActionListener(optionListener);
 283 
 284         ButtonGroup group4 = new ButtonGroup();
 285         group4.add(singleSelectionRadioButton);
 286         group4.add(multiSelectionRadioButton);
 287 
 288 
 289         // Create show button
 290         showButton = new JButton("Show FileChooser");
 291         showButton.addActionListener(this);
 292         showButton.setMnemonic('s');
 293 
 294         // Create laf combo box
 295         lafComboBox = new JComboBox<>(supportedLaFs.toArray(new SupportedLaF[0]));
 296         lafComboBox.setSelectedItem(nimbusLaF);
 297         lafComboBox.setEditable(false);
 298         lafComboBox.addActionListener(optionListener);
 299 
 300         // ********************************************************
 301         // ******************** Dialog Type ***********************
 302         // ********************************************************
 303         JPanel control1 = new InsetPanel(insets);
 304         control1.setBorder(BorderFactory.createTitledBorder("Dialog Type"));
 305 
 306         control1.setLayout(new BoxLayout(control1, BoxLayout.Y_AXIS));
 307         control1.add(Box.createRigidArea(vpad20));
 308         control1.add(openRadioButton);
 309         control1.add(Box.createRigidArea(vpad7));
 310         control1.add(saveRadioButton);
 311         control1.add(Box.createRigidArea(vpad7));
 312         control1.add(customButton);
 313         control1.add(Box.createRigidArea(vpad4));
 314         JPanel fieldWrapper = new JPanel();
 315         fieldWrapper.setLayout(new BoxLayout(fieldWrapper, BoxLayout.X_AXIS));


 712                 if (selected) {
 713                     chooser.setMultiSelectionEnabled(false);
 714                 }
 715             } else if (c == multiSelectionRadioButton) {
 716                 if (selected) {
 717                     chooser.setMultiSelectionEnabled(true);
 718                 }
 719             } else if (c == lafComboBox) {
 720                 SupportedLaF supportedLaF = ((SupportedLaF) lafComboBox.
 721                         getSelectedItem());
 722                 LookAndFeel laf = supportedLaF.laf;
 723                 try {
 724                     UIManager.setLookAndFeel(laf);
 725                     SwingUtilities.updateComponentTreeUI(frame);
 726                     if (chooser != null) {
 727                         SwingUtilities.updateComponentTreeUI(chooser);
 728                     }
 729                     frame.pack();
 730                 } catch (UnsupportedLookAndFeelException exc) {
 731                     // This should not happen because we already checked
 732                     ((DefaultComboBoxModel<?>) lafComboBox.getModel()).
 733                             removeElement(supportedLaF);
 734                 }
 735             }
 736 
 737         }
 738     }
 739 
 740 
 741     private class FilePreviewer extends JComponent implements
 742             PropertyChangeListener {
 743 
 744         ImageIcon thumbnail = null;
 745 
 746         @SuppressWarnings("LeakingThisInConstructor")
 747         public FilePreviewer(JFileChooser fc) {
 748             setPreferredSize(new Dimension(100, 50));
 749             fc.addPropertyChangeListener(this);
 750         }
 751 
 752         public void loadImage(File f) {


< prev index next >