Solved

Message Box without OK button

Posted on 2003-11-19
8
1,330 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
Comment Utility
Use a JDailog :

JDaiolg g = new JDialog();
0
 
LVL 15

Accepted Solution

by:
jimmack earned 50 total points
Comment Utility
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
Comment Utility
Hi Javatm.  Perhaps I shouldn't have taken the time to test my layout ;-)
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
jimmack, thats okey friend :) we are professionals.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:monkesdb
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The setSize() should replace the pack() method ;-)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 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

10 Experts available now in Live!

Get 1:1 Help Now