如果我使用 JTable,setLookAndFeel() 会给出 NullPointerException
setLookAndFeel() gives NullPointerException if I use JTable
我有一个带有两个面板的 JFrame
。我在扩展 JFrame
的 class 中设置 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
。所以如果我没有 JTable
它对两个面板都有效。但是,如果我将 JTable
添加到任何面板,然后转到该面板会给我 NullPointerException
,如下所示。
所以这里有什么问题,我需要为 JTable
和
设置什么吗?
编辑:我发现问题不在于 JTable,而在于 tableCellRenderer。我正在添加如下所示的 tableRenderer
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.run(RepaintManager.java:808)
at javax.swing.RepaintManager.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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.access00(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.access0(EventQueue.java:103)
at java.awt.EventQueue.run(EventQueue.java:706)
at java.awt.EventQueue.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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 OS 所以我需要 GTK 外观。所以我做了 UIManager.setLookAndFeel("ccom.sun.java.swing.plaf.gtk.GTKLookAndFeel");
但这又给了我例外。我不知道这里出了什么问题。我是从 getSystemLookAndFeelClassName()
的实际实现中了解到这些包的
您的问题缺少一些有用的信息,例如:JVM 版本、操作系统...
我以前也遇到过类似的问题,跟那个JVM版本的BUG有关,请问你更新到最新的JVM版本了吗?
看看this post。
我假设您的 JTable 未使用任何内容进行自定义,并且其上不存在任何事件处理程序...
这段代码现在对我来说工作正常
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)
of SynthTableUI.class
它有一行
Color b = component.getBackground();
但因为我正在返回 null
,它正在尝试获取 null
的 background
。这就是实际问题所在。感谢您的帮助
我有一个带有两个面板的 JFrame
。我在扩展 JFrame
的 class 中设置 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
。所以如果我没有 JTable
它对两个面板都有效。但是,如果我将 JTable
添加到任何面板,然后转到该面板会给我 NullPointerException
,如下所示。
所以这里有什么问题,我需要为 JTable
和
编辑:我发现问题不在于 JTable,而在于 tableCellRenderer。我正在添加如下所示的 tableRenderer
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.run(RepaintManager.java:808)
at javax.swing.RepaintManager.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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.access00(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.access0(EventQueue.java:103)
at java.awt.EventQueue.run(EventQueue.java:706)
at java.awt.EventQueue.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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 OS 所以我需要 GTK 外观。所以我做了 UIManager.setLookAndFeel("ccom.sun.java.swing.plaf.gtk.GTKLookAndFeel");
但这又给了我例外。我不知道这里出了什么问题。我是从 getSystemLookAndFeelClassName()
您的问题缺少一些有用的信息,例如:JVM 版本、操作系统...
我以前也遇到过类似的问题,跟那个JVM版本的BUG有关,请问你更新到最新的JVM版本了吗?
看看this post。
我假设您的 JTable 未使用任何内容进行自定义,并且其上不存在任何事件处理程序...
这段代码现在对我来说工作正常
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)
of SynthTableUI.class
它有一行
Color b = component.getBackground();
但因为我正在返回 null
,它正在尝试获取 null
的 background
。这就是实际问题所在。感谢您的帮助