Java JOptionPane Message showExceptionMessage(Component parentComponent, Exception exception)

Here you can find the source of showExceptionMessage(Component parentComponent, Exception exception)

Description

Show an Exception dialogue.

License

Open Source License

Parameter

Parameter Description
parentComponent a parameter
exception a parameter

Exception

Parameter Description
HeadlessException an exception

Declaration

public static void showExceptionMessage(Component parentComponent, Exception exception)
        throws HeadlessException 

Method Source Code


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

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

public class Main {
    /**//from w  w w.  j a va 2  s .co  m
     * Show an Exception dialogue.
     * 
     * @param parentComponent
     * @param exception
     * @throws HeadlessException
     */
    public static void showExceptionMessage(Component parentComponent, Exception exception)
            throws HeadlessException {

        StringWriter stringWriter = new StringWriter();
        exception.printStackTrace(new PrintWriter(stringWriter));

        JLabel message = new JLabel(exception.getMessage());
        message.setBorder(BorderFactory.createEmptyBorder(3, 0, 10, 0));

        JTextArea text = new JTextArea();
        text.setEditable(false);
        text.setFont(UIManager.getFont("Label.font"));
        text.setText(stringWriter.toString());
        text.setCaretPosition(0);

        JScrollPane scroller = new JScrollPane(text);
        scroller.setPreferredSize(new Dimension(400, 200));

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        panel.add(message, BorderLayout.NORTH);
        panel.add(scroller, BorderLayout.SOUTH);

        JOptionPane.showMessageDialog(parentComponent, panel, "Exception!", JOptionPane.ERROR_MESSAGE);

    }
}

Related

  1. showActionFailedWithExceptionMessage(final Component parent, final Exception ex)
  2. showAlert(String message)
  3. showException(Component c, String message, Throwable t)
  4. showException(String message, Exception ex)
  5. showExceptionMessage(Component parent, Throwable t)
  6. showExceptionMessage(Exception e)
  7. showExceptionMessage(Exception e)
  8. showInput(Component parent, String message, String initValue)
  9. showInput(String message)