Mega Code Archive

 
Categories / Java Tutorial / Swing
 

Custom FileView for Some Java-Related File Types

FileView is the area where all the file names are listed. import java.io.File; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.filechooser.FileView; class JavaFileView extends FileView {   Icon jarIcon = new ImageIcon("yourFile.gif");   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") || filename.endsWith(".class")) {       typeDescription = "Java Source";     }     return typeDescription;   }   public Icon getIcon(File file) {     if (file.isDirectory()) {       return null;     }     Icon icon = null;     String filename = file.getName().toLowerCase();     if (filename.endsWith(".java") || filename.endsWith(".class")) {       icon = jarIcon;     }     return icon;   } } public class UsingFileView {   public static void main(String[] a){     JFileChooser fileChooser = new JFileChooser();     fileChooser.setFileView(new JavaFileView());     fileChooser.showOpenDialog(null);   } }