I need to present a information message that needs to be in the screen for 5 seconds, during this time, user can't close the dialog. The specification says clearly that the dialog shouldn't have any button. Is there a way I can use JoptionPane.showMessageDialog in a way that the dialog have no button?
-
1Perhaps creating a JWindow (so it's undecorated and has no "close" button in a title bar, etc.) with a JLabel for your message would meet your needs? Then simply create a Timer to auto-close it. – bobby_light Jan 02 '13 at 18:18
4 Answers
How about this way using showOptionDialog
, maybe not showMessageDialog
, but the same thing when we have no buttons or place to enter text (downfall is it can be closed by user):
JOptionPane.showOptionDialog(null, "Hello","Empty?", JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);
UPDATE
Here is another way, it uses JOptionPane
and JDialog
(even better as it is uncloseable by user):
final JOptionPane optionPane = new JOptionPane("Hello world", JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
final JDialog dialog = new JDialog();
dialog.setTitle("Message");
dialog.setModal(true);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.pack();
//create timer to dispose of dialog after 5 seconds
Timer timer = new Timer(5000, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
dialog.dispose();
}
});
timer.setRepeats(false);//the timer should only go off once
//start timer to close JDialog as dialog modal we must start the timer before its visible
timer.start();
dialog.setVisible(true);

- 36,155
- 13
- 81
- 138
-
@joeyrohan Yes there is always a need for `pack()` on any Window to which components are added. – David Kroukamp Jan 02 '13 at 18:47
-
ooops well yeah there was ;) asked because i thought as we dont in `JOptionPane.showMessageDialog` – joey rohan Jan 02 '13 at 18:55
-
@joeyrohan not a problem... `JOptionPane` is a class which contains methods to show simple messages, they probably call `pack()` from within the class itself. – David Kroukamp Jan 02 '13 at 18:56
Looks like David came up with something to satisfy your requirement of "No buttons".
Having said that, it sounds like you may need to clarify what are you real requirements. Is it really required that the dialog be un-closeable, or is that there is no button to close the dialog? JOptionPane and JDialog have a close button like a standard window.

- 2,036
- 3
- 17
- 21
I used some of the above code to create a "method" to call externally. This can thereby allow multiple popups. My version allows changing the message type, title, display time, screen placement, text and provides examples of changing the font and color of the text message.
/*
* LabelDemo.java contains a method that allow multiple popups. This version allows
* changing the message type, title, display time, screen placement, text and
* provides examples of changing the font and color of the text message.
*/
package components;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.Timer;
/*
* LabelDemo.java
*
*/
public class LabelDemo {
public LabelDemo() {
}
public static void main(String[] args) {
TextDisplayPopup("1st popup", "<html><font size=11 color=blue>information type",
6, 50, 105, JOptionPane.INFORMATION_MESSAGE);
TextDisplayPopup("2nd popup", "<html><font size=15 color=red>ERROR TYPE\n"
+ "<html><font size=6 color=green>2nd line with long sentence for checking"
+ " popup box dynamic sizing.",
10, 240, 240, JOptionPane.ERROR_MESSAGE);
}
public static void TextDisplayPopup(String strTitle, String strText,
int iDelayInSeconds, int iX_Location, int iY_Location, int iMessageType) {
final JOptionPane optionPane = new JOptionPane(strText,
iMessageType, JOptionPane.DEFAULT_OPTION,
null, new Object[]{}, null);
final JDialog dialog = new JDialog();
dialog.setTitle(strTitle);
dialog.setModal(false);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.pack();
dialog.setLocation(iX_Location, iY_Location);
//create timer to dispose of dialog after, iDelayInSeconds, seconds
Timer timer = new Timer(iDelayInSeconds*1000, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent ae) {
dialog.dispose();
}
});
timer.setRepeats(false); // one-time use timer
// timer removes dialog after iDelayInSeconds
timer.start();
dialog.setVisible(true);
System.out.println("end of test");
}
}
Output: 1st popup
I dont think, that you could use a JOptionPane, because if i remember right, they always have one button at least. But you can use for example a splash panel like this or you can use a normal panel and run a thread in it. Like
public class TestFrame extends JFrame implements Runnabel{
private Thread thread;
private CallerClass c; //Class which built this frame
public TestPanel(CallerClass cc){
this.c = cc;
this.thread = null;
//Window can't be closed on (x)
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
//some things you put into your frame
//...
this.setVisible(true);
}
public synchronized void start(){
if (thread == null){
thread = new Thread(this);
thread.start();
}
@Override
public void run() {
try{
Thread.sleep(5000);
}catch(InterruptedException e){ }
this.setVisible(false);
this.c.destroyFrame();
this.stop();
}
}
Where destroyFrame() is e method in your class that build this panel to destroy it (set it to null or something) an you have to create this Frame with SwingUtilities.invokeLater(new TestFrame(this))
if you don't want the rest of your graphics to freeze.

- 930
- 6
- 25
-
-1 this code doesnt look compilable and/or runnable at all, also violates alot of Swing best practices – David Kroukamp Jan 02 '13 at 18:58
-
this isn't the whole code it should only show the important things and I think it will work.. – Alex VII Jan 02 '13 at 19:09
-
2I agree with David, the use of a Thread to control the dialog, with re-synchronisation with the EDT is bad practice, especially when a javax.sing.Timer would suffice – MadProgrammer Jan 02 '13 at 20:56
-
@alexvii : **this isn't the whole code it should only show the important things and I think it will work** Ask yourself the question again, after reading this line `this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);`, the class extends `JPanel`. You still think it can run ? – nIcE cOw Jan 03 '13 at 05:01
-
1@GagandeepBali : thanks this was right I don't know why but I mixed up Frame and Panel it really should be a JFrame.. there you can use the "setDefaultCloseOperation" – Alex VII Jan 03 '13 at 09:16
-
@alexvii : It's OK no worries, mistakes do happen. Just KEEP SMILING :-) – nIcE cOw Jan 03 '13 at 09:27
-
@alexvii : I doubt the edit done to the answer still serves any purpose. The call `Thread.sleep(...)` is blocking the thingy, which shouldn't be blocked - The EDT (Event Dispatch Thread), if you knew about [Initial Thread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) – nIcE cOw Jan 03 '13 at 09:50
-
@GagandeepBali : I'm not sure, but couldn't you handle this if you create this Frame with "SwingUtilities.invokeLater(new TestFrame(this))" in our case? – Alex VII Jan 03 '13 at 10:20