42
import java.lang.reflect.InvocationTargetException;
43
import java.util.logging.Level;
44
import java.util.logging.Logger;
45
import javax.swing.UIManager.LookAndFeelInfo;
46
import java.awt.BorderLayout;
47
import java.awt.CardLayout;
48
import java.awt.Component;
49
import java.awt.Dimension;
50
import java.awt.Graphics;
52
import java.awt.Insets;
53
import java.awt.event.ActionEvent;
54
import java.awt.event.ActionListener;
55
import java.beans.PropertyChangeEvent;
56
import java.beans.PropertyChangeListener;
58
import javax.swing.BorderFactory;
59
import javax.swing.Box;
60
import javax.swing.BoxLayout;
61
import javax.swing.ButtonGroup;
62
import javax.swing.DefaultComboBoxModel;
63
import javax.swing.ImageIcon;
64
import javax.swing.JButton;
65
import javax.swing.JCheckBox;
66
import javax.swing.JComboBox;
67
import javax.swing.JComponent;
68
import javax.swing.JDialog;
69
import javax.swing.JFileChooser;
70
import javax.swing.JFrame;
71
import javax.swing.JLabel;
72
import javax.swing.JOptionPane;
73
import javax.swing.JPanel;
74
import javax.swing.JRadioButton;
75
import javax.swing.JTextField;
76
import javax.swing.JToggleButton;
77
import javax.swing.LookAndFeel;
78
import javax.swing.SwingUtilities;
79
import javax.swing.UIManager;
80
import javax.swing.UnsupportedLookAndFeelException;
81
import javax.swing.WindowConstants;
82
import javax.swing.filechooser.FileFilter;
83
import javax.swing.filechooser.FileNameExtensionFilter;
84
import javax.swing.filechooser.FileSystemView;
85
import java.util.ArrayList;
86
import javax.swing.plaf.FileChooserUI;
87
import javax.swing.plaf.basic.BasicFileChooserUI;
89
import static javax.swing.JFileChooser.*;
98
@SuppressWarnings("serial")
99
public class FileChooserDemo extends JPanel implements ActionListener {
101
public static final String NIMBUS_LAF_NAME = "Nimbus";
102
private static JFrame frame;
103
private final List<SupportedLaF> supportedLaFs =
104
new ArrayList<SupportedLaF>();
105
private static SupportedLaF nimbusLaF;
108
private static class SupportedLaF {
110
private final String name;
111
private final LookAndFeel laf;
113
SupportedLaF(String name, LookAndFeel laf) {
119
public String toString() {
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;
154
@SuppressWarnings("LeakingThisInConstructor")
155
public FileChooserDemo() {
156
UIManager.LookAndFeelInfo[] installedLafs = UIManager.
157
getInstalledLookAndFeels();
158
for (UIManager.LookAndFeelInfo lafInfo : installedLafs) {
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;
170
} catch (Exception ignored) {
175
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
177
chooser = new JFileChooser();
178
previewer = new FilePreviewer(chooser);
181
fileView = new ExampleFileView();
182
fileView.putIcon("jpg", new ImageIcon(getClass().getResource(
183
"/resources/images/jpgIcon.jpg")));
184
fileView.putIcon("gif", new ImageIcon(getClass().getResource(
185
"/resources/images/gifIcon.gif")));
188
fileSystemView = new ExampleFileSystemView();
191
OptionListener optionListener = new OptionListener();
194
openRadioButton = new JRadioButton("Open");
195
openRadioButton.setSelected(true);
196
openRadioButton.addActionListener(optionListener);
198
saveRadioButton = new JRadioButton("Save");
199
saveRadioButton.addActionListener(optionListener);
201
customButton = new JRadioButton("Custom");
202
customButton.addActionListener(optionListener);
204
customField = new JTextField(8) {
207
public Dimension getMaximumSize() {
208
return new Dimension(getPreferredSize().width,
209
getPreferredSize().height);
212
customField.setText("Doit");
213
customField.setAlignmentY(JComponent.TOP_ALIGNMENT);
214
customField.setEnabled(false);
215
customField.addActionListener(optionListener);
217
ButtonGroup group1 = new ButtonGroup();
218
group1.add(openRadioButton);
219
group1.add(saveRadioButton);
220
group1.add(customButton);
223
showAllFilesFilterCheckBox = new JCheckBox("Show \"All Files\" Filter");
224
showAllFilesFilterCheckBox.addActionListener(optionListener);
225
showAllFilesFilterCheckBox.setSelected(true);
227
showImageFilesFilterCheckBox = new JCheckBox("Show JPG and GIF Filters");
228
showImageFilesFilterCheckBox.addActionListener(optionListener);
229
showImageFilesFilterCheckBox.setSelected(false);
231
accessoryCheckBox = new JCheckBox("Show Preview");
232
accessoryCheckBox.addActionListener(optionListener);
233
accessoryCheckBox.setSelected(false);
236
setHiddenCheckBox = new JCheckBox("Show Hidden Files");
237
setHiddenCheckBox.addActionListener(optionListener);
239
showFullDescriptionCheckBox = new JCheckBox("With File Extensions");
240
showFullDescriptionCheckBox.addActionListener(optionListener);
241
showFullDescriptionCheckBox.setSelected(true);
242
showFullDescriptionCheckBox.setEnabled(false);
244
useFileViewCheckBox = new JCheckBox("Use FileView");
245
useFileViewCheckBox.addActionListener(optionListener);
246
useFileViewCheckBox.setSelected(false);
248
useFileSystemViewCheckBox = new JCheckBox("Use FileSystemView", false);
249
useFileSystemViewCheckBox.addActionListener(optionListener);
251
useEmbedInWizardCheckBox = new JCheckBox("Embed in Wizard");
252
useEmbedInWizardCheckBox.addActionListener(optionListener);
253
useEmbedInWizardCheckBox.setSelected(false);
255
useControlsCheckBox = new JCheckBox("Show Control Buttons");
256
useControlsCheckBox.addActionListener(optionListener);
257
useControlsCheckBox.setSelected(true);
259
enableDragCheckBox = new JCheckBox("Enable Dragging");
260
enableDragCheckBox.addActionListener(optionListener);
263
ButtonGroup group3 = new ButtonGroup();
264
justFilesRadioButton = new JRadioButton("Just Select Files");
265
justFilesRadioButton.setSelected(true);
266
group3.add(justFilesRadioButton);
267
justFilesRadioButton.addActionListener(optionListener);
269
justDirectoriesRadioButton = new JRadioButton("Just Select Directories");
270
group3.add(justDirectoriesRadioButton);
271
justDirectoriesRadioButton.addActionListener(optionListener);
273
bothFilesAndDirectoriesRadioButton = new JRadioButton(
274
"Select Files or Directories");
275
group3.add(bothFilesAndDirectoriesRadioButton);
276
bothFilesAndDirectoriesRadioButton.addActionListener(optionListener);
278
singleSelectionRadioButton = new JRadioButton("Single Selection", true);
279
singleSelectionRadioButton.addActionListener(optionListener);
281
multiSelectionRadioButton = new JRadioButton("Multi Selection");
282
multiSelectionRadioButton.addActionListener(optionListener);
284
ButtonGroup group4 = new ButtonGroup();
285
group4.add(singleSelectionRadioButton);
286
group4.add(multiSelectionRadioButton);
290
showButton = new JButton("Show FileChooser");
291
showButton.addActionListener(this);
292
showButton.setMnemonic('s');
295
lafComboBox = new JComboBox<>(supportedLaFs.toArray(new SupportedLaF[0]));
296
lafComboBox.setSelectedItem(nimbusLaF);
297
lafComboBox.setEditable(false);
298
lafComboBox.addActionListener(optionListener);
303
JPanel control1 = new InsetPanel(insets);
304
control1.setBorder(BorderFactory.createTitledBorder("Dialog Type"));
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));
316
fieldWrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
317
fieldWrapper.add(Box.createRigidArea(hpad10));
318
fieldWrapper.add(Box.createRigidArea(hpad10));
319
fieldWrapper.add(customField);
320
control1.add(fieldWrapper);
321
control1.add(Box.createRigidArea(vpad20));
322
control1.add(Box.createGlue());
327
JPanel control2 = new InsetPanel(insets);
328
control2.setBorder(BorderFactory.createTitledBorder("Filter Controls"));
329
control2.setLayout(new BoxLayout(control2, BoxLayout.Y_AXIS));
330
control2.add(Box.createRigidArea(vpad20));
331
control2.add(showAllFilesFilterCheckBox);
332
control2.add(Box.createRigidArea(vpad7));
333
control2.add(showImageFilesFilterCheckBox);
334
control2.add(Box.createRigidArea(vpad4));
335
JPanel checkWrapper = new JPanel();
336
checkWrapper.setLayout(new BoxLayout(checkWrapper, BoxLayout.X_AXIS));
337
checkWrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
338
checkWrapper.add(Box.createRigidArea(hpad10));
339
checkWrapper.add(Box.createRigidArea(hpad10));
340
checkWrapper.add(showFullDescriptionCheckBox);
341
control2.add(checkWrapper);
342
control2.add(Box.createRigidArea(vpad20));
343
control2.add(Box.createGlue());
348
JPanel control3 = new InsetPanel(insets);
349
control3.setBorder(BorderFactory.createTitledBorder("Display Options"));
350
control3.setLayout(new BoxLayout(control3, BoxLayout.Y_AXIS));
351
control3.add(Box.createRigidArea(vpad20));
352
control3.add(setHiddenCheckBox);
353
control3.add(Box.createRigidArea(vpad7));
354
control3.add(useFileViewCheckBox);
355
control3.add(Box.createRigidArea(vpad7));
356
control3.add(useFileSystemViewCheckBox);
357
control3.add(Box.createRigidArea(vpad7));
358
control3.add(accessoryCheckBox);
359
control3.add(Box.createRigidArea(vpad7));
360
control3.add(useEmbedInWizardCheckBox);
361
control3.add(Box.createRigidArea(vpad7));
362
control3.add(useControlsCheckBox);
363
control3.add(Box.createRigidArea(vpad7));
364
control3.add(enableDragCheckBox);
365
control3.add(Box.createRigidArea(vpad20));
366
control3.add(Box.createGlue());
371
JPanel control4 = new InsetPanel(insets);
372
control4.setBorder(BorderFactory.createTitledBorder(
373
"File and Directory Options"));
374
control4.setLayout(new BoxLayout(control4, BoxLayout.Y_AXIS));
375
control4.add(Box.createRigidArea(vpad20));
376
control4.add(justFilesRadioButton);
377
control4.add(Box.createRigidArea(vpad7));
378
control4.add(justDirectoriesRadioButton);
379
control4.add(Box.createRigidArea(vpad7));
380
control4.add(bothFilesAndDirectoriesRadioButton);
381
control4.add(Box.createRigidArea(vpad20));
382
control4.add(singleSelectionRadioButton);
383
control4.add(Box.createRigidArea(vpad7));
384
control4.add(multiSelectionRadioButton);
385
control4.add(Box.createRigidArea(vpad20));
386
control4.add(Box.createGlue());
392
JPanel panel = new JPanel();
393
panel.add(new JLabel("Look and Feel: "));
394
panel.add(lafComboBox);
395
panel.add(showButton);
400
JPanel wrapper = new JPanel();
401
wrapper.setLayout(new BoxLayout(wrapper, BoxLayout.X_AXIS));
403
add(Box.createRigidArea(vpad20));
405
wrapper.add(Box.createRigidArea(hpad10));
406
wrapper.add(Box.createRigidArea(hpad10));
407
wrapper.add(control1);
408
wrapper.add(Box.createRigidArea(hpad10));
409
wrapper.add(control2);
410
wrapper.add(Box.createRigidArea(hpad10));
411
wrapper.add(control3);
412
wrapper.add(Box.createRigidArea(hpad10));
413
wrapper.add(control4);
414
wrapper.add(Box.createRigidArea(hpad10));
415
wrapper.add(Box.createRigidArea(hpad10));
418
add(Box.createRigidArea(vpad20));
420
add(Box.createRigidArea(vpad20));
423
public void actionPerformed(ActionEvent e) {
424
if (customButton.isSelected()) {
425
chooser.setApproveButtonText(customField.getText());
427
if (chooser.isMultiSelectionEnabled()) {
428
chooser.setSelectedFiles(null);
430
chooser.setSelectedFile(null);
433
JComponent accessory = chooser.getAccessory();
434
if (accessory != null) {
435
((FilePreviewer) accessory).loadImage(null);
438
if (useEmbedInWizardCheckBox.isSelected()) {
439
WizardDialog wizard = new WizardDialog(frame, true);
440
wizard.setVisible(true);
445
int retval = chooser.showDialog(frame, null);
446
if (retval == APPROVE_OPTION) {
447
JOptionPane.showMessageDialog(frame, getResultString());
448
} else if (retval == CANCEL_OPTION) {
449
JOptionPane.showMessageDialog(frame,
450
"User cancelled operation. No file was chosen.");
451
} else if (retval == ERROR_OPTION) {
452
JOptionPane.showMessageDialog(frame,
453
"An error occurred. No file was chosen.");
455
JOptionPane.showMessageDialog(frame, "Unknown operation occurred.");
459
private void resetFileFilters(boolean enableFilters,
460
boolean showExtensionInDescription) {
461
chooser.resetChoosableFileFilters();
463
FileFilter jpgFilter = createFileFilter(
464
"JPEG Compressed Image Files",
465
showExtensionInDescription, "jpg");
466
FileFilter gifFilter = createFileFilter("GIF Image Files",
467
showExtensionInDescription, "gif");
468
FileFilter bothFilter = createFileFilter("JPEG and GIF Image Files",
469
showExtensionInDescription, "jpg",
471
chooser.addChoosableFileFilter(bothFilter);
472
chooser.addChoosableFileFilter(jpgFilter);
473
chooser.addChoosableFileFilter(gifFilter);
477
private FileFilter createFileFilter(String description,
478
boolean showExtensionInDescription, String... extensions) {
479
if (showExtensionInDescription) {
480
description = createFileNameFilterDescriptionFromExtensions(
481
description, extensions);
483
return new FileNameExtensionFilter(description, extensions);
486
private String createFileNameFilterDescriptionFromExtensions(
487
String description, String[] extensions) {
488
String fullDescription = (description == null) ? "(" : description
491
fullDescription += "." + extensions[0];
492
for (int i = 1; i < extensions.length; i++) {
493
fullDescription += ", .";
494
fullDescription += extensions[i];
496
fullDescription += ")";
497
return fullDescription;
501
private class WizardDialog extends JDialog implements ActionListener {
503
CardLayout cardLayout;
506
JButton backButton, nextButton, closeButton;
508
@SuppressWarnings("LeakingThisInConstructor")
509
WizardDialog(JFrame frame, boolean modal) {
510
super(frame, "Embedded JFileChooser Demo", modal);
512
cardLayout = new CardLayout();
513
cardPanel = new JPanel(cardLayout);
514
getContentPane().add(cardPanel, BorderLayout.CENTER);
516
messageLabel = new JLabel("", JLabel.CENTER);
517
cardPanel.add(chooser, "fileChooser");
518
cardPanel.add(messageLabel, "label");
519
cardLayout.show(cardPanel, "fileChooser");
520
chooser.addActionListener(this);
522
JPanel buttonPanel = new JPanel();
523
backButton = new JButton("< Back");
524
nextButton = new JButton("Next >");
525
closeButton = new JButton("Close");
527
buttonPanel.add(backButton);
528
buttonPanel.add(nextButton);
529
buttonPanel.add(closeButton);
531
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
533
backButton.setEnabled(false);
534
getRootPane().setDefaultButton(nextButton);
536
backButton.addActionListener(this);
537
nextButton.addActionListener(this);
538
closeButton.addActionListener(this);
541
setLocationRelativeTo(frame);
544
public void actionPerformed(ActionEvent evt) {
545
Object src = evt.getSource();
546
String cmd = evt.getActionCommand();
548
if (src == backButton) {
550
} else if (src == nextButton) {
551
FileChooserUI ui = chooser.getUI();
552
if (ui instanceof BasicFileChooserUI) {
556
((BasicFileChooserUI) ui).getApproveSelectionAction().
557
actionPerformed(null);
561
} else if (src == closeButton) {
563
} else if (APPROVE_SELECTION.equals(cmd)) {
565
} else if (CANCEL_SELECTION.equals(cmd)) {
570
private void back() {
571
backButton.setEnabled(false);
572
nextButton.setEnabled(true);
573
cardLayout.show(cardPanel, "fileChooser");
574
getRootPane().setDefaultButton(nextButton);
575
chooser.requestFocus();
578
private void next() {
579
backButton.setEnabled(true);
580
nextButton.setEnabled(false);
581
messageLabel.setText(getResultString());
582
cardLayout.show(cardPanel, "label");
583
getRootPane().setDefaultButton(closeButton);
584
closeButton.requestFocus();
587
private void close() {
592
public void dispose() {
593
chooser.removeActionListener(this);
597
cardPanel.remove(chooser);
598
chooser.setVisible(true);
604
private String getResultString() {
607
if (chooser.getFileFilter() == null) {
610
filter = chooser.getFileFilter().getDescription();
613
boolean isDirMode = (chooser.getFileSelectionMode() == DIRECTORIES_ONLY);
614
boolean isMulti = chooser.isMultiSelectionEnabled();
617
File[] files = chooser.getSelectedFiles();
618
if (files != null && files.length > 0) {
620
for (File file : files) {
621
path = path + "<br>" + file.getPath();
625
File file = chooser.getSelectedFile();
627
path = "<br>" + file.getPath();
631
path = path.replace(" ", " ");
632
filter = filter.replace(" ", " ");
634
"<html>You chose " + (isMulti ? "these" : "this") + " " + (isDirMode ? (isMulti
635
? "directories" : "directory")
636
: (isMulti ? "files" : "file")) + ": <code>" + path
637
+ "</code><br><br>with filter: <br><code>" + filter;
639
resultString = "Nothing was chosen";
646
private class OptionListener implements ActionListener {
648
public void actionPerformed(ActionEvent e) {
649
JComponent c = (JComponent) e.getSource();
650
boolean selected = false;
651
if (c instanceof JToggleButton) {
652
selected = ((JToggleButton) c).isSelected();
655
if (c == openRadioButton) {
656
chooser.setDialogType(OPEN_DIALOG);
657
customField.setEnabled(false);
659
} else if (c == useEmbedInWizardCheckBox) {
660
useControlsCheckBox.setEnabled(!selected);
661
useControlsCheckBox.setSelected(!selected);
662
chooser.setControlButtonsAreShown(!selected);
663
} else if (c == useControlsCheckBox) {
664
chooser.setControlButtonsAreShown(selected);
665
} else if (c == enableDragCheckBox) {
666
chooser.setDragEnabled(selected);
667
} else if (c == saveRadioButton) {
668
chooser.setDialogType(SAVE_DIALOG);
669
customField.setEnabled(false);
671
} else if (c == customButton || c == customField) {
672
customField.setEnabled(true);
673
chooser.setDialogType(CUSTOM_DIALOG);
675
} else if (c == showAllFilesFilterCheckBox) {
676
chooser.setAcceptAllFileFilterUsed(selected);
677
} else if (c == showImageFilesFilterCheckBox) {
678
resetFileFilters(selected,
679
showFullDescriptionCheckBox.isSelected());
680
showFullDescriptionCheckBox.setEnabled(selected);
681
} else if (c == setHiddenCheckBox) {
682
chooser.setFileHidingEnabled(!selected);
683
} else if (c == accessoryCheckBox) {
685
chooser.setAccessory(previewer);
687
chooser.setAccessory(null);
689
} else if (c == useFileViewCheckBox) {
691
chooser.setFileView(fileView);
693
chooser.setFileView(null);
695
} else if (c == useFileSystemViewCheckBox) {
697
chooser.setFileSystemView(fileSystemView);
700
chooser.setFileSystemView(FileSystemView.getFileSystemView());
702
} else if (c == showFullDescriptionCheckBox) {
703
resetFileFilters(showImageFilesFilterCheckBox.isSelected(),
705
} else if (c == justFilesRadioButton) {
706
chooser.setFileSelectionMode(FILES_ONLY);
707
} else if (c == justDirectoriesRadioButton) {
708
chooser.setFileSelectionMode(DIRECTORIES_ONLY);
709
} else if (c == bothFilesAndDirectoriesRadioButton) {
710
chooser.setFileSelectionMode(FILES_AND_DIRECTORIES);
711
} else if (c == singleSelectionRadioButton) {
713
chooser.setMultiSelectionEnabled(false);
715
} else if (c == multiSelectionRadioButton) {
717
chooser.setMultiSelectionEnabled(true);
719
} else if (c == lafComboBox) {
720
SupportedLaF supportedLaF = ((SupportedLaF) lafComboBox.
722
LookAndFeel laf = supportedLaF.laf;
724
UIManager.setLookAndFeel(laf);
725
SwingUtilities.updateComponentTreeUI(frame);
726
if (chooser != null) {
727
SwingUtilities.updateComponentTreeUI(chooser);
730
} catch (UnsupportedLookAndFeelException exc) {
732
((DefaultComboBoxModel<?>) lafComboBox.getModel()).
733
removeElement(supportedLaF);
741
private class FilePreviewer extends JComponent implements
742
PropertyChangeListener {
744
ImageIcon thumbnail = null;
746
@SuppressWarnings("LeakingThisInConstructor")
747
public FilePreviewer(JFileChooser fc) {
748
setPreferredSize(new Dimension(100, 50));
749
fc.addPropertyChangeListener(this);
752
public void loadImage(File f) {
756
ImageIcon tmpIcon = new ImageIcon(f.getPath());
757
if (tmpIcon.getIconWidth() > 90) {
758
thumbnail = new ImageIcon(
759
tmpIcon.getImage().getScaledInstance(90, -1,
760
Image.SCALE_DEFAULT));
767
public void propertyChange(PropertyChangeEvent e) {
768
String prop = e.getPropertyName();
769
if (SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
771
loadImage((File) e.getNewValue());
778
public void paint(Graphics g) {
779
if (thumbnail != null) {
780
int x = getWidth() / 2 - thumbnail.getIconWidth() / 2;
781
int y = getHeight() / 2 - thumbnail.getIconHeight() / 2;
789
thumbnail.paintIcon(this, g, x, y);
794
public static void main(String[] s) {
796
SwingUtilities.invokeAndWait(new Runnable() {
806
for (LookAndFeelInfo info : UIManager.
807
getInstalledLookAndFeels()) {
808
if (NIMBUS_LAF_NAME.equals(info.getName())) {
809
UIManager.setLookAndFeel(info.getClassName());
813
} catch (Exception ignored) {
816
FileChooserDemo panel = new FileChooserDemo();
818
frame = new JFrame("FileChooserDemo");
819
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
820
frame.getContentPane().add("Center", panel);
822
frame.setVisible(true);
825
} catch (InterruptedException ex) {
826
Logger.getLogger(FileChooserDemo.class.getName()).log(Level.SEVERE,
829
} catch (InvocationTargetException ex) {
830
Logger.getLogger(FileChooserDemo.class.getName()).log(Level.SEVERE,
837
private static class InsetPanel extends JPanel {
841
InsetPanel(Insets i) {
846
public Insets getInsets() {