Example usage for javax.swing JTable getBackground

List of usage examples for javax.swing JTable getBackground

Introduction

In this page you can find the example usage for javax.swing JTable getBackground.

Prototype

@Transient
public Color getBackground() 

Source Link

Document

Gets the background color of this component.

Usage

From source file:Main.java

/**
 * Setups the given table for usage as row-header. This method setups the background color to
 * the same one than the column headers.
 *
 * {@note In a previous version, we were assigning to the row headers the same cell renderer than
 *        the one created by <cite>Swing</cite> for the column headers. But it produced strange
 *        effects when the L&F uses a vertical grandiant instead than a uniform color.}
 *
 * @param  table The table to setup as row headers.
 * @return The renderer which has been assigned to the table.
 *//*from  w  ww  .ja va 2  s. c  o m*/
public static TableCellRenderer setupAsRowHeader(final JTable table) {
    final JTableHeader header = table.getTableHeader();
    Color background = header.getBackground();
    Color foreground = header.getForeground();
    if (background == null || background.equals(table.getBackground())) {
        if (!SystemColor.control.equals(background)) {
            background = SystemColor.control;
            foreground = SystemColor.controlText;
        } else {
            final Locale locale = table.getLocale();
            background = UIManager.getColor("Label.background", locale);
            foreground = UIManager.getColor("Label.foreground", locale);
        }
    }
    final DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setBackground(background);
    renderer.setForeground(foreground);
    renderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT);
    final TableColumn column = table.getColumnModel().getColumn(0);
    column.setCellRenderer(renderer);
    column.setPreferredWidth(60);
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setCellSelectionEnabled(false);
    return renderer;
}

From source file:Main.java

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int rowIndex, int vColIndex) {
    if (curColor instanceof Color) {
        curColor = (Color) value;
    } else {//from w  ww . j  a  va  2  s.c  o  m
        curColor = table.getBackground();
    }
    return this;
}

From source file:Main.java

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    if (isSelected) {
        setForeground(table.getSelectionForeground());
        super.setBackground(table.getSelectionBackground());
    } else {//from   w  ww . j  a  v  a  2s  . c  o  m
        setForeground(table.getForeground());
        setBackground(table.getBackground());
    }
    setSelectedItem(value);
    return this;
}

From source file:com.sander.verhagen.frame.LineWrapCellRenderer.java

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    String text = value.toString();
    setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
    setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
    int columnWidth = table.getColumnModel().getColumn(column).getWidth();
    setSize(columnWidth, 0 /* don't know yet */);
    setText(text);//from   w w w .  j  a  v a  2s.  c om

    int fontHeight = this.getFontMetrics(this.getFont()).getHeight();
    if (StringUtils.isEmpty(text)) {
        table.setRowHeight(row, fontHeight);
    } else {
        table.setRowHeight(row, fontHeight * getLineCount(this));
    }
    return this;
}

From source file:GenderRenderer.java

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {

    if (isSelected) {
        setForeground(table.getSelectionForeground());
        super.setBackground(table.getSelectionBackground());
    } else {/* w ww.  j a v  a  2  s. co  m*/
        setForeground(table.getForeground());
        setBackground(table.getBackground());
    }

    boolean isMale = ((Boolean) value).booleanValue();
    setSelectedIndex(isMale ? 0 : 1);
    return this;
}

From source file:com.mirth.connect.client.ui.ImageCellRenderer.java

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    if (isSelected) {
        setForeground(table.getSelectionForeground());
        super.setBackground(table.getSelectionBackground());
    } else {//  w w w. j  av a2  s .  co  m
        setForeground(table.getForeground());
        setBackground(table.getBackground());
    }

    // MIRTH-2186 Java sometimes calls this method with a hard-coded null value so tablecellrenderers must explicitly check for null.
    // Only observed so far on Mac OSX.
    // http://stackoverflow.com/questions/3054775/java-swing-jtable-strange-behavior-from-getaccessiblechild-method-resulting
    if (value != null) {
        CellData data = (CellData) value;

        setText(data.getText());

        setIcon(data.getIcon());
        setHorizontalAlignment(LEFT);

        if (data.getIcon() == null && StringUtils.isEmpty(data.getText())) {
            setHorizontalAlignment(CENTER);
            setText("--");
        }
    }

    return this;
}

From source file:com.github.alexfalappa.nbspringboot.navigator.RequestMethodCellRenderer.java

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    if (!isSelected) {
        if (value == null) {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        } else if (value instanceof RequestMethod) {
            final RequestMethod reqMethod = (RequestMethod) value;
            setForeground(fgColors.get(reqMethod));
            setBackground(bgColors.get(reqMethod));
        }//from w ww . ja  va  2  s . c o m
    }
    return this;
}

From source file:ExpenseReport.java

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    if (value instanceof Boolean) {
        Boolean b = (Boolean) value;
        setSelected(b.booleanValue());/*from w  w w .j a v  a 2 s  .c  om*/
    }

    setBackground(isSelected && !hasFocus ? table.getSelectionBackground() : table.getBackground());
    setForeground(isSelected && !hasFocus ? table.getSelectionForeground() : table.getForeground());

    setFont(table.getFont());
    setBorder(hasFocus ? UIManager.getBorder("Table.focusCellHighlightBorder") : m_noFocusBorder);

    return this;
}

From source file:ExtendedTableCellRenderer.java

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 {//from w  ww  .  j  av a 2  s .c o  m
        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;
}

From source file:TableDialogEditDemo.java

public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus,
        int row, int column) {
    Color newColor = (Color) color;
    setBackground(newColor);/*from  www.j  a va 2s.  c  o  m*/
    if (isBordered) {
        if (isSelected) {
            if (selectedBorder == null) {
                selectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5, table.getSelectionBackground());
            }
            setBorder(selectedBorder);
        } else {
            if (unselectedBorder == null) {
                unselectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5, table.getBackground());
            }
            setBorder(unselectedBorder);
        }
    }

    setToolTipText("RGB value: " + newColor.getRed() + ", " + newColor.getGreen() + ", " + newColor.getBlue());
    return this;
}