Изменение цвет ячейки JTable

$sql = "SELECT " .$varible. " FROM information_schema.tables where table_schema='folder'";
$result = $conn->query($sql);
7
задан Camilo Díaz Repka 3 May 2009 в 23:28
поделиться

1 ответ

In the first parameter for setDefaultRenderer, put the class literal for the Class that you want to override rendering. I.e., if your data consist all of strings, you can put

myTable.setDefaultRenderer(String.class, new CustomRenderer());

If your data also consists of values with BigDecimal or Integer as classes, you have to invoke that method several times for each class type (BigDecimal.class or Integer.class in each case).

And finally, to change the background color you do this in your renderer:

class CustomRenderer extends DefaultTableCellRenderer 
{
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        c.setBackground(new java.awt.Color(255, 72, 72));
        return c;
    }
}

If you write a renderer that should work for all classes of an interface, you will also need to modify the getColumnClass function of your table model and let it return the interface class for all objects that implement this interface:

public Class<? extends Object> getColumnClass(int c) {
    Object object = getValueAt(0, c);
    if(object == null) {
        return Object.class;
    if(getValueAt(0, c) instanceof IColorable) {
        return ICarPart.class;
    } else {
        return getValueAt(0, c).getClass();
    }
}

With that one can register a renderer for IColorable.class and does not need to register a separate renderer for each implementation.

13
ответ дан 6 December 2019 в 19:41
поделиться
Другие вопросы по тегам:

Похожие вопросы: