Example usage for javax.swing JTable getSelectionBackground

List of usage examples for javax.swing JTable getSelectionBackground

Introduction

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

Prototype

public Color getSelectionBackground() 

Source Link

Document

Returns the background color for selected cells.

Usage

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 {/*  ww  w  . ja v a  2 s.  c  o  m*/
        setForeground(table.getForeground());
        setBackground(table.getBackground());
    }
    setSelectedItem(value);
    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 {//from w w w .ja  v  a2  s  .  com
        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 {//from w  w  w .ja  v a 2  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.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  ww  w.  ja  v a  2 s  . c o  m

    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:com.digitalgeneralists.assurance.ui.renderers.ComparisonResultListRenderer.java

public java.awt.Component getTableCellRendererComponent(JTable list, Object value, boolean isSelected,
        boolean cellHasFocus, int row, int column) {
    ComparisonResultPanel panel = new ComparisonResultPanel((ComparisonResult) value);

    if (isSelected) {
        panel.setBackground(list.getSelectionBackground());
        panel.setForeground(list.getSelectionForeground());
    } else {/*  w  w w  .  j  a v a 2 s . c  o  m*/
        if ((row % 2.0) == 0) {
            panel.setBackground(Color.white);
        } else {
            panel.setBackground(Color.lightGray);
        }
    }

    list.setRowHeight(row, ((panel.getNumberOfLines() * 18) + 70));

    return panel;
}

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  w w. jav  a2  s . c om*/
        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:com.emr.schemas.ButtonColumn.java

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    if (isSelected) {
        renderButton.setForeground(table.getSelectionForeground());
        renderButton.setBackground(table.getSelectionBackground());
    } else {//from www .j  av a  2s .  com
        renderButton.setForeground(table.getForeground());
        renderButton.setBackground(UIManager.getColor("Button.background"));
    }

    if (hasFocus) {
        renderButton.setBorder(focusBorder);
    } else {
        renderButton.setBorder(originalBorder);
    }

    //      renderButton.setText( (value == null) ? "" : value.toString() );
    renderButton.setText(btnText);

    return renderButton;
}

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   www.  j a  v  a 2 s.c o  m
    }

    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:TableDialogEditDemo.java

public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus,
        int row, int column) {
    Color newColor = (Color) color;
    setBackground(newColor);/*from w  w w  . j ava2  s  .  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;
}

From source file:de.codesourcery.eve.skills.ui.components.impl.planning.ResourceStatusComponent.java

@Override
protected JPanel createPanel() {
    final JPanel result = new JPanel();

    table.setFillsViewportHeight(true);/* w  w  w .  ja va2s .c o  m*/

    table.setRowSorter(model.getRowSorter());

    FixedBooleanTableCellRenderer.attach(table);

    table.setDefaultRenderer(Integer.class, new DefaultTableCellRenderer() {
        @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 (!(value instanceof Integer)) {
                return this;
            }

            if (column == 0) {
                setHorizontalAlignment(SwingConstants.LEFT);
            } else {
                setHorizontalAlignment(SwingConstants.RIGHT);
            }

            Integer amount = (Integer) value;
            if (amount.intValue() < 0) {
                if (!isSelected) {
                    setBackground(Color.RED);
                } else {
                    setBackground(table.getSelectionBackground());
                }
            } else {
                setBackground(table.getBackground());
            }
            return this;
        }
    });

    addToShoppingListButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final List<ItemWithQuantity> items = model.getSelectedItems();

            if (items.isEmpty()) {
                return;
            }

            final ShoppingListEditorComponent comp = new ShoppingListEditorComponent(title, "", items);

            comp.setModal(true);
            ComponentWrapper.wrapComponent(comp).setVisible(true);
            if (!comp.wasCancelled() && !comp.getShoppingList().isEmpty()) {
                shoppingListManager.addShoppingList(comp.getShoppingList());
                getComponentCallback().dispose(ResourceStatusComponent.this);
            }

        }
    });

    new GridLayoutBuilder()
            .add(new GridLayoutBuilder.VerticalGroup(new GridLayoutBuilder.Cell(new JScrollPane(table)),
                    new GridLayoutBuilder.FixedCell(addToShoppingListButton)))
            .addTo(result);
    return result;
}