我写了一个JTable,其中第一列被render成了checkbox,但是当每次选中某一行的时候,只有checkbox这一列没有高亮显示。怎么做才能让一整行都高亮显示选中? 
是用isSelected吗?怎么用?多谢!

解决方案 »

  1.   

    isSelected: 目标单元格当前如果是选中的,则为true,反之为false,不高亮显示跟render成了checkbox应该没什么关系,把table的选择模式设为table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);看看,再不行就看一个这个吧http://community.csdn.net/Expert/topic/4131/4131260.xml?temp=.5156061
      

  2.   

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
            if (isSelected) {
                this.setForeground(table.getSelectionForeground());
                this.setBackground(table.getSelectionBackground());
            }
            else {
                this.setForeground(table.getForeground());
                this.setBackground(table.getBackground());
            }
      .................
      

  3.   

    多谢 fog628(无名) !
    我已经设定了table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);但是当选中一整行的时候,仍然是那一列checkbox没有变颜色。
      

  4.   

    Acylas(Acylas)多谢!
    我没有说清楚,我不是想要checkbox选中前后颜色发生变化,而是想如果如果选中一整行,那列包含checkbox的颜色应该是高亮显示的。我会先看看sun的例子。
      

  5.   

    因为table中保存checkbox的那一列有些值是空值,这时需要render成两个横线“--”,而不是checkbox。我的程序如下:
    class CheckCellRenderer implements TableCellRenderer {  /**
       * Component that renders a null cell.
       */
      JLabel nullCell;  /**
       * Component that renders a selected check box.
       */
      JCheckBox selectedCheckBox;  /**
       * Component that renders a non-selected check box.
       */
      JCheckBox nonSelectedCheckBox;
      CheckCellRenderer(JTable table) {    nullCell = new JLabel("--");
        nullCell.setHorizontalAlignment(SwingConstants.CENTER);    selectedCheckBox = new JCheckBox();
        selectedCheckBox.setSelected(true);
        selectedCheckBox.setHorizontalAlignment(SwingConstants.CENTER);
        selectedCheckBox.setBackground(table.getBackground());    nonSelectedCheckBox = new JCheckBox();
        nonSelectedCheckBox.setSelected(false);
        nonSelectedCheckBox.setHorizontalAlignment(SwingConstants.CENTER);
        nonSelectedCheckBox.setBackground(table.getBackground());
      }
      public Component getTableCellRendererComponent(JTable table,
                                                     Object value,
                                                     boolean isSelected,
                                                     boolean hasFocus,
                                                     int row, int column) {
        if (value instanceof Boolean) {
          Boolean checked = (Boolean)value;
          if (checked.booleanValue()) {
            return selectedCheckBox;
          }
          else {
            return nonSelectedCheckBox;
          }
        }
        else {
          return nullCell;
        }
      }
    }因为已经设定了整个checkbox的背景色,而且占满了cell的空间,因此当选中一行的时候,checkbox那一列没有高亮显示。有什么办法解决吗?是不是只能监听ListSelectionEvent,然后据此来设定所有checkbox的背景色?
      

  6.   

    table.setSelectionMode
    和我那个设置一起加上就可以实现。
    isSelected表示当前这个单元格是否选中,而不是表示这个单元格的值。
      

  7.   

    多谢Acylas回复!
    可是我上面写的那个CheckCellRenderer 没有setForeground方法,所以会出错
      

  8.   

    搞定了!多谢各位帮助。但是我写得比较笨,哪里可以简化?:)
      public Component getTableCellRendererComponent(JTable table,
                                                     Object value,
                                                     boolean isSelected,
                                                     boolean hasFocus,
                                                     int row, int column) {
        if (value instanceof Boolean) {
          Boolean checked = (Boolean)value;
          if (checked.booleanValue()) {
            if (isSelected) {
                selectedCheckBox.setForeground(table.getSelectionForeground());
                selectedCheckBox.setBackground(table.getSelectionBackground());
            }
            else {
                selectedCheckBox.setForeground(table.getForeground());
                selectedCheckBox.setBackground(table.getBackground());
            }
                      
            return selectedCheckBox;
          }
          else {
              if (isSelected) {
                  nonSelectedCheckBox.setForeground(table.getSelectionForeground());
                  nonSelectedCheckBox.setBackground(table.getSelectionBackground());
              }
              else {
                  nonSelectedCheckBox.setForeground(table.getForeground());
                  nonSelectedCheckBox.setBackground(table.getBackground());
              }
            return nonSelectedCheckBox;
          }
        }
        else {
            if (isSelected) {
                nullCell.setForeground(table.getSelectionForeground());
                nullCell.setBackground(table.getSelectionBackground());
            }
            else {
                nullCell.setForeground(table.getForeground());
                nullCell.setBackground(table.getBackground());
            }
           
            return nullCell;
        }
      }