Search code examples
javaswinglook-and-feeljprogressbaruimanager

Overriding LookAndFeel


Hello I am trying to change the colors of my swing progress bar in java. Initially I tried using the following code:

UIManager.put("ProgressBar.selectionBackground", Color.black);
UIManager.put("ProgressBar.selectionForeground", Color.white);
UIManager.put("ProgressBar.background", Color.white);
UIManager.put("ProgressBar.foreground", Color.RED);
m_progressBar.setBackground(Color.BLACK);
m_progressBar.setForeground(Color.red);

But none of the commands seemed to have any effect!!! Unless I set m_progressBar.stringPainted(true) and then the foreground does change.

Anyhows I know that the problem arises from the following command: UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); which is invoked before the UI is initialized.

So my questions are:
1) Out of curiosity: why does the the stringPainted(true) invocation manages to override foreground UI settings?
2) How do I keep UIManager.setLookAndFeel in the beginning of the code and still override UI settings?

Thank you!!


Solution

  • Try this.............

    `Changing the Look and Feel After Startup`
    

    You can change the L&F with setLookAndFeel even after the program's GUI is visible. To make existing components reflect the new L&F, invoke the SwingUtilities updateComponentTreeUI method once per top-level container. Then you might wish to resize each top-level container to reflect the new sizes of its contained components.

    For example:

    UIManager.setLookAndFeel(lnfName);
    SwingUtilities.updateComponentTreeUI(frame);
    frame.pack();
    

    In other words: you can change the look and feel of a top-level container (I don't have tested if it is limited to top-level containers only or if this method can be applied to any container). Or, to answer your question even more precisely: I don't see there's method of styling a single component, but there is a way for single components in (toplevel) containers.