简体   繁体   English

JTable中的选定行未突出显示

[英]Selected row in JTable is not highlighting

Well, this is my first post here. 好吧,这是我在这里的第一篇文章。 But I am little frustrated because I have search everywhere but nothing is working. 但是我有点沮丧,因为我到处都有搜索,但是什么都没用。 I have JTable and code works as expected below lines right after if ( value.equals("CMAU1294522") ) .. however only one cell is show square box. 我有JTable并且代码在if( value.equals("CMAU1294522") )之后value.equals("CMAU1294522")在下面的行中工作,但是只有一个单元格显示方形框。 I mouse clicked on a particular row, I want the entire row to show light gray (I think that is standard). 我用鼠标单击了特定的行,我希望整行显示为浅灰色(我认为这是标准的)。

table = new JTable(sorter)  {  
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
        Component comp = super.prepareRenderer(renderer, row, col);
        Object value = getModel().getValueAt(row, col);


        UIManager.put("Table.editRow", new javax.swing.plaf.ColorUIResource(Color.YELLOW)); 


      if(editingRow ==1){
          table.setSelectionBackground(Color.red);
        }
        if (getSelectedRow() == row) {
            table.setSelectionBackground(Color.red);
        }
        if (value.equals("CMAU1294522")) {
            comp.setBackground(Color.red);
        } else if (value.equals("PNCT")) {
            comp.setBackground(Color.green);
        } else if (NewJApplet.contReady.containsKey(value)) {
            comp.setBackground(Color.CYAN);
        } else if (NewJApplet.badCont.containsKey(value)) {
            comp.setBackground(Color.red);
        } else {
            comp.setBackground(Color.white);
        }
        return comp;
    }

Is there any way to do it in prepareRenderer function I already have? 在我已经拥有的prepareRenderer函数中,有什么办法可以做到?

I there any way to do it in PreparRenerere function I already have? 我有什么办法可以在已经拥有的PreparRenerere函数中进行操作吗?

You code to get the "value" is wrong. 您编写的代码获取“值”是错误的。 You always get the value of the current cell being rendered. 您总是会得到当前正在渲染的单元格的值。 If you want to highlight the entire row based on a specific value then you need to hardcode the column: 如果要基于特定值突出显示整个行,则需要对列进行硬编码:

Object value = getModel().getValueAt(row, ???);

Also, it looks like you are sorting the data in the table so you should be using getValueAt(...) instead of getModel().getValueAt(), so you check the data in the sort table, not the data in the unsorted model. 另外,看起来您正在对表中的数据进行排序,因此您应该使用getValueAt(...)而不是getModel()。getValueAt(),以便检查排序表中的数据,而不是未排序的数据模型。

Check out the example code in Table Row Rendering for a working example. 请查看“ 表行渲染”中的示例代码以获取可用示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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