JFileChooser: setFileView(FileView fileView) : JFileChooser « javax.swing « Java by API






JFileChooser: setFileView(FileView fileView)

  

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.io.File;

import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileView;

public class MainClass {

  public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    FileView view = new JavaFileView();
    fileChooser.setFileView(view);
    int status = fileChooser.showOpenDialog(null);
    if (status == JFileChooser.APPROVE_OPTION) {
      File selectedFile = fileChooser.getSelectedFile();
      System.out.println(selectedFile.getParent());
      System.out.println(selectedFile.getName());
    } else if (status == JFileChooser.CANCEL_OPTION) {
      System.out.println("JFileChooser.CANCEL_OPTION");
    }
  }
}
class JavaFileView extends FileView {
  Icon javaIcon = new MyIcon(Color.BLUE);

  Icon classIcon = new MyIcon(Color.GREEN);

  Icon htmlIcon = new MyIcon(Color.RED);

  Icon jarIcon = new MyIcon(Color.PINK);

  public String getName(File file) {
    String filename = file.getName();
    if (filename.endsWith(".java")) {
      String name = filename + " : " + file.length();
      return name;
    }
    return null;
  }

  public String getTypeDescription(File file) {
    String typeDescription = null;
    String filename = file.getName().toLowerCase();

    if (filename.endsWith(".java")) {
      typeDescription = "Java Source";
    } else if (filename.endsWith(".class")) {
      typeDescription = "Java Class File";
    } else if (filename.endsWith(".jar")) {
      typeDescription = "Java Archive";
    } else if (filename.endsWith(".html") || filename.endsWith(".htm")) {
      typeDescription = "Applet Loader";
    }
    return typeDescription;
  }

  public Icon getIcon(File file) {
    if (file.isDirectory()) {
      return null;
    }
    Icon icon = null;
    String filename = file.getName().toLowerCase();
    if (filename.endsWith(".java")) {
      icon = javaIcon;
    } else if (filename.endsWith(".class")) {
      icon = classIcon;
    } else if (filename.endsWith(".jar")) {
      icon = jarIcon;
    } else if (filename.endsWith(".html") || filename.endsWith(".htm")) {
      icon = htmlIcon;
    }
    return icon;
  }
}

class MyIcon implements Icon {
  Color myColor;

  public MyIcon(Color myColor) {
    this.myColor = myColor;
  }

  public int getIconWidth() {
    return 16;
  }

  public int getIconHeight() {
    return 16;
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    g.setColor(myColor);
    g.drawRect(0, 0, 16, 16);
  }
}
           
         
    
  








Related examples in the same category

1.JFileChooser.APPROVE_OPTION
2.JFileChooser.CANCEL_OPTION
3.JFileChooser.DIRECTORIES_ONLY
4.JFileChooser.DIRECTORY_CHANGED_PROPERTY
5.JFileChooser.ERROR_OPTION
6.JFileChooser.SELECTED_FILE_CHANGED_PROPERTY
7.JFileChooser.SELECTED_FILES_CHANGED_PROPERTY
8.JFileChooser: addActionListener(ActionListener l)
9.JFileChooser: addChoosableFileFilter(FileFilter filter)
10.JFileChooser: addPropertyChangeListener(PropertyChangeListener listener)
11.JFileChooser: createDialog(Component parent)
12.JFileChooser: getCurrentDirectory()
13.JFileChooser: getFileSystemView()
14.JFileChooser: getSelectedFile()
15.JFileChooser: getTypeDescription(File f)
16.JFileChooser: isFileHidingEnabled()
17.JFileChooser: setAccessory(JComponent newAccessory)
18.JFileChooser: setApproveButtonMnemonic(char mnemonic)
19.JFileChooser: setApproveButtonText(String approveButtonText)
20.JFileChooser: setApproveButtonToolTipText(String toolTipText)
21.JFileChooser: setControlButtonsAreShown(boolean b)
22.JFileChooser: setCurrentDirectory(File dir)
23.JFileChooser: setFileFilter(FileFilter filter)
24.JFileChooser: setFileHidingEnabled(boolean b)
25.JFileChooser: setFileSelectionMode(int mode)
26.JFileChooser: setMultiSelectionEnabled(boolean b)
27.JFileChooser: setSelectedFile(File file)
28.JFileChooser: showOpenDialog(Component parent)