JTabbedPane: avoid automatic re-ordering tabs if stacked / Nimbus

后端 未结 3 1875
天涯浪人
天涯浪人 2021-01-06 11:49

a JTabbedPane is just what I need for my purpose. I have very limited horizontal space, so my Tabs get stacked, which is perfectly ok.

But the default behaviour is t

3条回答
  •  萌比男神i
    2021-01-06 12:38

    It's a bit of a hack but you can override the Nimbus defaults to have the plain old Java look-and-feel for the tabbed pane, whilst keeping everything else the same. The plain Java look-and-feel for a tabbed pane doesn't do the annoying reordering. Just store the defaults before you set the look-and-feel and then set them back.

    // get the defaults to keep
    HashMap defaultsToKeep = new HashMap<>();
    for (Map.Entry entry : UIManager.getDefaults().entrySet()) {
        boolean isStringKey = entry.getKey().getClass() == String.class ;
        String key = isStringKey ? ((String) entry.getKey()):"";    
        if (key.startsWith("TabbedPane")) {
            defaultsToKeep.put(entry.getKey(), entry.getValue());
        }
    }
    
    // set nimbus look and feel
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
    
    // set back your originals
    for (Map.Entry entry : defaultsToKeep.entrySet()) {
        UIManager.getDefaults().put(entry.getKey(), entry.getValue());
    }
    
    JFrame.setDefaultLookAndFeelDecorated(true);
    

提交回复
热议问题