简体   繁体   中英

Color specific row by rowid

I have JTable like this

 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);

How to color one row foe example row=2 in Color green?

The typical method is just to have your CellRenderer objects color the rows you need: http://download.oracle.com/javase/tutorial/uiswing/components/table.html#renderer

Another approach can be found in the second part of camickr's answer here: Is there anyway I can highlight a row in JTable?

搜索prepareRenderer例如如下链接Table Row Rendering通过从trashgod 使用的TableCellRenderer JTable的变单元的色彩

请参阅表格行渲染

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM