Module jEdit

Class EnhancedTreeCellRenderer

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants, TreeCellRenderer
Direct Known Subclasses:
OptionsDialog.PaneNameRenderer

public abstract class EnhancedTreeCellRenderer extends DefaultTreeCellRenderer
An enhancement of the DefaultTreeCellRenderer to be used as superclass for custom tree cell renderers. Using DefaultTreeCellRenderer as superclass for a custom tree cell renderer without further measures is not stable in regards to on-the-fly Look and Feel changes, at least not with Java 6. For Java 7 it should be tested again.

With Java 6 the DefaultTreeCellRenderer initializes some values according to the Look and Feel in its constructor. If the DefaultTreeCellRenderer is created by the JTree code, it is recreated on a Look and Feel change. This way all works fine. But if a tree cell renderer is set explicitly on the JTree, no matter whether DefaultTreeCellRenderer, a subclass of it or a complete own implementation, the set instance is used beyond Look and Feel boundaries and this causes two problems.

  1. The values that were initialized in the constructor of DefaultTreeCellRenderer are not reset according to the new Look and Feel. Some values are partly set by the JTree code, but this is not complete and reliable and thus the renderer paints the tree cells wrongly.
  2. The Look and Feel change is first applied to the JTree, then the sizes of the tree cells which are saved in a cache are recalculated. Only after that, the children of the JTree get the new Look and Feel applied, amongst them also the tree cell renderer.
    So even if a custom tree cell renderer is aware of on-the-fly Look and Feel changes by reinitializing values from the Look and Feel if it changes, those cached sizes are still calculated for the old Look and Feel. The only way to work around this is to cause the cached sizes to be recalculated. This can be done by changing any significant property of the JTree which influences size calculations.

To work around the described problems this enhanced tree cell renderer listens for Look and Feel changes on the JTree where this renderer is used, requests a subclass to create a new instance of the renderer and sets it on the JTree. By doing so the DefaultTreeCellRenderer reinitializes to the new Look and Feel in its constructor and the JTree recalculates the cached size values because a different object is set as tree cell renderer.

See Also:
  • Constructor Details

    • EnhancedTreeCellRenderer

      public EnhancedTreeCellRenderer()
  • Method Details

    • getTreeCellRendererComponent

      public final Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
      Specified by:
      getTreeCellRendererComponent in interface TreeCellRenderer
      Overrides:
      getTreeCellRendererComponent in class DefaultTreeCellRenderer
    • newInstance

      protected abstract TreeCellRenderer newInstance()
      Creates a new instance of the tree cell renderer. Each invocation has to return a different object. Saving a reference and returning the same instance from different calls of this method is not appropriate.

      Any one-time initializations that are necessary and are not made in the constructor should be made in this method. The simplest implementation of this method will just call the constructor and return the result.

      This is an instance method so that the new instance can be set up with information from the current instance.

      Returns:
      a new readily initialized instance of this class
    • configureTreeCellRendererComponent

      protected abstract void configureTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
      Configures this instance of the renderer component based on the passed in components. The value is set from messaging the tree with convertValueToText, which ultimately invokes toString on value. The foreground color is set based on the selection and the icon is set based on the leaf and expanded parameters. The parameters of this method are the same as the ones of getTreeCellRendererComponent(JTree, Object, boolean, boolean, boolean, int, boolean).
      Parameters:
      tree - The tree in which this renderer component is used currently
      value - The value to be displayed for the tree cell to be rendered
      selected - Whether the tree cell to be rendered is selected
      expanded - Whether the tree cell to be rendered is expanded
      leaf - Whether the tree cell to be rendered is a leaf
      row - The row index of the tree cell to be rendered
      hasFocus - Whether the tree cell to be rendered has the focus