4

I had used JDialog box to display an Error message ,

JOptionPane.showMessageDialog(
        null, "ErrorMsg", "Failure", JOptionPane.ERROR_MESSAGE);

but the condition is the message box should always be on the foreground so that the user is bound to proceed and doesn't misses it.

Hence i used a JFrame instead and used setAlwaysOnTop(true) to display the message.i used JLabel and JButton and I made it appear as JDialog by the help of the below link

How do I remove the maximize and minimize buttons from a JFrame?

I want to add a Error message symbol in the above frame as we get when we use JOptionPane.ERROR_MESSAGE type in a dialog.

I dont want to add an Image Icon in the JFrame. I am extending my class from JDialog. Is it possible to use MessageType in my frame?

Community
  • 1
  • 1
Shaik Md
  • 597
  • 4
  • 8
  • 22

2 Answers2

12

The Method setAlwaysOnTop(boolean) belongs to Window class. So both classes JDialog and JFrame inherit this method. You can use JDialog marked as AlwaysOnTop.

You can use JOptionPane to prepare required JDialog:

JOptionPane optionPane = new JOptionPane("ErrorMsg", JOptionPane.ERROR_MESSAGE);    
JDialog dialog = optionPane.createDialog("Failure");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
svaor
  • 2,205
  • 2
  • 19
  • 41
  • How to close the dialog ? after pressing "OK" the program is still running ..how to dispose it . – Shaik Md Nov 03 '11 at 11:09
  • What do you want to do? Do you mean modal dialog (setModal(true))? Or you want to get result of user action on the dialog? – svaor Nov 03 '11 at 11:24
  • JDialog dialog = new JOptionPane("ErrorMsg", JOptionPane.ERROR_MESSAGE,JOptionPane.DEFAULT_OPTION).createDialog(" Failure"); dialog.setAlwaysOnTop(true); dialog.setVisible(true); dialog.dispose(); – Shaik Md Nov 03 '11 at 11:38
  • Thanks a Lot :) !! the above code is working , optimum solution for my problem .. – Shaik Md Nov 03 '11 at 11:39
  • 1
    Don't mention it. :) Now you can mark this question as unswered. – svaor Nov 03 '11 at 11:58
2

you can pass any JComponent as Object in the conctructor for JOptionPane,

in your case is there null value - JOptionPane.showMessageDialog(null, "ErrorMsg", for example

JOptionPane.showMessageDialog(myFrame, 
  "ErrorMsg", "Failure", JOptionPane.ERROR_MESSAGE);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • But what does that mean? I believe mKorbel outlined the best way to get a dialog always on top of a frame. If the entire application is minimized or another app. takes priority, your app. should surrender the screen space. – Andrew Thompson Nov 03 '11 at 11:28