Occasionally my [url http://r0k.us/graphics/SIHwheel.html]applet will throw java.lang.reflect.InvocationTargetException . It always seems to load fine the first time, but if you click away and quickly come back, the exception is sometimes thrown. This causes the applet not to run. If I click away and wait for the Java Console to die, then come back, it seems to run reliably again. Here is the startup code:
public void init()
{ // fallback to manual init() if cannot read color name dictionary
if (!NTC.readCND(null)) NTC.init();
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JPanel frame = makeContent();
setContentPane(frame);
}
});
} catch (Exception e) {
System.err.println("makeContent() failed to complete: " + e);
}
}
public JPanel makeContent()
{
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.motif.MotifLookAndFeel");
// Motif L&F does not provide cut/copy/paste automatically
JTextField field = new JTextField(10);
InputMap map = field.getInputMap();
while (map.getParent() != null) {
map = map.getParent();
}
map.put(KeyStroke.getKeyStroke("ctrl C"), DefaultEditorKit.copyAction);
map.put(KeyStroke.getKeyStroke("ctrl V"), DefaultEditorKit.pasteAction);
map.put(KeyStroke.getKeyStroke("ctrl A"), DefaultEditorKit.selectAllAction);
map.put(KeyStroke.getKeyStroke("ctrl X"), DefaultEditorKit.cutAction);
} catch (UnsupportedLookAndFeelException e) {
System.out.println("UnsupportedLookAndFeelException: " + e);
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException: " + e);
} catch (InstantiationException e) {
System.out.println("InstantiationException: " + e);
} catch (IllegalAccessException e) {
System.out.println("IllegalAccessException: " + e);
}
paintingCanvas = new SIHCanvas(640, 640);
controls = new SIHControls();
colorList = new SIHListPane();
JPanel outer = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = gbc.BOTH;
gbc.gridx = 1;
gbc.gridy = 0;
outer.add(paintingCanvas, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
outer.add(controls, gbc);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 2;
outer.add(colorList, gbc);
return(outer);
}
Does anyone know what the problem might be, and how I might force reliable restarts every time?
The calls to NTC.readCND(null) and potentially NTC.init() are completely non-GUI. The first will read and process a large properties file (1567 entries, stored within the .jar) which contains data for the GUI. Should that fail, NTC.init() does some quick processing on a minimal, hard-coded color list of 8 entries.