Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class Main {
    public static void main(String[] args) {
        // Create a file filter to show only a directory or .doc files
        FileFilter filter = new FileFilter() {
            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                }

                String fileName = f.getName().toLowerCase();
                if (fileName.endsWith(".doc")) {
                    return true;
                }

                return false; // Reject any other files
            }

            @Override
            public String getDescription() {
                return "Word Document";
            }
        };

        // Set the file filter
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(filter);

        int returnValue = fileChooser.showDialog(null, "Attach");
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            // Process the file
        }

    }
}