dcsimg
December 6, 2016
Hot Topics:

User Code: Rendering a JTable Header

  • October 17, 2001
  • By Martin Hilpert
  • Send Email »
  • More Articles »

The following is a renderer for JTable headers that supports tooltips and icons.


     /**
     * To support column header icons and tool tips. :-)
     * Usage:
     * <P>
     * <PRE>
     *  MyHeaderRenderer mhr = new MyHeaderRenderer();
     *  mhr.setToolTip(toolTipText);
     *  mhr.setIcon(icon);
     *  TableColumn col = getColumnModel().getColumn(columnIndex);
     *  col.setHeaderRenderer(mhr);
     * </PRE>
     * This means that you need a new MyHeaderRenderer for each column. The alternative
     * would have been to use only one global renderer and do all the column (index)
     * mappings within this class which would save memory but would be slower. ;-)
     */
    class MyHeaderRenderer extends DefaultTableCellRenderer {

        /** label to display also an icon */
        JLabel label = null;

        /**
         */
        public MyHeaderRenderer() {
            super();
            label = new JLabel();
            label.setHorizontalAlignment(SwingConstants.CENTER);
            label.setOpaque(true);
        }//constructor

        /**
         * Overwrites DefaultTableCellRenderer.
         */
        public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected,
                                               boolean hasFocus, int row,
int column) {

            if (value != null) {
                label.setText("" + value);
            } else {
                label.setText("");
            }

            return label;
        }//getTableCellRendererComponent()

        /**
         * Overwrires DefaultTableCellRenderer.
         */
        protected void setValue(Object value) {
            if (value != null) {
                label.setText("" + value);
            } else {
                label.setText("");
            }
        }//setValue()

        /**
         * @param toolTip text to use for label's tool tip
         */
        public void setToolTip(String toolTip) {
            if (toolTip != null) {
                label.setToolTipText(toolTip);
            }
        }//setToolTip()

        /**
         * @param icon icon to set for the label
         */
        public void setIcon(Icon icon) {
            if (label != null) {
               label.setIcon(icon);
            }
        }//setIcon()

    }//MyHeaderRenderer






Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel