JavaFileView.java Source code

Java tutorial

Introduction

Here is the source code for JavaFileView.java

Source

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);
    }
}