1 year ago

#257715

test-img

martin

I have some errors with JList rendering JLists

So I want to make a main JList that would render JLists inside it. The problem is that when I actually add a new string to some JList inside the main JList, I must call mainList.updateUI(); to show the added string in that JList, but it causes this exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.swing.plaf.synth.SynthStyle.get(javax.swing.plaf.synth.SynthContext, Object)" because the return value of "javax.swing.plaf.synth.SynthContext.getStyle()" is null
    at java.desktop/javax.swing.plaf.synth.SynthDefaultLookup.getDefault(SynthDefaultLookup.java:43)
    at java.desktop/sun.swing.DefaultLookup.get(DefaultLookup.java:125)
    at java.desktop/sun.swing.DefaultLookup.getBorder(DefaultLookup.java:202)
    at java.desktop/sun.swing.DefaultLookup.getBorder(DefaultLookup.java:210)
    at java.desktop/javax.swing.DefaultListCellRenderer.getNoFocusBorder(DefaultListCellRenderer.java:105)
    at java.desktop/javax.swing.DefaultListCellRenderer.getListCellRendererComponent(DefaultListCellRenderer.java:172)
    at com.martin.gui.CostumJListRenderer.getListCellRendererComponent(CostumJListRenderer.java:10)
    at java.desktop/javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1444)
    at java.desktop/javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1394)
    at java.desktop/javax.swing.plaf.basic.BasicListUI.paintImpl(BasicListUI.java:330)
    at java.desktop/javax.swing.plaf.basic.BasicListUI.paint(BasicListUI.java:306)
    at java.desktop/javax.swing.plaf.synth.SynthListUI.update(SynthListUI.java:90)
    at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:842)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1119)
    at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:952)
    at java.desktop/javax.swing.JComponent.paint(JComponent.java:1128)
    at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5311)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1657)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1632)
    at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1570)
    at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1337)
    at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5259)
    at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5069)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:879)
    at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:862)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:862)
    at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:835)
    at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:784)
    at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1898)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

My goal is to have categories of users with specific rank inside one main list, like how users are listed by ranks in discord.

This is the code:

My main list renderer

public class CostumJListRenderer extends DefaultListCellRenderer {

   @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (value instanceof JList<?>) {
                c = (Component) value;
            }
            return c;
    }

}

The adding part:

if(object != null) {
    if(!ScreenGUI.getInstance().coloredString.containsKey(String.valueOf(object.get("rnk")))) {
        //checking if the colored string contains rank, if not then new list and rank+color will be assigned
        ScreenGUI.getInstance().coloredString.put(String.valueOf(object.get("rnk")), Color.decode(String.valueOf(object.get("usrClr"))));
        JList<String> list = new JList<>();
        Color color = Color.decode(String.valueOf(object.get("usrClr")));
        TitledBorder border = new TitledBorder(new LineBorder(Color.DARK_GRAY, 1), String.valueOf(object.get("rnk")));
        border.setTitleJustification(TitledBorder.LEFT);
        border.setTitlePosition(TitledBorder.TOP);
        border.setTitleColor(color);
        border.setTitleFont(new Font("", Font.BOLD, 13));
        list.setBorder(border);
        DefaultListModel<String> strModel = new DefaultListModel<>();
        ScreenGUI.getInstance().addOnlineUsers(strModel, String.valueOf(object.get("prefX")).charAt(0) + String.valueOf(object.get("rnk")) + String.valueOf(object.get("prefX")).charAt(1) + " - " + String.valueOf(object.get("usr")), String.valueOf(object.get("rnk"))); //username perimeter
        ScreenGUI.getInstance().list.add(new ListData(list, String.valueOf(object.get("rnk")), String.valueOf(object.get("usrClr")), strModel));
    }else {
        ScreenGUI.getInstance().list.forEach((current) -> {
            if(current.rank.equals(String.valueOf(object.get("rnk")))) {
                current.model.addElement(String.valueOf(object.get("prefX")).charAt(0) + String.valueOf(object.get("rnk")) + String.valueOf(object.get("prefX")).charAt(1) + " - " + String.valueOf(object.get("usr")));
            }
        });
        ScreenGUI.getInstance().updateUsrUI(); //mainList.updateUI();
}

My ListData class that contains info about JList

public class ListData {

    public JList<String> list;
    public DefaultListModel<String> model;
    public String rank;
    public String color;

    public ListData(JList<String> list, String rank, String color, DefaultListModel<String> model) {
        this.list = list;
        this.rank = rank;
        this.color = color;
        this.model = model;
        list.setFont(new Font("", Font.BOLD, 14));
        list.setModel(model);
        list.setForeground(Color.decode(color));
        ScreenGUI.getInstance().addToMainUserModel(list);
    }
}

I must also mention that I am using Nimbus theme in JFrame

How can I fix the exception, and am I even doing it correct?

Thanks for help in the comments!

FIX:

You have two options:

  1. Make JPanel with BoxLayout and then just add JList(-s) to it!
  2. Batter one, just go to JavaFX and use accordion component!

(thanks to @GilbertLeBlanc for help)

java

swing

nullpointerexception

jlist

listcellrenderer

0 Answers

Your Answer

Accepted video resources