Other |
---|
1.2.2 1.2.2Fixed |
Duplicate :
|
|
Duplicate :
|
|
Duplicate :
|
|
Relates :
|
|
Relates :
|
Name: tb29552 Date: 11/18/98 /* In JDK 1.1 using Swing1.0.3, we had no trouble creating an offscreen Image and paint()ing a Swing component using the Image's graphics object. Under JDK1.2/Swing1.1, the same code fails with a NullPointerException in JComponent. The following simple program demonstrates the effect. It creates a JTabbedPane with two tabs, each containing a single JButton. When tab "A" is selected and "Button A" is pressed, the code creates an offscreen image, and calls paint on "Button B" with the image's graphics context. Running this program produces the following output: C:\> java -Djava.compiler=NONE TabTest Clicking on aa Paint bb Exception occurred during event dispatching: java.lang.NullPointerException: at javax.swing.JComponent.paint(JComponent.java:472) at TabTest$2.actionPerformed(TabTest.java:40) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1066) at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1101) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:217) at java.awt.Component.processMouseEvent(Component.java:3126) at java.awt.Component.processEvent(Component.java:2965) at java.awt.Container.processEvent(Container.java:987) at java.awt.Component.dispatchEventImpl(Component.java:2376) at java.awt.Container.dispatchEventImpl(Container.java:1032) at java.awt.Component.dispatchEvent(Component.java:2289) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:1944) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:1732) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:1645) at java.awt.Container.dispatchEventImpl(Container.java:1019) at java.awt.Window.dispatchEventImpl(Window.java:712) at java.awt.Component.dispatchEvent(Component.java:2289) at java.awt.EventQueue.dispatchEvent(EventQueue.java:258) at java.awt.EventDispatchThread.run(EventDispatchThread.java:68) */ */ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; public class TabTest { public static void main(String args[]) { class DriverFrame extends JFrame { public DriverFrame () { super(); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent event) { dispose(); // free the system resources System.exit(0); // close the application } }); Container c = getContentPane(); final JButton aa = new JButton("Button A"); final JButton bb = new JButton("Button B"); c.setLayout(new java.awt.BorderLayout()); this.setSize(300, 300); JTabbedPane jtp = new JTabbedPane(); aa.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Clicking on aa"); Image img = createImage(100, 100); Graphics g = img.getGraphics(); System.out.println("Paint bb"); bb.paint(g); } }); jtp.addTab("A", aa); jtp.addTab("B", bb); c.add(jtp); } } new DriverFrame ().setVisible(true); } } (Review ID: 42826) ======================================================================
|