About custom dialogs

Hi.

I have some application which connects to database. In main method (after initializing splashscreen, and before opening main window) I try to open properties file in user home directory where are defined parameters to database connection. When the file is not fount I catch FileNotFoundException and in catch block I want show a dialog to a user with four text fields and let him to fill database parameters. But I cannot use JOptionPane class because I want four text fields and also I want return four String values (or one String value, but I want my application to be convenient to user so I want four text fields). I thought that I can create my own JFrame and use method invokeAndWait, but I don't know (and cannot find) how to close this window programatically and this philosophy is quite different from just invoking one static method and wait until it return result. Do you ever encounter this problem? And how did you solve it?

Thx very much.

[977 byte] By [paulie.xa] at [2007-11-26 17:39:47]
# 1
Have a look at http://forum.java.sun.com/thread.jspa?threadID=5133979&tstart=0
Joerg22a at 2007-7-9 0:07:56 > top of Java-index,Desktop,Core GUI APIs...
# 2
OK, thanks. But I still don't know how to use it like static methods from JOptionPane. How can I get back values from components and close JOptionPane object and continue on initializing ?Sorry, I truly thought about it :(...
paulie.xa at 2007-7-9 0:07:56 > top of Java-index,Desktop,Core GUI APIs...
# 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;

}

tjacobs01a at 2007-7-9 0:07:56 > top of Java-index,Desktop,Core GUI APIs...
# 4
I thik that this must be easier to do... Please have a look at my post at http://forum.java.sun.com/thread.jspa?messageID=9485380
paulie.xa at 2007-7-9 0:07:56 > top of Java-index,Desktop,Core GUI APIs...