Search code examples
javaswingprogress-barjprogressbarnimbus

how to make Indeterminate progressbar look nicely?


I'm using nimbus lookAndFill

UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

and my indeterminate JProgressBar looks like that:

http://img15.imageshack.us/img15/9470/uglyprogress.jpg

can i make it looks better?


Solution

  • There's an easier solution. You can just copy the progress bar UI defaults before setting the nimbus look-and-feel and then set them back after. You then get Nimbus look and feel but without its progress bar styling.

    // copy progress bar defaults
    HashMap<Object, Object> progressDefaults = new HashMap<>();
    for(Map.Entry<Object, Object> entry : UIManager.getDefaults().entrySet()){
        if(entry.getKey().getClass() == String.class && ((String)entry.getKey()).startsWith("ProgressBar")){
            progressDefaults.put(entry.getKey(), entry.getValue());
        }
    }
    
    // set nimbus
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    
    // copy back progress bar defaults
    for(Map.Entry<Object, Object> entry : progressDefaults.entrySet()){
        UIManager.getDefaults().put(entry.getKey(), entry.getValue());
    }