Created
October 18, 2012 12:05
-
-
Save tamurashingo/3911386 to your computer and use it in GitHub Desktop.
show the stacktrace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*- | |
* usage | |
* try { | |
* ... | |
* } | |
* catch ( Exception ex ) { | |
* showException( null, true, ex ); | |
* } | |
*/ | |
public static void showException( Frame parent, boolean modal, Throwable ex ) { | |
final JDialog dialog = new JDialog( parent, modal ); | |
JScrollPane scrollPane = new JScrollPane(); | |
dialog.getContentPane().add( scrollPane, BorderLayout.CENTER ); | |
dialog.setTitle( "StackTrace" ); | |
JButton button = new JButton( "OK" ); | |
button.addActionListener( new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
dialog.dispose(); | |
} | |
}); | |
dialog.getContentPane().add( button, BorderLayout.SOUTH ); | |
DefaultMutableTreeNode root = new DefaultMutableTreeNode( "StackTrace" ); | |
while ( ex != null ) { | |
DefaultMutableTreeNode node = new DefaultMutableTreeNode( ex.getClass().getName() + ":" + ex.getMessage() ); | |
for ( StackTraceElement elm : ex.getStackTrace() ) { | |
DefaultMutableTreeNode child = new DefaultMutableTreeNode( elm.toString() ); | |
node.add( child ); | |
} | |
root.add( node ); | |
ex = ex.getCause(); | |
} | |
JTree tree = new JTree( root ); | |
JViewport viewPort = scrollPane.getViewport(); | |
viewPort.setView( tree ); | |
dialog.pack(); | |
showCenter( dialog ); | |
} | |
public static void showCenter( java.awt.Window window ) { | |
Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); | |
int x = (int) ((d.getWidth() - window.getWidth()) / 2); | |
int y = (int) ((d.getHeight() - window.getHeight()) / 2); | |
window.setLocation( x, y ); | |
window.setVisible( true ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment