How to create a sophisticated modal interface returning a value?
Hi,
I am trying to create an interface in Java with sophisticated features to allow a user to select a date value. It has to be modal, that is, when the user clicks on a button, the frame opens, the user selects a value and the value must be retrieved by the calling frame.
JDialog is not OK, because it does not allow to design a sophisticated interface. The customization possibilities of JDialog are not enough for my needs. I need to design a full screen with several tables, combox boxes, etc...
How should I proceed? What type of object should I use / inherit of? What mechanism should I use to retrieve the value selected by the user? I can see (in http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html) that JOptionPane allows to retrieve a value from a modal box. That is exactly what I need, but for a much more sophisticated interface.
Any ideas? Thanks !
[914 byte] By [
Jrma] at [2007-11-27 10:22:49]

Why is JDialog not good enough? You can make it as complex as any other widget, if you want to
> JDialog is not OK, because it does not allow to design a sophisticated interface.
Why do you think that?
> The customization possibilities of JDialog are not enough for my needs. I need to design a full screen with several tables, combox boxes, etc...
You can do that with JDialog.
~
I agree a JDialog can be made to do whatever you want it to, but if you wanna use a jframe you can just extend one. then once instantiated it'll hold whatever fields you give it between times it is displayed.
Cogsya at 2007-7-28 17:18:16 >

Well, can you tell me if there is any way I can have full control of the design of the screen? Because, when I create a class inheriting from JDialog, NetBeans does not offer me the possibility to work on the design of the screen...
Do you know of any code examples? Thanks!
Jrma at 2007-7-28 17:18:16 >

> Well, can you tell me if there is any way I can have
> full control of the design of the screen? Because,
> when I create a class inheriting from JDialog,
> NetBeans does not offer me the possibility to work on
> the design of the screen...
>
> Do you know of any code examples? Thanks!
That's because NetBeans is rubbish (ducks).
Rather than extending JDialog, design your complex UI on a JPanel. Always a good idea, anyway. Then, you can simply set this JPanel as the content pane of a JDialog, or a JFrame, or a JApplet, or whatever you need
http://java.sun.com/javase/6/docs/api/javax/swing/JDialog.html
http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
A JDialog is a Container.
http://java.sun.com/javase/6/docs/api/java/awt/Container.html
~
> I agree a JDialog can be made to do whatever you want
> it to, but if you wanna use a jframe you can just
> extend one. then once instantiated it'll hold
> whatever fields you give it between times it is
> displayed.
Please don't answer to any more threads...we have enough people spreading misinformation already.
> I agree a JDialog can be made to do whatever you want
> it to, but if you wanna use a jframe you can just
> extend one. then once instantiated it'll hold
> whatever fields you give it between times it is
> displayed.
You extend JFrame? Why? Don't you know what inheritance is for?
Hi georgesmc (and others),
If I understand you well, I should have a JPanel as following:
public class MySophisticatedInterface extends javax.swing.JPanel {
// ...code for my sophisticated interface
}
and in the "calling" JFrame, I should have:
private void MyCallingButtonMouseReleased(java.awt.event.MouseEvent evt) {
MySophisticatedInterface MSI = new MySophisticatedInterface(...);
JDialog MyJDialog = new JDialog(this, true);// True indicates modal behavior
MyJDialog.setContentPane(MSI);
// Then, how should I call MYJDialog to retrieve the value from MySophisticatedInterface?
// Is there a special method I should implement / override?
}
Thanks !!!
Jrma at 2007-7-28 17:18:16 >

> // Then, how should I call MYJDialog to retrieve
> the value from MySophisticatedInterface?
Reply #6, to wit:
http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
Code examples included.
~
Hi YawMark,
I have re-read the link you mention in details again, but I don't see how it explains how I can solve my issue.
I searched a little more and I can see that JOptionPane has a getValue() and setValue() method, but I could not find any code example explaining i) how to establish a proper relationship between the JOptionPane and MyJDialog,
and ii) how the MyJDialog can pass the value selected by the user in MySophisticatedInterface to the JOptionPane via the setValue()...
Do I have to play with even listener to pass my value back to my Calling button? Can you (or anybody) explain the magic trick I am missing?
Many thanks for your responses so far !!!
Jrma at 2007-7-28 17:18:16 >

in your example, MSI would need to be declared and instantiated outside of the mouse event so that it persists between times it is displayed, if that is what you want. otherwise, reading from it after the dialog has returned will work fine.
The mechanism for retrieving the data from MSI could be as simple as a get method in your MySophisticatedInterface class.
Cogsya at 2007-7-28 17:18:16 >

If your problem is using the Netbeans GUI designer, why not create your fancy interface on a JFrame, then simply copy the GUI setup code netbeans generates into a JDialog.
The modal apparatus in JDialog is far more complex than you might expect (and the specialised classes involved are not public, and use package level methods you can't access even if you copy them).
I had a serious look at the source of JDialog once, with a view to doing my own modal stuff. Too much like hard work.
> I had a serious look at the source of JDialog once,
> with a view to doing my own modal stuff. Too much
> like hard work.
It's really not that hard, as long as you realize that calling setVisible(true) on a modal dialog suspends the current event pump and starts a new one. Once the dialog is set invisible or disposed, the original pump resumes.
So, assuming that you've created a class to manage the dialog, you'd implement a show() method like the following (hasn't been compiled, may contain syntax errors):
// note: this class is responsible for building and displaying
//a dialog; it doesn't need to inherit from JDialog, and
//becomes a lot cleaner if you don't inherit
public class MyDialogManager
{
private JDialog _myDialog;
private JTextField _field;
public MyDialogManager(JFrame owner, String title)
{
_myDialog = new JDialog(owner, true, title);
// build out the dialog here, including text field
}
public String showMyDialog()
{
_myDialog.setVisible(true);
// this thread is suspended until the dialog is closed
return _field.getText();
}
}
>
> It's really not that hard, as long as you realize
> that calling setVisible(true) on a modal dialog
> suspends the current event pump and starts a new one.
> Once the dialog is set invisible or disposed, the
> original pump resumes.
I know. The problem I saw was that this special event pump rerouted those events it didn't block back into the original processing chain, using method calls that were package local.
So you couldn't just rip off the code for JDialog's event pump, you'd have to recreate fairly substantial parts of the GUI core.
> I know. The problem I saw was that this special event
> pump rerouted those events it didn't block back into
> the original processing chain, using method calls
> that were package local.
>
> So you couldn't just rip off the code for JDialog's
> event pump, you'd have to recreate fairly substantial
> parts of the GUI core.
OK, I think I understand what you're saying. However, I don't believe that you need to rip this code for most cases, but simply rely on JDialog to do its thing. Have done that for several apps and never seen a problem, but if you know of any I'd be interested.
I am nearly through it, but I am facing a last issue... Calling setVisible(false) in my MySophisticatedInterface (i.e., JPanel) does not actually close MyJDialog.
How can I close MyJDialog from MySophisticatedInterface which has been assigned with MyJDialog.setContentPane(MSI)?
Thanks!
Jrma at 2007-7-28 17:18:21 >

At last, I managed to craft a working solution. The sophisticated interface needs to implement (at least) the following:
public class MySophisticatedInterface extends javax.swing.JPanel {
private Date TheResult;
private JDialog TheParentJDialog;
public MySophisticatedInterface (JDialog TheJDialog) {
TheParentJDialog = TheJDialog;
}
public Date GetResult() {
return TheResult;
}
public RegisterResultAndClose() {
TheResult = ...;
TheParentDialog.dispose();
}
// Remaining code for sophisticated interface...
}
and in the "calling" JFrame, one should have something like:
private void MyCallingButtonMouseReleased(java.awt.event.MouseEvent evt) {
JDialog MyJDialog = new JDialog(this, true); // True indicates modal behavior
MySophisticatedInterface MSI = new MySophisticatedInterface(MyJDialog);
MyJDialog.setContentPane(MSI);
MyJDialog.pack();
MyJDialog.setVisible(true);
Date RetrievedResult = MSI.GetResult();
// ...
}
It works (I am not claiming that there is no better solution) !!! Thank you to everyone for their input...
Jrma at 2007-7-28 17:18:21 >

