按 rowid 为特定行着色

发布于 2024-11-08 02:59:38 字数 2476 浏览 0 评论 0原文

我有这样的 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);

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无边思念无边月 2024-11-15 02:59:38

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

另一种方法可以在 camickr 答案的第二部分中找到:
我是否可以突出显示一行在 JTable 中?

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?

心不设防 2024-11-15 02:59:38

例如,搜索 prepareRenderer ,遵循由trashgod 提供的链接 Table Row Rendering,来自 JTable 使用 TableCellRenderer 更改单元格颜色

search for prepareRenderer for example follows link Table Row Rendering by trashgod from JTable change cell colors using TableCellRenderer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文