gpt4 book ai didi

java - 如果我使用 JTable,setLookAndFeel() 会给出 NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:47 25 4
gpt4 key购买 nike

我有一个带有两个面板的 JFrame。我正在一个扩展 JFrame 的类中设置 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$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"); 但这又给了我异常(exception)。我不知道这里出了什么问题。我从 getSystemLookAndFeelClassName()

的实际实现中了解到这些包

最佳答案

您的问题缺少一些有用的信息,例如:JVM 版本、操作系统...

我以前遇到过类似的问题,是跟那个JVM版本的BUG有关,请问你更新到最新的JVM版本了吗?

看看this post .

我假设你的 JTable 没有定制任何东西,并且没有事件处理程序存在......

关于java - 如果我使用 JTable,setLookAndFeel() 会给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31424235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com