Scrollable Table Cell Renderer : Tree Renderer Editor « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing JFC » Tree Renderer Editor 




Scrollable Table Cell Renderer
   
/*
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
    License as published by the Free Software Foundation; either
    version 2 of the license, or (at your option) any later version.
*/


import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.Scrollable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;


/**
    A <tt>TableCellRenderer</tt> which is based on an <tt>ExtendedJLabel</tt> rather than
    a <tt>JLabel</tt> like the <tt>javax.swing.table.DefaultTableCellRenderer</tt>.

    @author <a href="mailto:[email protected]">Ingo Kegel</a>
    @version $Revision: 1.4 $ $Date: 2003/08/18 07:48:28 $
*/
public class ExtendedTableCellRenderer extends ExtendedJLabel
                                       implements TableCellRenderer {

    private static final Border NO_FOCUS_BORDER = new EmptyBorder(1111);

    private Color unselectedForeground;
    private Color unselectedBackground;

    /**
     * Constructor.
     */
    public ExtendedTableCellRenderer() {
        super();
        setOpaque(true);
        setBorder(NO_FOCUS_BORDER);
    }

    public void setForeground(Color c) {
        super.setForeground(c)
        unselectedForeground = c; 
    }
    
    public void setBackground(Color c) {
        super.setBackground(c)
        unselectedBackground = c; 
    }

    public void updateUI() {
        super.updateUI()
        setForeground(null);
        setBackground(null);
    }
    
    public Component getTableCellRendererComponent(JTable table,
                                                   Object value,
                                                   boolean isSelected,
                                                   boolean hasFocus,
                                                   int row,
                                                   int column)
    {
        if (isSelected) {
           super.setForeground(table.getSelectionForeground());
           super.setBackground(table.getSelectionBackground());

        else {
            super.setForeground((unselectedForeground != null? unselectedForeground : table.getForeground());
            super.setBackground((unselectedBackground != null? unselectedBackground : table.getBackground());
        }

        setFont(table.getFont());

        if (hasFocus) {
            setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
            if (table.isCellEditable(row, column)) {
                super.setForeground(UIManager.getColor("Table.focusCellForeground"));
                super.setBackground(UIManager.getColor("Table.focusCellBackground"));
            }
        else {
            setBorder(NO_FOCUS_BORDER);
        }

        setValue(value)

        Color background = getBackground();
        boolean colorMatch = (background != null&& (background.equals(table.getBackground())) && table.isOpaque();
        setOpaque(!colorMatch);

        return this;
    }
    

    public void validate() {}

    public void revalidate() {}

    public void repaint(long tm, int x, int y, int width, int height) {}

    public void repaint(Rectangle r) { }

    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
        if (propertyName == "text") {
            super.firePropertyChange(propertyName, oldValue, newValue);
        }
    }

    public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }


    private void setValue(Object value) {
        setText((value == null"" : value.toString());
    }
    

}



/*
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
    License as published by the Free Software Foundation; either
    version 2 of the license, or (at your option) any later version.
*/

 class ExtendedJLabel extends JLabel implements Scrollable {

    private boolean underlined = false;
    private boolean autoTooltip = false;

    /**
        Constructor.
     */
    public ExtendedJLabel() {
    }

    /**
        Constructor.
        @param text the label text.
     */
    public ExtendedJLabel(String text) {
        super(text);
    }
    
    public Dimension getPreferredScrollableViewportSize() {
        return getSize();
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return getWidth() 10;
    }
    
    public boolean getScrollableTracksViewportWidth() {
        return false;
    }
    
    public boolean getScrollableTracksViewportHeight() {
        return false;
    }
    
    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 10;
    }
    
    /**
        Check whether this label is underlined.
        @return underlined or not
     */
    public boolean isUnderlined() {
        return underlined;
    }
    
    /**
        Set whether this label is underlined.
        @param underlined underlined or not
     */
    public void setUnderlined(boolean underlined) {
        this.underlined = underlined;
        repaint();
    }
    
    /**
        Check whether the tooltip text is automatically equal
        to the text of this label or not.
        @return equal or not
     */
    public boolean getAutoTooltip() {
        return autoTooltip;
    }

    /**
        Set whether the tooltip text is automatically equal
        to the text of this label or not.
        @param autoTooltip equal or not
     */
    public void setAutoTooltip(boolean autoTooltip) {
        this.autoTooltip = autoTooltip;
        setToolTipText(getText());
    }
    
    public void setText(String text) {
        super.setText(text);
        if (autoTooltip) {
            setToolTipText(text);
        }
    }
    
    /**
        Convenience method for calling <tt>setText()</tt> with a <tt>short</tt>.
        @param number the <tt>short</tt>
     */
    public void setText(short number) {
        setText(String.valueOf(number));
    }

    /**
        Convenience method for calling <tt>setText()</tt> with a <tt>int</tt>.
        @param number the <tt>int</tt>
     */
    public void setText(int number) {
        setText(String.valueOf(number));
    }

    /**
        Convenience method for calling <tt>setText()</tt> with a <tt>double</tt>.
        @param number the <tt>double</tt>
     */
    public void setText(double number) {
        setText(String.valueOf(number));
    }

    /**
        Convenience method for calling <tt>setText()</tt> with a <tt>float</tt>.
        @param number the <tt>float</tt>
     */
    public void setText(float number) {
        setText(String.valueOf(number));
    }

    /**
        Convenience method for calling <tt>setText()</tt> with a <tt>long</tt>.
        @param number the <tt>long</tt>
     */
    public void setText(long number) {
        setText(String.valueOf(number));
    }

    public void paint(Graphics g) {
        super.paint(g);
        
        if (underlined) {
            Insets i = getInsets();
            FontMetrics fm = g.getFontMetrics();

            Rectangle textRect = new Rectangle();
            Rectangle viewRect = new Rectangle(i.left, i.top, getWidth() (i.right + i.left), getHeight() (i.bottom + i.top) );

            SwingUtilities.layoutCompoundLabel(
                                this,
                                fm,
                                getText(),
                                getIcon(),
                                getVerticalAlignment(),
                                getHorizontalAlignment(),
                                getVerticalTextPosition(),
                                getHorizontalTextPosition(),
                                viewRect,
                                new Rectangle(),
                                textRect,
                                getText() == null ((Integer)UIManager.get("Button.textIconGap")).intValue()
                          );


            int offset = 2;
            if (UIManager.getLookAndFeel().isNativeLookAndFeel() && System.getProperty("os.name").startsWith("Windows")) {
                offset = 1;
            }
            g.fillRect(textRect.x + ((Integer)UIManager.get("Button.textShiftOffset")).intValue() ,
                       textRect.y + fm.getAscent() ((Integer)UIManager.get("Button.textShiftOffset")).intValue() + offset,
                       textRect.width,
                       1);
        }
    }
}

   
    
    
  














Related examples in the same category
1.Tree Cell RendererTree Cell Renderer
2.CheckBox Node Tree SampleCheckBox Node Tree Sample
3.Combobox Tree Node EditorCombobox Tree Node Editor
4.extends DefaultTreeCellEditor to create Tree Leaf editorextends DefaultTreeCellEditor to create Tree Leaf editor
5.Tree Changed RendererTree Changed Renderer
6.Build a tree and populate it with custom renderers and editorsBuild a tree and populate it with custom renderers and editors
7.Using DefaultTreeCellRenderer
8.This program demonstrates cell rendering and listening to tree selection events.This program demonstrates cell rendering and listening to tree selection events.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.