RT

解决方案 »

  1.   

    用渲染器啊。继承 TableCellRenderer 类,在 getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 方法中判断 column 参数,如果符合要求,就 setBackground 一下。然后获取你所说的特定列的 TableColumn 对象,调用 setCellRenderer(TableCellRenderer cellRenderer) 方法,
    把你建的 renderer 设置进去,就OK啦。
      

  2.   


        JTable table = new JTable();
        // Add data...    // Install the custom renderer on the first visible column
        int vColIndex = 0;
        TableColumn col = table.getColumnModel().getColumn(vColIndex);
        col.setCellRenderer(new MyTableCellRenderer());    public class MyTableCellRenderer implements TableCellRenderer
        {
            // This method is called each time a cell in a column
            // using this renderer needs to be rendered.
            public Component getTableCellRendererComponent(JTable table, Object value,
                            boolean isSelected, boolean hasFocus, int row, int column)
            {
                // specify the background of cell located at (1, 2)
                if ((row == 1) && (column == 2))
                {
                    setBackground(Color.GRAY);
                }            // 'value' is value contained in the cell located at
                // (rowIndex, vColIndex)
                // Configure the component with the specified value
                setText(value.toString());            // Since the renderer is a component, return itself
                return this;
            }
        }