Java JOptionPane Error showError(JFrame owner, ImageIcon image, String title, String message, String text)

Here you can find the source of showError(JFrame owner, ImageIcon image, String title, String message, String text)

Description

show Error

License

Open Source License

Declaration

public static void showError(JFrame owner, ImageIcon image, String title, String message, String text) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

public class Main {
    public static void showError(JFrame owner, ImageIcon image, String title, String message, String text) {
        if (owner == null || !owner.isVisible()) {
            System.out.println(text);
        } else {/*from w w w  .  j ava 2  s . c  o  m*/
            final JDialog frame = new JDialog(owner, true);
            frame.setTitle(title);
            frame.setLayout(new BorderLayout());
            //frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

            JLabel label = new JLabel(message, image, SwingConstants.LEFT);

            label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

            JTextArea area = new JTextArea(text);
            area.setEditable(false);
            area.setWrapStyleWord(false);
            area.setLineWrap(false);

            JScrollPane scroll = new JScrollPane(area);
            scroll.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

            JButton ok = new JButton("OK");
            ok.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    frame.dispose();
                }
            });

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
            buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            buttonPanel.add(Box.createGlue());
            buttonPanel.add(ok);

            frame.add(label, BorderLayout.NORTH);
            frame.add(scroll, BorderLayout.CENTER);
            frame.add(buttonPanel, BorderLayout.SOUTH);
            frame.pack();
            frame.setSize(new Dimension(350, 200));
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
}

Related

  1. showError(Exception e)
  2. showError(Exception e)
  3. showError(final Component component, final String title, final String message)
  4. showError(final Component rootComponent, final String message, final String title)
  5. showError(JComponent parent, Throwable e)
  6. showError(String errorMsg)
  7. showError(String macAddress, String message)
  8. showError(String message)
  9. showError(String message)