问题
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.
But if I set "f.setSize(300, 400);", the thumb will be visible.
How can I set the thumb always visible?
回答1:
Try to set the minimum size of thumb to 1
getDefaults().put("ScrollBar.minimumThumbSize", new Dimension(29, 1));
回答2:
You should try this:
UIManager.getLookAndFeelDefaults().put("ScrollBar.minimumThumbSize", new Dimension(30, 30));
来源:https://stackoverflow.com/questions/32857372/jscrollbar-dont-show-thumb-in-nimbus-lf