Java JOptionPane Error displayErrorMessage(Component component, Throwable t)

Here you can find the source of displayErrorMessage(Component component, Throwable t)

Description

display Error Message

License

Open Source License

Declaration

public static void displayErrorMessage(Component component, Throwable t) 

Method Source Code


//package com.java2s;
/*//from  w  w w.ja v a2s .com
 * ClientUtils.java
 *
 * Network Embedded Sensor Testbed (NESTbed)
 *
 * Copyright (C) 2006-2007
 * Dependable Systems Research Group
 * School of Computing
 * Clemson University
 * Andrew R. Dalton and Jason O. Hallstrom
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 *
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301, USA.
 */

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Main {
    public static void displayErrorMessage(Component component, Throwable t) {
        Frame parent = null;

        for (Component c = component; c != null && parent == null; c = c.getParent()) {
            if (c instanceof Frame) {
                parent = (Frame) c;
            }
        }

        JDialog dialog = createExceptionDialog(parent, t.getClass().getName(), t);
        dialog.setVisible(true);
    }

    private static JDialog createExceptionDialog(Frame parent, String title, Throwable error) {
        ByteArrayOutputStream messageStream = new ByteArrayOutputStream();
        PrintWriter out = new PrintWriter(messageStream);
        error.printStackTrace(out);
        out.close();

        JTextField errorMsg = new JTextField(error.toString());
        JTextArea errorText = new JTextArea(messageStream.toString());
        JScrollPane textPane = new JScrollPane(errorText);

        errorText.setEditable(false);
        errorMsg.setEditable(false);

        textPane.setPreferredSize(new Dimension(500, 400));

        JOptionPane warning = new JOptionPane(textPane, JOptionPane.ERROR_MESSAGE);

        JDialog dialog = warning.createDialog(parent, title);
        dialog.getContentPane().add(warning);
        dialog.setResizable(true);

        return dialog;
    }
}

Related

  1. contChoose(final Component parent, final String title, final String message, final String noContMsg, boolean cont, boolean error)
  2. createAndShowErrorPanel(String title, Map messages)
  3. displayError(Component parent, String title, String message)
  4. displayError(String message)
  5. displayError(Throwable e)
  6. displayErrorMessage(Component parent, String message)
  7. displayErrorMessage(Component parentComponent, String message, String windowTitle)
  8. displayErrorMessage(final Throwable throwable, final Component parentComponent)
  9. displayErrorMessage(String msg, Throwable t)