# 3
Check out my StandardDialog class and the static showStandardDialog method. It will wait until the dialog has closed before continuing
You are welcome to use and modify this code, but please don't change the package or take credit for it as your own work
Also, you may want to replace the parent class (currently modelOnParentDialog) with just JDialog... It won't affect anything.
tjacobs.ui.StandardDialog.java
================
package tjacobs.ui;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import javax.swing.JDialog;
/**
* Standard Dialog is a basic dialog with Apply/Ok/Cancel buttons
* To implement it, you have to implement apply() which gets run
* when the user hits either apply or ok, and showApplyButton(),
* which says whether or not to show the apply button
*/
//public abstract class StandardDialog extends JDialog {
public abstract class StandardDialog extends ModalOnParentDialog {
public StandardDialog() throws HeadlessException {
super();
_init();
}
public StandardDialog(Dialog owner) throws HeadlessException {
super(owner);
_init();
}
public StandardDialog(Dialog owner, boolean modal) throws HeadlessException {
super(owner, modal);
_init();
}
public StandardDialog(Frame owner) throws HeadlessException {
super(owner);
_init();
}
public StandardDialog(Frame owner, boolean modal) throws HeadlessException {
super(owner, modal);
_init();
}
public StandardDialog(Dialog owner, String title) throws HeadlessException {
super(owner, title);
_init();
}
public StandardDialog(Dialog owner, String title, boolean modal)
throws HeadlessException {
super(owner, title, modal);
_init();
}
public StandardDialog(Frame owner, String title) throws HeadlessException {
super(owner, title);
_init();
}
public StandardDialog(Frame owner, String title, boolean modal)
throws HeadlessException {
super(owner, title, modal);
_init();
}
public StandardDialog(Dialog owner, String title, boolean modal,
GraphicsConfiguration gc) throws HeadlessException {
super(owner, title, modal, gc);
_init();
}
public StandardDialog(Frame owner, String title, boolean modal,
GraphicsConfiguration gc) {
super(owner, title, modal, gc);
_init();
}
/*
public void setVisible(boolean b) {
super.setVisible(b);
if (b) StandardFrame.Watch(this); else StandardFrame.RemoveWatch(this);
}
*/
public void setMainContent(Component c) {
mContent.add(c, BorderLayout.CENTER);
mMainPane = c;
}
protected void _init() {
mContent = getContentPane();
mContent.setLayout(new BorderLayout());
mButtonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
mContent.add(mButtonPane, BorderLayout.SOUTH);
mButtonListener = new ButtonListener();
if (showApplyButton()) {
mApply = new JButton("Apply");
mButtonPane.add(mApply);
mApply.addActionListener(mButtonListener);
}
mOk = new JButton("OK");
mButtonPane.add(mOk);
mOk.addActionListener(mButtonListener);
mCancel = new JButton("Cancel");
mButtonPane.add(mCancel);
mCancel.addActionListener(mButtonListener);
}
public Component getMainPane() {
return mMainPane;
}
public void ok (){
mCancelState = OK;
apply();
close();
}
public void cancel() {
mCancelState = CANCEL;
close();
}
public void close () {
dispose();
}
public abstract boolean showApplyButton();
public abstract void apply();
public void dispose() {
if (mCancelState != OK) {
mCancelState = CANCEL;
}
synchronized(this) {
notifyAll();
}
super.dispose();
}
protected class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent ae) {
JButton b = (JButton) ae.getSource();
if (b == mApply) {
apply();
}
else if (b == mOk) {
ok();
}
else if (b == mCancel) {
cancel();
}
}
}
public int getCancelState() {
return mCancelState;
}
public static int showStandardDialog(StandardDialog sd) {
Window w = sd.getOwner();
if (w == null) {
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
Dimension ps = sd.getPreferredSize();
return showStandardDialog(sd, (d.width - ps.width) / 2, (d.height - ps.height) / 2);
}
else {
Point p = w.getLocation();
Dimension d = w.getSize();
return showStandardDialog(sd, p.x + 100, p.y + 20);
}
}
public static int showStandardDialog(final StandardDialog sd, int x, int y) {
sd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
sd.cancel();
}
});
sd.setLocation(x, y);
sd.pack();
sd.setVisible(true);
synchronized(sd) {
try {
sd.wait();
}
catch (InterruptedException ex) {}
}
return sd.getCancelState();
}
public static final int OK = 1;
public static final int SHOWN = 0;
public static final int CANCEL = -1;
private int mCancelState = 0;
protected boolean mBlockInCode;
protected Container mContent;
protected Component mMainPane;
protected JButton mApply;
protected JButton mOk;
protected JButton mCancel;
protected JPanel mButtonPane;
protected ActionListener mButtonListener;
}