public class BaseDialog extends JDialog {
protected BaseDialog(Frame owner, String title) {
super(owner, title, true);
}
protected boolean dataIsValid() { return true; }
protected void setModelFromView() {}
protected JButton stdOKButton() {
JButton button = new JButton("OK");
button.setMnemonic('O');
getRootPane().setDefaultButton(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (dataIsValid()) {
setModelFromView();
setVisible(false);
}
}
});
return button;
}
protected JButton stdCancelButton() {
JButton button = new JButton("Cancel");
button.setMnemonic('C');
button.setDefaultCapable(false);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
return button;
}
protected void stdLayout(Component widgetBox,
JButton[] buttons) {
// create button box
Box buttonBox = Box.createHorizontalBox();
buttonBox.add(Box.createHorizontalGlue());
for (int i = 0; i < buttons.length; i++) {
if (i > 0) {
buttonBox.add(createHorizontalStrut(5));
}
buttonBox.add(buttons[i]);
}
matchComponentSize(buttons);
// create main box
Box mainBox = Box.createVerticalBox();
mainBox.add(widgetBox);
mainBox.add(createVerticalStrut(17));
mainBox.add(buttonBox);
// assemble std dialog
Container cont = getContentPane();
cont.add(createVerticalStrut(13), NORTH);
cont.add(createVerticalStrut(11), SOUTH);
cont.add(createHorizontalStrut(11), EAST);
cont.add(createHorizontalStrut(12), WEST);
cont.add(mainBox);
pack();
}
protected void matchComponentSize(JComponent[]
comps) {
Dimension size = new Dimension(0, 0);
for (int i = 0; i < comps.length; i++) {
if (comps[i].isVisible()) {
Dimension c = comps[i].getPreferredSize();
size.width = (size.width > c.width)?
size.width : c.width;
size.height = (size.height > c.height)?
size.height : c.height;
}
}
for (int i = 0; i < comps.length; i++) {
comps[i].setPreferredSize(size);
comps[i].setMinimumSize(size);
comps[i].setMaximumSize(size);
}
}
protected Component createHorizontalStrut(int w) {
return Box.createRigidArea(new Dimension(w, 0));
}
protected Component createVerticalStrut(int h) {
return Box.createRigidArea(new Dimension(0, h));
}
}
|
|
public class MessageDialog extends BaseDialog {
private JPanel panel = new JPanel();
private boolean cancel = true;
public MessageDialog(Frame owner) {
super(owner, "This Space Available");
createComponents();
layoutComponents();
setLocationRelativeTo(owner);
}
public boolean canceled() {
return cancel;
}
protected void setModelFromView() {
cancel = false;
}
private void createComponents() {
panel.add(new JLabel("<html><center><h1>"
+ "This Space Available<br>"
+ "Phone 541-601-8583</h1>"));
panel.setBorder(BorderFactory
.createLineBorder(Color.BLACK));
}
private void layoutComponents() {
JButton[] buttons = { stdOKButton(),
stdCancelButton() };
stdLayout(panel, buttons);
}
}
|
Here's how you fire it up!
private void doMessage() {
MessageDialog dialog = new MessageDialog(frame);
dialog.setVisible(true); // this blocks
if (!dialog.canceled()) {
phone("541-601-8583");
}
}
|
|