1

我有这样的 JTable

 String[] columnTitles=new String[]{"Command","Offset","Type","Value","Units","R/W"};
        Object[][] data=new Object[20][7];

        for(int i=0;i<ROWS_NUMBER;i++){

            cmbName=RootData.getRootData().getMessageContainer().getComboBoxNameTable();
            cmbOffset=RootData.getRootData().getMessageContainer().getComboBoxOffsetTable();
            txtType=new JTextField();
            txtValue=new JTextField();

            txtUnit=new JTextField();
            cmbRW=new JComboBox(new String[]{"Read","Write"});

            editorsCommand.add(new DefaultCellEditor(cmbName));
            editorsOffset.add(new DefaultCellEditor(cmbOffset));
            editorsType.add(new DefaultCellEditor(txtType));
            editorsValue.add(new DefaultCellEditor(txtValue));

            editorsUnits.add(new DefaultCellEditor(txtUnit));
            editorsRW.add(new DefaultCellEditor(cmbRW));
        }



        for(int i=0;i<20;i++)
            for(int j=0;j<7;j++)
                data[i][j]="";
        DefaultTableModel model=new DefaultTableModel(data,columnTitles);

        this.tblCommands=new JTable(model){
            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            public TableCellEditor getCellEditor(int row, int column)
            {
                int modelColumn = convertColumnIndexToModel( column );

                if (modelColumn == 0 && row < 20)
                    return editorsCommand.get(row);
                else if(modelColumn == 1 && row < 20)
                    return editorsOffset.get(row);
                else if(modelColumn == 2 && row < 20)
                    return editorsType.get(row);
                else if(modelColumn == 3 && row < 20)
                    return editorsValue.get(row);
                else if(modelColumn == 4 && row < 20)
                    return editorsUnits.get(row);
                else if(modelColumn == 5 && row < 20)
                    return editorsRW.get(row);
                else
                    return super.getCellEditor(row, column);
            }

        };


      tblCommands.getColumnModel().getColumn(0).setMinWidth(300);
      System.out.println(model.isCellEditable(0,2));
      spCommand=new JScrollPane(tblCommands);

如何在颜色绿色中为一行敌人示例 row=2 着色?

4

3 回答 3

3

典型的方法是让您的 CellRenderer 对象为您需要的行着色:http: //download.oracle.com/javase/tutorial/uiswing/components/table.html#renderer

在 camickr 的回答的第二部分可以找到另一种方法: 无论如何我可以在 JTable 中突出显示一行吗?

于 2011-05-19T11:29:40.190 回答
2

使用 TableCellRenderer搜索JTable中的垃圾邮件prepareRenderer链接,例如更改单元格颜色Table Row Rendering

于 2011-05-19T11:33:33.887 回答
2

请参阅表格行渲染

于 2011-05-19T15:31:32.447 回答