Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.awt.Component;

import java.io.File;
import javax.swing.JFileChooser;

import javax.swing.filechooser.FileFilter;

public class Main {
    /**
     * Dialog for choosing file.
     * 
     * @param parent the parent component of the dialog, can be null
     * @return selected file or null if no file is selected
     */
    public static File chooseImageFile(Component parent) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new FileFilter() {
            @Override
            public String getDescription() {
                return "Supported image files(JPEG, PNG, BMP)";
            }

            @Override
            public boolean accept(File f) {
                String name = f.getName();
                boolean accepted = f.isDirectory() || name.endsWith(".jpg") || name.endsWith(".jpeg")
                        || name.endsWith(".png") || name.endsWith(".bmp");
                return accepted;
            }
        });

        fileChooser.showOpenDialog(parent);
        return fileChooser.getSelectedFile();
    }
}