October 7th, 1999, 10:21 AM
Hi, is there an easy way (using AWT) to create windows-like message boxes that can be launched from a java app/applet?
I just need a small window with two or three option buttons, that I can get a return code from to see which button the user pressed.
Thanks in advance for any help you can give me,
Tom.
poochi
October 7th, 1999, 11:38 AM
check JOptionPane Class( methods : showXXXDialog() )
> I just need a small window with two or three option buttons, that I can get a return code from to see which button the user pressed.
For that you need to extend JOptionPane class and have membervariable in it ..
Check my very first post in my profile.
Poochi..
amaresh
October 7th, 1999, 12:23 PM
You can use JOptionPane as poochi suggested. However, that's not AWT, but part of swing package. If you don't mind using swing, you can use JOptionPane directly. However, for some reason, if you don't want to use swing classes, you have to write your own message box class, which also is not very difficult to do.There are many ways to do that and the complexity in each one of them depends on how elegant and nice looking you want the message box to be.
I am just mentioning one such procedure that doesn't take much time to implement: Create the dialog box. Provide a function inside your dialog box class (let's call it MessageBox) for accepting the number of buttons, options, icons if any and the string to be displayed.
Set the layout of your dialog to be border layout, and put three panes - one at north,one at south and one at center. Set the layout of all the panes to be flow layout. The north pane is intended to carry the icon specific to the message, the south pane intended to carry the buttons, the centre pane intended to carry the text to be displayed.
Put a Text Area in the central pane.
Now, provide a function that takes the String, icon name and button option. Inside this function write the code you need, to put the icons in the northern pane of the dialog box, string inside a text area in the central pane, buttons in the southern pane of the dialog box. All these Icon and button creation needs to be done at run time, inside the function. (However, you can also create them initially and later on hide / show depending on the function arguement. Still, you will need to align the buttons to the centre of the dialog's southern pane).
However, this procedure will give you a "kind of" message box, but may not look professional because of the "text area" in the middle with scroll bars. Also, you need to take care of positioning the icon in the northern pane of the dialog (by default it will be at centre, because of flow layout).
If you want to avoid text area in the central pane, you need to break the string in to suitable segments, and put each of them as saperate labels one below the other. This involves slightly more work, but will give you a better looking message box. (The text area with scroll bars at the centre of the dialog doesn't look all that nice..Does it?)
You can refer the book "Java in a Nut Shell" by David Flanagen (O'Reilly publications). There you will find the code for this kind of message box. You can put this as a bean also. In my opinion, that's a better way to do this.
If you want to use this class inside an applet, you need to pack this message box class in a jar file with other necessary classes and put that in "ARCHIVE" tag of your html page.
Let me know if this is not clear.
Thanks,
Amaresh
October 8th, 1999, 05:00 AM
Thanks both of you for your help.
I'm (obviously!) new to Java so I'm going to try it both ways to help with my education.
Thanks again, I appreciate it.
Tom.