Create jDialog Swing with a message of error with image

344 views Asked by At

I'm new of Java and I want to do a jDialog that is opening when I push a button in the main JFrame and show a message of error in this way:

enter image description here

I can't put the image in another way in NetBeans? I create in the source package a directory with the image and a try much thing:

jDialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
jDialog1.add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResourceAsStream("/img/error_button.png")))));
jDialog1.pack();
jDialog1.setLocationByPlatform(true);

jDialog1.setVisible(true);

Is there a simple way to do this?

1

There are 1 answers

2
Andrew Thompson On BEST ANSWER

We can use an option pane for this. It includes its own icons according to the type of message (and look and feel).

enter image description here

Exception e = new Exception("Error!");
JOptionPane.showMessageDialog(f, e, e.getMessage(), JOptionPane.WARNING_MESSAGE);