Adding Accessory Panels : JFileChooser « Swing « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Swing » JFileChooser 
14. 74. 6. Adding Accessory Panels

The accessory component can enhance the functionality of the chooser, including previewing an image or document, or playing an audio file.

To respond to file selection changes, the accessory component should attach itself as a PropertyChangeListener to the JFileChooser.

Adding Accessory Panels
import java.awt.Dimension;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
  
class LabelAccessory extends JLabel implements PropertyChangeListener {
  private static final int PREFERRED_WIDTH = 125;
  private static final int PREFERRED_HEIGHT = 100;

  public LabelAccessory(JFileChooser chooser) {
    setVerticalAlignment(JLabel.CENTER);
    setHorizontalAlignment(JLabel.CENTER);
    chooser.addPropertyChangeListener(this);
    setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
  }
  public void propertyChange(PropertyChangeEvent changeEvent) {
    String changeName = changeEvent.getPropertyName();
    if (changeName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
      File file = (File)changeEvent.getNewValue();
      if (file != null) {
        ImageIcon icon = new ImageIcon(file.getPath());
        if (icon.getIconWidth() > PREFERRED_WIDTH) {
          icon = new ImageIcon(icon.getImage().getScaledInstance(
            PREFERRED_WIDTH, -1, Image.SCALE_DEFAULT));
          if (icon.getIconHeight() > PREFERRED_HEIGHT) {
            icon = new ImageIcon(icon.getImage().getScaledInstance(
            -1, PREFERRED_HEIGHT, Image.SCALE_DEFAULT));
          }
        }
        setIcon(icon);
      }
    }
  }
}

public class JFileChooserAddingAccessoryPanels {

  public static void main(String[] a){

    JFileChooser fileChooser = new JFileChooser();

    fileChooser.setAccessory(new LabelAccessory(fileChooser));

    fileChooser.showOpenDialog(null);
  }


}
14. 74. JFileChooser
14. 74. 1. JFileChooser
14. 74. 2. Using JFileChooserUsing JFileChooser
14. 74. 3. Working with File FiltersWorking with File Filters
14. 74. 4. To enable the display of hidden filesTo enable the display of hidden files
14. 74. 5. The JFileChooser supports three selection modes: files only, directories only, and files and directories
14. 74. 6. Adding Accessory PanelsAdding Accessory Panels
14. 74. 7. Custom FileView for Some Java-Related File TypesCustom FileView for Some Java-Related File Types
14. 74. 8. Using a JFileChooser in Your JFrameUsing a JFileChooser in Your JFrame
14. 74. 9. ActionListener for JFileChooser in Your JFrameActionListener for JFileChooser in Your JFrame
14. 74. 10. JFileChooser Selection Option: JFileChooser.APPROVE_OPTION, JFileChooser.CANCEL_OPTIONJFileChooser Selection Option: JFileChooser.APPROVE_OPTION, JFileChooser.CANCEL_OPTION
14. 74. 11. Adding an ActionListener to a JFileChooser to listen for selection of the approval or cancel actions.Adding an ActionListener to a JFileChooser to listen for selection of the approval or cancel actions.
14. 74. 12. Customizing a JFileChooser Look and Feel
ww_w_.j_a_va2_s.__c_o__m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.