Search code examples
javaswingjlistnimbusjscrollbar

JScrollBar don't show thumb in Nimbus L&F


I got a problem which only happens on Nimbus L&F. If there are too many items in a JList, the thumb of JScrollBar will disappear. But in metal L&F, the thumb will be always visible, because it has a min size. I have also checked the logic in Nimbus L&F, there does have a same min size. But it not effected.

Please see my code below:

    public static void main(String[] args) {
    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            try {
                UIManager.setLookAndFeel(info.getClassName());
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
            }
            break;
        }
    }

    JFrame f = new JFrame("Metal (height 300)");

    String[] ss = new String[100];
    for (int i = 0; i < ss.length; i++) {
        ss[i] = "" + i;
    }

    JList<String> l = new JList<String>();
    l.setListData(ss);

    final JScrollPane jScrollPane = new JScrollPane(l);
    f.getContentPane().add(jScrollPane);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300, 300);
    f.setVisible(true);
}

When I set "f.setSize(300, 300);", the thumb will disappear.

enter image description here

But if I set "f.setSize(300, 400);", the thumb will be visible.

enter image description here

How can I set the thumb always visible?

enter image description here


Solution

  • Try to set the minimum size of thumb to 1

    getDefaults().put("ScrollBar.minimumThumbSize", new Dimension(29, 1));