SetLookAndFeel () provides NullPointerException if I use JTable

I have JFrame

two panels. And I install UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())

in a class that extends JFrame

. This works well for both panels. If I'm not in it JTable

. But if I add JTable

to any panel, then going to that panel will give me NullPointerException

like below.

So what is the problem, I need to set something for JTable

also

EDIT: I figured out that the problem is not with the JTable, but with the tableCellRenderer. I am adding tableRenderer as shown below

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();
}

 }

      

So this is giving me a NullPointerException. And if I remove the setCellRenderer line it works fine. Can someone help me.

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: Googling I found this NullPointerException at javax.swing.plaf.synth.SynthContext.getPainter .

And if I do UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");

that works great. But I am using Ubuntu OS so I need the GTK look and feel. So I did UIManager.setLookAndFeel("ccom.sun.java.swing.plaf.gtk.GTKLookAndFeel");

, but this again gives me an exception. I cannot figure out what is wrong here. I learned about these packages from the actual implementationgetSystemLookAndFeelClassName()

+3


source to share


2 answers


This code works fine for me now

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;
    }
}

      

Finally I figured out the problem. The problem is with the above line return null

. Returnnull



gives NullPointerException

. Because if we see the implementation

private void paintCell(SynthContext context, Graphics g, Rectangle cellRect, int row, int column)

of SynthTableUI.class

it has a string like

Color b = component.getBackground();

but since I am returning null



, he tries to get background

ofnull



... This is the real problem. Thank you for your help.

0


source


Your question missed some useful information like: JVM version, Operating system ...

I faced a similar issue in the past and was related to the BUG of this particular JVM version, so have you updated to the latest JVM version?



Take a look at this post .

I am assuming your JTable is not configured with anything and there is no event handler in it ...

+2


source







All Articles