我有一个JFrame
,有两个面板。我将UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
设置在一个扩展JFrame
的类中。所以它适用于两个面板如果我没有JTable
。但是,如果我向任何面板添加JTable
,那么转到该面板会给我NullPointerException
,如下所示。
那么问题是什么,我需要为JTable
设置
public class Frame extends JFrame
{
public void initialize()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e)
{
e.printStackTrace();
}
JPanel panel1 = new JPanel();
panel1.add(new JLabel("Panel1"));
JPanel panel2 = new JPanel();
JTable table = new JTable(2,2);
table.getColumnModel().getColumn(1).setCellRenderer(new CheckBoxRenderer()); // Adding this renderer creates me problem
JScrollPane jScrollPane = new JScrollPane(table);
jScrollPane.setSize(100, 100);
panel2.add(jScrollPane);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Panel1", panel1);
tabbedPane.addTab("Panel2", panel2);
add(tabbedPane);
setSize(400, 400);
setVisible(true);
}
class CheckBoxRenderer implements TableCellRenderer
{
JCheckBox check = new JCheckBox();
public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column)
{
if (row != 0 && obj instanceof Boolean)
{
check.setSelected((Boolean) obj);
return check;
}
return null;
}
}
public static void main(String[] args)
{
Frame frame = new Frame();
frame.initialize();
}
}
所以这给了我NullPointerException。如果我删除setCellRenderer行,它工作正常。有人能帮助我吗
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:685)
at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)
at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)
at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)
at javax.swing.JComponent.paintComponent(JComponent.java:779)
at javax.swing.JComponent.paint(JComponent.java:1055)
at javax.swing.JComponent.paintChildren(JComponent.java:888)
at javax.swing.JComponent.paint(JComponent.java:1064)
at javax.swing.JViewport.paint(JViewport.java:731)
at javax.swing.JComponent.paintChildren(JComponent.java:888)
at javax.swing.JComponent.paint(JComponent.java:1064)
at javax.swing.JComponent.paintChildren(JComponent.java:888)
at javax.swing.JComponent.paint(JComponent.java:1064)
at javax.swing.JComponent.paintChildren(JComponent.java:888)
at javax.swing.JComponent.paint(JComponent.java:1064)
at javax.swing.JComponent.paintChildren(JComponent.java:888)
at javax.swing.JComponent.paint(JComponent.java:1064)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5232)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
at javax.swing.JComponent._paintImmediately(JComponent.java:5180)
at javax.swing.JComponent.paintImmediately(JComponent.java:4991)
at javax.swing.RepaintManager$3.run(RepaintManager.java:808)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
EDIT2:谷歌上搜索我发现了NullPointerException at javax.swing.plaf.synth.SynthContext.getPainter。
如果我UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
工作正常。但我使用的是Ubuntu操作系统,所以我需要GTK外观。所以我做了UIManager.setLookAndFeel("ccom.sun.java.swing.plaf.gtk.GTKLookAndFeel");
,但这又给了我一个例外。我无法弄清楚这里有什么问题。我从getSystemLookAndFeelClassName()
答案 0 :(得分:2)
您的问题遗漏了几个有用的信息,例如:JVM版本,操作系统......
我过去遇到过类似的问题,它与该特定JVM版本的BUG有关,所以您是否更新到最新的JVM版本?
看看this post。
我假设您的JTable没有自定义任何东西,并且没有事件处理程序......
答案 1 :(得分:0)
此代码现在可以正常使用
class CheckBoxRenderer implements TableCellRenderer
{
JCheckBox check = new JCheckBox();
@Override
public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column)
{
if (row != 0 && obj instanceof Boolean)
{
check.setSelected((Boolean) obj);
}
// return null; this line gives NullPointerException
return check;
}
}
最后,我想出了问题所在。问题在于上面的行return null
。返回null
即可获得NullPointerException
。因为如果我们看到
private void paintCell(SynthContext context, Graphics g, Rectangle cellRect, int row, int column)
的{{1}} 有一行
SynthTableUI.class
但是因为我正在返回Color b = component.getBackground();
,它正试图获取null
的{{1}}。这就是实际问题所在。谢谢你的帮助