1

I saw this post: how to make Indeterminate progressbar look nicely?

That's just the opposite direction of what I want, I want to use the System (windows) look and feel, but have the nimbus progressbar.

My code so far:

private void setLookAndFeel(){

    // set nimbus look&feel
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            try {
                UIManager.setLookAndFeel(info.getClassName());
            } catch (ClassNotFoundException | InstantiationException
                    | IllegalAccessException
                    | UnsupportedLookAndFeelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
    }

    // 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 system look&feel
    try {
        UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.updateComponentTreeUI(this);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Couldn't use system look and feel: " + e);
    } 

    // copy back progress bar defaults
    for(Map.Entry<Object, Object> entry : progressDefaults.entrySet()){
        System.out.println(entry.getKey().toString() + ": " + entry.getValue());
        UIManager.put(entry.getKey(), entry.getValue()); //getDefaults().
    }
    System.out.println("lookAndFeel " + UIManager.getLookAndFeel());
}

But if I compile I get the error message:

com.sun.java.swing.plaf.windows.WindowsLookAndFeel cannot be cast to > javax.swing.plaf.nimbus.NimbusLookAndFeel

where I initialize the progress bar.

If someone could explain to me what's going on and/or how to get a nimbus-looking progressbar on a system (windows) look and feel I would be so happy. I'm mainly interested in the glass-effekt.

edit: If I don't copy the progressbar defaults back, the error is gone.

Community
  • 1
  • 1
murkr
  • 634
  • 5
  • 27

1 Answers1

0

I have no idea what the problem was, but the solution is the following: Divide the method in 2 parts (directly before "//copy progress bar defaults"). Then call the method that sets the nimbus look&feel, then the method to initialize the class-variables (and with that the progressbar). Third, call the new method with everything beginning from "//copy progress bar defaults". (the method will copy the progressbar defaults from the nimbus look&feel, set the system look and feel and then paste the progressbar defaults).

This line is just added as a workaround to that AWEFUL captcha bullshit thing.

murkr
  • 634
  • 5
  • 27