Solved

Message Box without OK button

Posted on 2003-11-19
8
1,333 Views
Last Modified: 2012-06-21
Hi,

I need a message box similar to JOptionPane.showMessageDialog() but without the OK button (I want the program to close it.) Is there anyway to do this without coding a custom dialog?

Thanks,

Wall
0
Comment
Question by:wallway
8 Comments
 
LVL 15

Expert Comment

by:Javatm
ID: 9783519
Use a JDailog :

JDaiolg g = new JDialog();
0
 
LVL 15

Accepted Solution

by:
jimmack earned 50 total points
ID: 9783552
What is wrong with creating your own dialog?

                JDialog myDialog = new JDialog((JFrame)null, "Title");
                myDialog.getContentPane().add(new JLabel("My message", SwingConstants.CENTER));
                myDialog.pack();
                myDialog.show();
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9783558
Hi Javatm.  Perhaps I shouldn't have taken the time to test my layout ;-)
0
 
LVL 15

Expert Comment

by:Javatm
ID: 9783604
jimmack, thats okey friend :) we are professionals.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:monkesdb
ID: 9784394
if you just want there to be no cancel button then you should be able to do it in one line.


JDialog dialog = new JOptionPanel("Message Goes Here", JOptionPanel.PLAIN_MESSAGE, CANCEL_OPTION).createDialog(null, "Title");


i've not tried it so you might have to "dialog.show()" or something to see it but that should do the trick.

if this doesn't work please let me know.
0
 

Author Comment

by:wallway
ID: 9788539
Thanks for the replies.


JDialog myDialog = new JDialog((JFrame)null, "Title");
myDialog.getContentPane().add(new JLabel("My message", SwingConstants.CENTER));
myDialog.pack();
myDialog.show();

This kindof works, except it shows in the top left hand corner, is too small, and doesn't show the message. I will have to do more configuration to get it to look like a normal information box.

JDialog dialog = new JOptionPanel("Message Goes Here", JOptionPanel.PLAIN_MESSAGE, CANCEL_OPTION).createDialog(null, "Title");

This one kinda works as well, however the buttons are still showing. But it is the right size and displays in the center as I need it to.

Wall


0
 
LVL 15

Expert Comment

by:jimmack
ID: 9788574
To get the dialog to show in the centre of your frame, you need to supply the parent as the first parameter in the constructor, eg:

JFrame myFrame = new JFrame("Frame title");

JDialog myDialog = new JDialog(myFrame, "Title");

To resize it, just use:

myDialog.setSize(new Dimension(200, 100));  //  or whatever size you need.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9788581
The setSize() should replace the pack() method ;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now