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.Dimension;

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

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;

import javax.swing.Action;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JComponent;
import javax.swing.JDialog;

import javax.swing.JPanel;
import javax.swing.JRootPane;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Main {
    /**
     * Initialises the {@link JDialog} for the {@link JComponent}.
     * 
     * @param dialog
     * @param component
     * @param parentComponent
     */
    private static void initDialog(final JDialog dialog, final JComponent component,
            final Component parentComponent) {
        dialog.setResizable(true);
        dialog.setComponentOrientation(component.getComponentOrientation());
        Container contentPane = dialog.getContentPane();

        contentPane.setLayout(new BorderLayout());
        contentPane.add(component, BorderLayout.CENTER);

        final int buttonWidth = 75;

        final JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(2, 4, 4, 4));

        buttonPanel.add(Box.createHorizontalGlue());

        @SuppressWarnings("serial")
        final Action closeAction = new AbstractAction("Close") {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        };

        final JButton button = new JButton(closeAction);
        fixWidth(button, buttonWidth);
        buttonPanel.add(button);

        contentPane.add(buttonPanel, BorderLayout.SOUTH);

        if (JDialog.isDefaultLookAndFeelDecorated()) {
            boolean supportsWindowDecorations = UIManager.getLookAndFeel().getSupportsWindowDecorations();
            if (supportsWindowDecorations) {
                dialog.setUndecorated(true);
                component.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
            }
        }
        dialog.pack();
        dialog.setLocationRelativeTo(parentComponent);
        WindowAdapter adapter = new WindowAdapter() {
            //         private boolean gotFocus = false;
            public void windowClosing(WindowEvent we) {
                fireAction(we.getSource(), closeAction, "close");
            }
        };
        dialog.addWindowListener(adapter);
        dialog.addWindowFocusListener(adapter);
    }

    /**
     * Fixes the width of the component but maintaining it old height.
     *
     * @param comp
     * @param width
     */
    public static void fixWidth(JComponent comp, int width) {
        comp.setPreferredSize(new Dimension(width, comp.getPreferredSize().height));
        comp.setMaximumSize(comp.getPreferredSize());
    }

    /**
     * Fires a {@link ActionListener} using zero modifiers. This is useful for
     * firing {@link ApplicationAction}s to show blocking dialog.
     *
     * @param source the source of the event.
     * @param listener the listener to fire.
     * @param command a string that may specify a command (possibly one 
     * of several) associated with the event.
     * @see #fireAction(Object, ActionListener, String, int)
     */
    public static void fireAction(Object source, ActionListener listener, String command) {
        fireAction(source, listener, command, 0);
    }

    /**
     * Fires a {@link ActionListener}. This is useful for firing 
     * {@link ApplicationAction}s to show blocking dialog.
     *
     * @param source the source of the event.
     * @param listener the listener to fire.
     * @param command a string that may specify a command (possibly one 
     * of several) associated with the event.
     * @param modifiers the modifier keys held down during this action.
     * @see ActionEvent#ActionEvent(Object, int, String, long, int)
     */
    public static void fireAction(final Object source, final ActionListener listener, final String command,
            final int modifiers) {
        fireAction(listener, new ActionEvent(source, ActionEvent.ACTION_PERFORMED, command,
                System.currentTimeMillis(), modifiers));
    }

    /**
     * Fires a {@link ActionListener}. This is useful for firing 
     * {@link ApplicationAction}s to show blocking dialog. This can be called
     * on and off the EDT.
     *
     * @param listener the listener to fire.
     * @param evt the action event to fire.
     * @param modifiers the modifier keys held down during this action.
     * @see ActionEvent#ActionEvent(Object, int, String, long, int)
     */
    public static void fireAction(final ActionListener listener, final ActionEvent evt) {
        if (!SwingUtilities.isEventDispatchThread()) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    fireAction(listener, evt);
                }
            });
            return;
        }
        listener.actionPerformed(evt);
    }
}