You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by Edgar Merino <do...@gmail.com> on 2009/07/04 04:54:34 UTC

Custom TableView.CellRenderer

Hello,  

    I've tried to create a custom CellRenderer by extending a FlowPane 
(test code below), however none of the components inside the container 
are painted, I've tried to look at the code but I don't see why it 
wouldn't paint (in the end, it ends up calling CellRenderer#paint(), 
which turns out to be FlowPane#paint()). Maybe I missed something? I 
tried with other components like ImageView and it rendered without 
problem, any hint?


Edgar Merino


NOTE: tableData is not actually important in this test code, only length is
CustomCellRenderer.java

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.FlowPane;
import org.apache.pivot.wtk.HorizontalAlignment;
import org.apache.pivot.wtk.Insets;
import org.apache.pivot.wtk.Label;
import org.apache.pivot.wtk.Orientation;
import org.apache.pivot.wtk.TableView;
import org.apache.pivot.wtk.TableView.Column;
import org.apache.pivot.wtk.VerticalAlignment;

/**
 *
 * @author emerino
 */
public class CustomCellRenderer extends FlowPane
        implements TableView.CellRenderer {

    private final Font primaryFont;
    private final Font secondaryFont;   
    private Label label1;
    private Label label2;

    public CustomCellRenderer() {
        getStyles().put("verticalAlignment", VerticalAlignment.JUSTIFY);
        getStyles().put("horizontalAlignment", HorizontalAlignment.JUSTIFY);
        getStyles().put("padding", new Insets(3));
        setOrientation(Orientation.VERTICAL);

        this.primaryFont = Font.decode("Sans bold 16");
        this.secondaryFont = Font.decode("Sans 12");       

        label1 = new Label("MESSAGE one");
        label1.getStyles().put("font", primaryFont);
        add(label1);

        label2 = new Label("below, secondary");
        label2.setText("adiĆ³s");
        label2.getStyles().put("font", secondaryFont);
        add(label2);                       
    }

    public void render(Object value, TableView tableView, Column column,
            boolean rowSelected, boolean rowHighlighted, boolean 
rowDisabled) {
       
        renderStyles(label1, tableView, rowSelected, rowDisabled);
        renderStyles(label2, tableView, rowSelected, rowDisabled);       
    }
  
    protected void renderStyles(Label label, TableView tableView, 
boolean rowSelected, boolean rowDisabled) {
        Component.StyleDictionary tableViewStyles = tableView.getStyles();
        Component.StyleDictionary styles = label.getStyles();

        Color color;
        if (tableView.isEnabled() && !rowDisabled) {
            if (rowSelected) {
                if (tableView.isFocused()) {
                    color = (Color)tableViewStyles.get("selectionColor");
                } else {
                    color = 
(Color)tableViewStyles.get("inactiveSelectionColor");
                }
            } else {
                color = (Color)tableViewStyles.get("color");
            }
        } else {
            color = (Color)tableViewStyles.get("disabledColor");
        }

        styles.put("color", color);
    }
}