Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDialog;
import javax.swing.JFileChooser;

public class Main {
    /**
     * Displays the given file chooser. Utility method for avoiding of memory leak in JDK
     * 1.3 {@link javax.swing.JFileChooser#showDialog}.
     *
     * @param chooser the file chooser to display.
     * @param parent the parent window.
     * @param approveButtonText the text for the approve button.
     *
     * @return the return code of the chooser.
     */
    public static final int showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText) {
        if (approveButtonText != null) {
            chooser.setApproveButtonText(approveButtonText);
            chooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG);
        }

        Frame frame = (parent instanceof Frame) ? (Frame) parent
                : (Frame) javax.swing.SwingUtilities.getAncestorOfClass(java.awt.Frame.class, parent);
        String title = chooser.getDialogTitle();

        if (title == null) {
            title = chooser.getUI().getDialogTitle(chooser);
        }

        final JDialog dialog = new JDialog(frame, title, true);
        dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        Container contentPane = dialog.getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(chooser, BorderLayout.CENTER);
        dialog.pack();
        dialog.setLocationRelativeTo(parent);
        chooser.rescanCurrentDirectory();

        final int[] retValue = new int[] { javax.swing.JFileChooser.CANCEL_OPTION };

        ActionListener l = new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                if (ev.getActionCommand() == JFileChooser.APPROVE_SELECTION) {
                    retValue[0] = JFileChooser.APPROVE_OPTION;
                }

                dialog.setVisible(false);
                dialog.dispose();
            }
        };

        chooser.addActionListener(l);
        dialog.show();

        return (retValue[0]);
    }
}