Solved

Message Box without OK button

Posted on 2003-11-19
8
1,335 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 15

Expert Comment

by:Javatm
ID: 9783604
jimmack, thats okey friend :) we are professionals.
0
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 38
Apps blocked by Java 9 79
JavaFX TableView not displaying correctly 3 16
Adding multiple JVM environments to RedHat 6 7 18
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

775 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