Problem with SWT_AWT and JOptionPane [message #458713] |
Tue, 19 July 2005 20:17 |
Ilya Messages: 2 Registered: July 2009 |
Junior Member |
|
|
Hi,
I am running into a strange crash with SWT_AWT and I can't figure out how
to work around it. I am using SWT 3.1 and JDK 1.4.2.
Please help if you can.
To reproduce, run the class below and when the window shows up, close it
by pressing the X button on the top right. You get the exception:
java.lang.NullPointerException
at java.awt.Component$NativeInLightFixer.<init>(Component.java:7046)
at java.awt.Component.addNotify(Component.java:5474)
at java.awt.Container.addNotify(Container.java:2042)
at java.awt.Window.addNotify(Window.java:418)
at java.awt.Dialog.addNotify(Dialog.java:389)
at java.awt.Window.pack(Window.java:436)
at javax.swing.JOptionPane.createDialog(JOptionPane.java:917)
at javax.swing.JOptionPane.showOptionDialog(JOptionPane.java:83 7)
at javax.swing.JOptionPane.showConfirmDialog(JOptionPane.java:7 68)
at javax.swing.JOptionPane.showConfirmDialog(JOptionPane.java:7 31)
at simple.SimpleExample$1.windowClosed(SimpleExample.java:71)
at java.awt.Window.processWindowEvent(Window.java:1124)
at java.awt.Window.processEvent(Window.java:1079)
...
NOTE that I am popping up a JOptionPane dialog when the app is closing.
For instance, this could be a "Would you like to save?" dialog with Yes/No
options, so this is a pretty standard usage.
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SimpleExample
{
/**
* This method is a constructor for the application.
*/
public SimpleExample()
{
}
private void initGUI()
{
Shell shell = new Shell();
shell.setText("Test");
shell.setSize(510, 430);
// Creating a composite from the shell
Composite composite = new Composite(shell, SWT.EMBEDDED);
composite.setBounds(0, 0, 500, 400);
composite.setLayout(new RowLayout());
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
// Creating the AWT frame using the composite object
// in which the main panel will be displayed
this.frame = SWT_AWT.new_Frame(composite);
frame.add(mainPanel);
frame.addWindowListener(new WindowAdapter()
{
/**
* Invoked when a window has been closed.
*/
public void windowClosed(WindowEvent e)
{
JOptionPane.showConfirmDialog(
SimpleExample.this.frame,
"Whatever",
"Title",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
}
});
// Opening the shell
shell.open();
// Checking to see when the display should be closed
Display display = shell.getDisplay();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
/**
* This main method creates and starts an instance of the
* application.
*/
public static void main(String[] args)
{
new SimpleExample().initGUI();
}
Frame frame;
}
|
|
|
Powered by
FUDForum. Page generated in 0.03804 seconds