Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open warning/information/error dialog in Swing?

Tags:

java

swing

How to open warning/information/error dialog in Swing?

I need standard error dialog with "Ok" button and "red cross" image. I.e. analog of org.eclipse.jface.dialogs.MessageDialog.openError()

like image 672
Oleg Vazhnev Avatar asked Jun 07 '11 19:06

Oleg Vazhnev


People also ask

What is dialog box in swing?

JDialog is a part Java swing package. The main purpose of the dialog is to add components to it. JDialog can be customized according to user need . Constructor of the class are: JDialog() : creates an empty dialog without any title or any specified owner.

How do you write a warning message in Java?

showMessageDialog() − To show the message alert. JOptionPane. WARNING_MESSAGE − To mark the alert message as warning.


2 Answers

See How to Make Dialogs.

You can use:

JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green."); 

And you can also change the symbol to an error message or an warning. E.g see JOptionPane Features.

like image 123
Jonas Avatar answered Sep 21 '22 07:09

Jonas


import javax.swing.JFrame; import javax.swing.JOptionPane;  public class ErrorDialog {    public static void main(String argv[]) {     String message = "\"The Comedy of Errors\"\n"         + "is considered by many scholars to be\n"         + "the first play Shakespeare wrote";     JOptionPane.showMessageDialog(new JFrame(), message, "Dialog",         JOptionPane.ERROR_MESSAGE);   } } 
like image 24
Said Erraoudy Avatar answered Sep 18 '22 07:09

Said Erraoudy