Oleg Vazhnev
Oleg Vazhnev

Reputation: 24067

How to open warning/information/error dialog in 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()

Upvotes: 53

Views: 154311

Answers (4)

Ramon Dias
Ramon Dias

Reputation: 1127

Just complementing, you can use static imports to give you a hand, making the code cleaner, like this:

import static javax.swing.JOptionPane.*;

public class SimpleDialog(){
    public static void main(String argv[]) {
        showMessageDialog(null, "Message", "Title", ERROR_MESSAGE);
    }
}

Upvotes: 4

Said Erraoudy
Said Erraoudy

Reputation: 1559

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);
  }
}

Upvotes: 27

Heisenbug
Heisenbug

Reputation: 39164

JOptionPane.showOptionDialog
JOptionPane.showMessageDialog
....

Have a look on this tutorial on how to make dialogs.

Upvotes: 9

Jonas
Jonas

Reputation: 128807

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.

Upvotes: 85

Related Questions