Home Forums FlexGantt HTML in Column Headers

  • Author
    Posts
  • Brian
    Participant
    Post count: 21
    #12793 |

    I’m trying to get the column header to render in 2 lines, one on top of the other. This component does not seem to support rendering HTML, but rather just displays the HTML on the screen

    for instance, I’m doing this…
    private static final TreeTableColumn col2 = new TreeTableColumn<>(MESSAGES”boo hoo“, Double.class, 2);

    I’ve also fooled with the headerObject and setting the name, etc, but still no luck

    I;ve also tried to make sure HTML support was not disabled, via..
    gChart.putClientProperty(“html.disable”, Boolean.FALSE);
    gChart.putClientProperty(“html.enable”, Boolean.TRUE);
    I’ve got a few hours into this now. Can you verify that this does, in fact, work.

  • Brian
    Participant
    Post count: 21
    #12794 |

    oops, a small cut/paste error in the above
    private static final TreeTableColumn col2 = new TreeTableColumn<>(MESSAGES”boo hoo”, Double.class, 2);
    should be
    private static final TreeTableColumn
    col2 = new TreeTableColumn<>(”boo hoo”, Double.class, 2);

  • Dirk Lemmermann
    Keymaster
    Post count: 64
    #12795 |

    In order to make renderers really really fast in Swing one has to override quite a few methods to disable them: all the repaint() methods, the firePropertyChange() methods, and the validate(), invalidate(), and revalidate() methods. However, when the firePropertyChange() methods are overriden then the HTML rendering no longer works. This is why the DefaultColumnHeaderRenderer does not render HTML. Below you will find the entire renderer code with the firePropertyChange() methods commented out. Used like this the header will indeed display HTML. So copy this code, rename it, and register it with the tree table control: TreeTableHeader.setColumnHeaderRenderer(Object.class, new MyRenderer()).

    Regards,

    Dirk

    
    /**
     * Dirk Lemmermann Software & Consulting
     * http://www.dlsc.com
     */
    package com.dlsc.flexgantt.swing.treetable;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import javax.swing.Icon;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
    
    import com.dlsc.flexgantt.model.treetable.SortDirection;
    import com.dlsc.flexgantt.model.treetable.TreeTableColumn;
    import com.dlsc.flexgantt.swing.util.ColorUtil;
    
    /**
     * The default colum header renderer displays the column's name. The renderer
     * looks up a header value via {@link TreeTableColumn#getHeaderValue()} and then
     * invokes {@link Object#toString()} on it.
     *
     * @author Dirk Lemmermann
     * @version $Revision: 1.11 $
     * @since 1.0
     */
    public class DefaultColumnHeaderRenderer extends JLabel implements
    		IColumnHeaderRenderer {
    
    	/*
    	 * Standard logger instance.
    	 */
    	private static final Logger LOGGER = Logger
    			.getLogger(DefaultColumnHeaderRenderer.class.getName());
    
    	/**
    	 * A foreground color used for visualizing that the colum header is the
    	 * focus owner.
    	 *
    	 * @since 1.0
    	 */
    	protected Color focusForeground = ColorUtil.getFocusForeground();
    
    	/**
    	 * A background color used for visualizing that the colum header is the
    	 * focus owner.
    	 *
    	 * @since 1.0
    	 */
    	protected Color focusBackground = ColorUtil.getFocusBackground();
    
    	/**
    	 * The direction in which the table column is sorted.
    	 *
    	 * @since 1.0
    	 */
    	protected SortDirection sortDirection;
    
    	/**
    	 * The font used for drawing the sort numbers (when using multi-column)
    	 * sort.
    	 *
    	 * @since 1.0
    	 */
    	protected Font sortOrderFont = new Font("courier", Font.PLAIN, 8); //$NON-NLS-1$
    
    	/**
    	 * The sort position, which is relevant when doing a multi-column sort.
    	 *
    	 * @since 1.0
    	 */
    	protected int sortPosition;
    
    	/**
    	 * A flag indicating whether the user is currently performing a sort based
    	 * on multiple columns.
    	 *
    	 * @since 1.0
    	 */
    	protected boolean multiColumnSort;
    
    	/**
    	 * Constructs a new header renderer.
    	 *
    	 * @since 1.0
    	 */
    	public DefaultColumnHeaderRenderer() {
    		setHorizontalAlignment(SwingConstants.CENTER);
    	}
    
    	/*
    	 * (non-Javadoc)
    	 *
    	 * @see com.dlsc.flexgantt.swing.treetable.IColumnHeaderRenderer#
    	 * getTreeTableColumnHeaderRendererComponent
    	 * (com.dlsc.flexgantt.swing.treetable.TreeTableHeader,
    	 * com.dlsc.flexgantt.model.treetable.TreeTableColumn, boolean, boolean,
    	 * int)
    	 */
    	public Component getColumnHeaderRendererComponent(TreeTableHeader header,
    			TreeTableColumn column, boolean selected, boolean hasFocus,
    			SortDirection sortDirection, int sortPosition) {
    		this.sortDirection = sortDirection;
    		this.sortPosition = sortPosition;
    		multiColumnSort = header.getSortCount() > 1;
    		setFont(header.getFont());
    		setOpaque(hasFocus);
    		setEnabled(header.isEnabled());
    
    		if (hasFocus) {
    			setForeground(focusForeground);
    			setBackground(focusBackground);
    		} else {
    			setForeground(header.getForeground());
    			setBackground(header.getBackground());
    		}
    		if (LOGGER.isLoggable(Level.FINER)) {
    			String sortString = "UNKNOWN"; //$NON-NLS-1$
    			switch (sortDirection) {
    			case NONE:
    				sortString = "SORTED_NONE"; //$NON-NLS-1$
    				break;
    			case ASCENDING:
    				sortString = "SORTED_ASCENDING"; //$NON-NLS-1$
    				break;
    			case DESCENDING:
    				sortString = "SORTED_DESCENDING"; //$NON-NLS-1$
    				break;
    
    			}
    			LOGGER.log(Level.FINER, "column = " + column + " selected = " //$NON-NLS-1$ //$NON-NLS-2$
    					+ selected + " focus = " + hasFocus + " sortDirection = " //$NON-NLS-1$ //$NON-NLS-2$
    					+ sortString);
    		}
    		Object headerValue = column.getHeaderValue();
    		if (headerValue != null) {
    			if (headerValue instanceof Icon) {
    				setIcon((Icon) headerValue);
    				setText(""); //$NON-NLS-1$
    			} else {
    				setIcon(null);
    				setText(headerValue.toString());
    			}
    		} else {
    			setIcon(null);
    			setText(column.getName());
    		}
    		return this;
    	}
    
    	@Override
    	protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2d = (Graphics2D) g.create();
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
    		g2d.setColor(getForeground());
    		int w = getWidth() - 10;
    		int h = getHeight();
    		int my = h / 2;
    		switch (sortDirection) {
    		case ASCENDING:
    			int[] xx2 = new int[] { w - 4, w + 4, w };
    			int[] yy2 = new int[] { my + 4, my + 4, my - 4 };
    			g2d.fillPolygon(xx2, yy2, 3);
    			break;
    		case DESCENDING:
    			int[] xx1 = new int[] { w - 4, w + 4, w };
    			int[] yy1 = new int[] { my - 4, my - 4, my + 4 };
    			g2d.fillPolygon(xx1, yy1, 3);
    			break;
    		case NONE:
    			break;
    		}
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_DEFAULT);
    		if (multiColumnSort && !sortDirection.equals(SortDirection.NONE)) {
    			g2d.setFont(sortOrderFont);
    			FontMetrics fm = g2d.getFontMetrics();
    			int ascent = fm.getAscent();
    			String str = Integer.toString(sortPosition);
    			int strWidth = fm.stringWidth(str);
    			g2d.drawString(str, w - strWidth / 2, my + 6 + ascent);
    		}
    		g2d.dispose();
    	}
    
    	/*
    	 * (non-Javadoc)
    	 *
    	 * @see javax.swing.JComponent#getPreferredSize()
    	 */
    	@Override
    	public Dimension getPreferredSize() {
    		Dimension dim = super.getPreferredSize();
    		dim.width += 32;
    		return dim;
    	}
    
    	/**
    	 * Returns the color used for the background of the column header when the
    	 * header is the focus owner.
    	 *
    	 * @return the focus background color
    	 * @since 1.0
    	 */
    	public Color getFocusBackground() {
    		return focusBackground;
    	}
    
    	/**
    	 * Sets the color used for the background of the column header when the
    	 * header is the focus owner.
    	 *
    	 * @param color
    	 *            the focus background color
    	 * @since 1.0
    	 */
    	public void setFocusBackground(Color color) {
    		this.focusBackground = color;
    	}
    
    	/**
    	 * Returns the color used for the foreground of the column header when the
    	 * header is the focus owner.
    	 *
    	 * @return the focus foreground color
    	 * @since 1.0
    	 */
    	public Color getFocusForeground() {
    		return focusForeground;
    	}
    
    	/**
    	 * Sets the color used for the foreground of the column header when the
    	 * header is the focus owner.
    	 *
    	 * @param color
    	 *            the focus foreground color
    	 * @since 1.0
    	 */
    	public void setFocusForeground(Color color) {
    		this.focusForeground = color;
    	}
    
    	/**
    	 * Overriden for performance reasons.
    	 *
    	 * @since 1.0
    	 */
    	@Override
    	public void validate() {
    	}
    
    	/**
    	 * Overriden for performance reasons.
    	 *
    	 * @since 1.0
    	 */
    	@Override
    	public void invalidate() {
    	}
    
    	/**
    	 * Overriden for performance reasons.
    	 *
    	 * @since 1.0
    	 */
    	@Override
    	public void revalidate() {
    	}
    
    //	/**
    //	 * Overriden for performance reasons.
    //	 *
    //	 * @since 1.0
    //	 */
    //	@Override
    //	public void firePropertyChange(String propertyName, boolean oldValue,
    //			boolean newValue) {
    //	}
    //
    //	/**
    //	 * Overriden for performance reasons.
    //	 *
    //	 * @since 1.0
    //	 */
    //	@Override
    //	public void firePropertyChange(String propertyName, byte oldValue,
    //			byte newValue) {
    //	}
    //
    //	/**
    //	 * Overriden for performance reasons.
    //	 *
    //	 * @since 1.0
    //	 */
    //	@Override
    //	public void firePropertyChange(String propertyName, char oldValue,
    //			char newValue) {
    //	}
    //
    //	/**
    //	 * Overriden for performance reasons.
    //	 *
    //	 * @since 1.0
    //	 */
    //	@Override
    //	public void firePropertyChange(String propertyName, double oldValue,
    //			double newValue) {
    //	}
    //
    //	/**
    //	 * Overriden for performance reasons.
    //	 *
    //	 * @since 1.0
    //	 */
    //	@Override
    //	public void firePropertyChange(String propertyName, float oldValue,
    //			float newValue) {
    //	}
    //
    //	/**
    //	 * Overriden for performance reasons.
    //	 *
    //	 * @since 1.0
    //	 */
    //	@Override
    //	public void firePropertyChange(String propertyName, long oldValue,
    //			long newValue) {
    //	}
    //
    //	/**
    //	 * Overriden for performance reasons.
    //	 *
    //	 * @since 1.0
    //	 */
    //	@Override
    //	protected void firePropertyChange(String propertyName, Object oldValue,
    //			Object newValue) {
    //	}
    //
    //	/**
    //	 * Overriden for performance reasons.
    //	 *
    //	 * @since 1.0
    //	 */
    //	@Override
    //	public void firePropertyChange(String propertyName, short oldValue,
    //			short newValue) {
    //	}
    
    	/**
    	 * Overriden for performance reasons.
    	 *
    	 * @since 1.0
    	 */
    	@Override
    	public void repaint() {
    	}
    
    	/**
    	 * Overriden for performance reasons.
    	 *
    	 * @since 1.0
    	 */
    	@Override
    	public void repaint(int x, int y, int width, int height) {
    	}
    
    	/**
    	 * Overriden for performance reasons.
    	 *
    	 * @since 1.0
    	 */
    	@Override
    	public void repaint(long tm) {
    	}
    
    	/**
    	 * Overriden in order to fix a bug in AWT, which causes the cursor to
    	 * flicker during drag and drop operations.
    	 *
    	 * @since 1.1
    	 */
    	@Override
    	public boolean isVisible() {
    		return false;
    	}
    }
    

You must be logged in to reply to this topic.