简体   繁体   中英

Nimbus Look&Feel refresh Painter

I'm using the Nimbus look & feel in my swing application.

I set the primary and secondary properties of the UIDefaults of the look & feel. The colors are right. Now i have the problem, that the painter of the components use the colors, which were defined before updating the color theme.

Is there a way, to update the painters of all components to use the new colors, or do I need to implement a custom painter for each property?

I already call SwingUtilities.updateComponentTreeUI(window) after setting the properties in the UIDefaults .

EDIT:

The following code sets the L&F of the whole application:

 try { for( LookAndFeelInfo info : UIManager.getInstalledLookAndFeels() ) { if( "Nimbus".equals( info.getName() ) ) { UIManager.setLookAndFeel(info.getClassName()); customizeNimbusLaF(); SwingUtilities.updateComponentTreeUI( appWindow ); break; } } } catch( Exception e ) { LogUtility.warning( "cannot set application look and feel" ); LogUtility.warning( e.getMessage() ); }


The code does, what it is supposed to do (setting the Nimbus Look & Feel). The problem is, that the Painters of the Menu and other components work with the old colors.
The following code sets the colors:

 private final void customizeNimbusLaF() { UIManager.put( "control" , UIConstants.GREY_LIGHT ); UIManager.put( "nimbusAlertYellow" , UIConstants.YELLOW ); UIManager.put( "nimbusBase" , UIConstants.GREY_DARK ); UIManager.put( "nimbusDisabledText" , UIConstants.GREY_DARK ); UIManager.put( "nimbusFocus" , UIConstants.BLUE_LIGHT ); UIManager.put( "nimbusGreen" , UIConstants.GREEN ); UIManager.put( "nimbusInfoBlue" , UIConstants.BLUE_MIDDLE ); UIManager.put( "nimbusRed", UIConstants.RED ); UIManager.put( "nimbusSelectionBackground", UIConstants.BLUE_MIDDLE ); UIManager.put( "background" ,UIConstants.GREY_LIGHT ); UIManager.put( "controlDkShadow" , UIConstants.GREY_DARK ); UIManager.put( "controlShadow", UIConstants.GREY_MIDDLE ); UIManager.put( "desktop", UIConstants.BLUE_MIDDLE ); UIManager.put( "menu", UIConstants.GREY_LIGHT ); UIManager.put( "nimbusBorder", UIConstants.GREY_MIDDLE ); UIManager.put( "nimbusSelection", UIConstants.BLUE_MIDDLE ); UIManager.put( "textBackground", UIConstants.BLUE_LIGHT ); UIManager.put( "textHighlight", UIConstants.BLUE_LIGHT ); UIManager.put( "textInactiveText", UIConstants.GREY_MIDDLE ); // panel UIManager.put( "Panel.background", UIConstants.GREY_LIGHT ); UIManager.put( "Panel.disabled", UIConstants.GREY_LIGHT ); UIManager.put( "Panel.font", UIConstants.DEFAULT_FONT ); UIManager.put( "Panel.opaque", true ); // button UIManager.put( "Button.background", UIConstants.GREY_LIGHT ); UIManager.put( "Button.disabled", UIConstants.GREY_LIGHT ); UIManager.put( "Button.disabledText", UIConstants.BLUE_MIDDLE ); UIManager.put( "Button.font", UIConstants.DEFAULT_FONT ); // menu UIManager.put( "Menu.background", UIConstants.GREY_LIGHT ); UIManager.put( "Menu.disabled", UIConstants.GREY_LIGHT ); UIManager.put( "Menu.disabledText", UIConstants.GREY_DARK ); UIManager.put( "Menu.font", UIConstants.MENU_FONT ); UIManager.put( "Menu.foreground", UIConstants.BLACK ); UIManager.put( "Menu[Disabled].textForeground", UIConstants.GREY_MIDDLE ); UIManager.put( "Menu[Enabled].textForeground", UIConstants.BLACK ); UIManager.put( "MenuBar.background", UIConstants.GREY_LIGHT ); UIManager.put( "MenuBar.disabled", UIConstants.GREY_LIGHT ); UIManager.put( "MenuBar.font", UIConstants.MENU_FONT ); UIManager.put( "MenuBar:Menu[Disabled].textForeground", UIConstants.GREY_MIDDLE ); UIManager.put( "MenuBar:Menu[Enabled].textForeground", UIConstants.BLACK ); UIManager.put( "MenuItem.background", UIConstants.GREY_LIGHT ); UIManager.put( "MenuItem.disabled", UIConstants.GREY_LIGHT ); UIManager.put( "MenuItem.disabledText", UIConstants.GREY_MIDDLE ); UIManager.put( "MenuItem.font", UIConstants.MENU_FONT ); UIManager.put( "MenuItem.foreground", UIConstants.BLACK ); UIManager.put( "MenuItem[Disabled].textForeground", UIConstants.GREY_MIDDLE ); UIManager.put( "MenuItem[Enabled].textForeground", UIConstants.BLACK ); // tree UIManager.put( "Tree.background", UIConstants.BLACK ); }


The datatypes of the constants in UIConstants are either of type Color of Font depending of the attribute to be set.

Can someone tell me where my problem is?

Greets Michael

no idea what did you tried, because

  • set all setting for UImanager before creating an Swing GUI and starting AWT Thread

  • you have to call SwingUtilities.updateComponentTreeUI(window) in all cases that Swing GUI is visible and you needed to change L&F on runtime

  • separated issue could be with XxxUIResources , but no idea without seeing your SSCCE

  • for better help sooner post an SSCCE demonstrated your issue about Nimbus L&F , value for UIManager and Colors stays unchanged

Change place on these to lines of code

       UIManager.setLookAndFeel(info.getClassName());
       customizeNimbusLaF();

to

       customizeNimbusLaF();
       UIManager.setLookAndFeel(info.getClassName());

I would like to add an alternative to this method proposed here.

In particular, all the problems related to the UIManager described in the following issue are solved by the material-ui-swing library with the concept of Material Theming System.

Material Theme System gives the possibility to set up your colors and style decision like with/without border, the border with round corner or not in a single class that is documented here under the package mdlaf.themes .

An example of usage can be the following one

  try {
      JDialog.setDefaultLookAndFeelDecorated(true);
      JFrame.setDefaultLookAndFeelDecorated(false);
      MaterialTheme theme = new MaterialLiteTheme();
      // here your custom config theme.setXXX();
      MaterialLookAndFeel material = new MaterialLookAndFeel(theme);
      UIManager.setLookAndFeel(material);
  } catch (UnsupportedLookAndFeelException e) {
      e.printStackTrace();
  }

You can keep around also the Theme, makes your custom change and when you are read call SwingUtilities.updateComponentTreeUI(window)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM