单击按钮后如何更改JTable行的颜色

时间:2015-10-22 09:12:08

标签: java swing jtable tablecellrenderer

我想在点击按钮后更改JTable行的颜色。我发现了许多示例,展示了如何使用不同颜色的行启动表,以及如何在选择行时更改颜色。但是,我想知道点击JButton时改变颜色的正确方法是什么。

TableCellRenderer colorRenderer = new ColorRenderer();
table.setDefaultRenderer(String.class, colorRenderer);

private class ColorRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = 1L;

    @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 (hasFocus) {
            setBackground(Color.cyan);
        } else if (isSelected) {
            setBackground(table.getSelectionBackground());
        } else {
            setBackground(table.getBackground());
        }
        return this;
    }
}

2 个答案:

答案 0 :(得分:1)

考虑到您要将数据加载到JTable中:

public void fillTable(){
       List<String> columns = new ArrayList<String>();
       List<String[]> values = new ArrayList<String[]>();
        columns.add("col1");
        columns.add("col2");
        columns.add("col3");
        for (int i = 0; i < 100; i++) {
            values.add(new String[] {"val"+i+" col1","val"+i+" col2","val"+i+" col3"});
        }
        TableModel tableModel = new DefaultTableModel(values.toArray(new Object[][] {}), columns.toArray());
        tableName.setModel(tableModel);
    }

然后,您可以像使用此类一样将渲染设置为JTable

public class EvenOddRenderer implements TableCellRenderer {

        public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();

        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            DEFAULT_RENDERER.setHorizontalAlignment(JLabel.CENTER);
            Component renderer = DEFAULT_RENDERER.getTableCellRendererComponent(
                    table, value, isSelected, hasFocus, row, column);
            ((JLabel) renderer).setOpaque(true);
            Color foreground, background;
            Color alternate = new Color(0xC0, 0xC0, 0xF0);
            Color lightBlue = new Color(204, 204, 255);

            if (isSelected) {
                foreground = Color.black;
                background = Color.gray;
            } else {
                if (row % 2 == 0) {
                    foreground = Color.black;
                    background = Color.white;
                } else {
                    foreground = Color.black;
                    background = lightBlue;
                }
            }
            renderer.setForeground(foreground);
            renderer.setBackground(background);
            return renderer;
        }
    }

像这样:

  TableCellRenderer renderer = new EvenOddRenderer();
  tableName.setDefaultRenderer(Object.class, renderer);
  tableName.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

JTable on load

然后您可以使用setRowSelectionInterval()方法选择任何行:

     private void btn(java.awt.event.ActionEvent evt) { 
      int index = 0; 
      tableName.setRowSelectionInterval(index, index);
     }

您在此处选择JTable

中的第一行

JTable after button click

在此示例中,您选择Color.gray作为JTable选择颜色

你可以肯定地改变它

答案 1 :(得分:0)

查看Table Row Rendering

此方法也适用于每列中包含不同数据类型的表,因此您不必为每种类型的数据创建自定义渲染器。