2017-10-17 174 views
0

我在scrollPane上有一些表格,我的一些单元格被涂成不同的颜色。当我点击某一行的单元格时,或者行应该改变它们的颜色,但只有当我滚动我的表时,如果多行应该着色,重绘才能正常工作。 如何通过重写getTableCellRenderrerComponent()来重绘单元格后对JTable进行完整更新? 谢谢。我应该如何刷新表格?

我的表类:

class MyGrid extends JTable { 
    private TNotifyEvent onCustomDrawCell; 

    public MyGrid() { 
     this.setSelectionMode(0); 
    } 

    public TNotifyEvent getOnCustomDrawCell() { 
     return this.onCustomDrawCell; 
    } 

    public void setOnCustomDrawCell(TNotifyEvent onCustomDrawCell) { 
     this.onCustomDrawCell = onCustomDrawCell; 
    } 
} 

使用我的表

MyGrid table = new MyGrid(); 
JScrollPane scroll = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); 

table.setOnCustomDrawCell(() -> { 
     for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) { 
      table.getColumnModel().getColumn(i).setCellRenderer(new DefaultTableCellRenderer() { 
       JLabel lbl = new JLabel(); 

       @Override 
       public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 
                   boolean hasFocus, int row, int column) { 
        lbl = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 
        lbl.setText(String.valueOf(value)); 
        MyItem item = new MyItem(); 
        item.setValue((Vector) model.getDataVector().get(row)); 
        item.setCanvas(lbl); 
        MutableBoolean aDoneMutable = new MutableBoolean(false); 
        getContentStyle(null, item, item.getCanvas(), null); 
        drawCells(null, item.getCanvas(), item, aDoneMutable); 
        return lbl; 
       } 
      }); 
     } 
    return true; 
    }); 

    public static void getContentStyle(Object Sender, MyItem ARecord, JLabel AItem, TcxStyle AStyle) { 
    try { 
     if (Integer.parseInt(String.valueOf(ARecord.getValue().get(3))) == 0) { 
      AItem.setOpaque(true); 
      AItem.setBackground(Color.GRAY); 
     } else if (ARecord.getValue().get(4).equals("222")) { 
      AItem.setOpaque(true); 
      AItem.setBackground(Color.PINK); 
     } else { 
      AItem.setOpaque(true); 
      AItem.setBackground(null); 
     } 
    } catch (Exception e) { 
    } 
} 

public static void drawCells(Object Sender, JLabel ACanvas, MyItem AViewInfo, final MutableBoolean mutableADone) { 
    boolean ADone = mutableADone.getValue(); 
    try { 
     if((AViewInfo.getValue().get(4).equals("44")) && (!AViewInfo.isSelected())) { 
      ACanvas.setForeground(Color.black); 
      ACanvas.setOpaque(true); 
      ACanvas.setBackground(PictureUtils.createColorfromString("$CCE6FF")); 
      AViewInfo.setCanvas(ACanvas); 
     } else { 
      if((AViewInfo.getValue().get(5).equals("555")) && (AViewInfo.isSelected()) && (AViewInfo.isHasFocus())) { 
       ACanvas.setOpaque(true); 
       ACanvas.setBackground(PictureUtils.createColorfromString("$CCE6FF")); 
       AViewInfo.setCanvas(ACanvas); 
      } 
     } 
    } catch (Exception e) {} 
    mutableADone.setValue(ADone); 
} 

而且myItem类:

class MyItem { 
    private String columnName; 
    private Vector value; 
    private JLabel canvas; 
    private Integer index; 
    private boolean isSelected; 
    private boolean hasFocus; 

    public MyItem() { 
    } 

    public String getColumnName() { 
     return this.columnName; 
    } 

    public void setColumnName(String columnName) { 
     this.columnName = columnName; 
    } 

    public Vector getValue() { 
     return this.value; 
    } 

    public void setValue(Vector value) { 
     this.value = value; 
    } 

    public JLabel getCanvas() { 
     return this.canvas; 
    } 

    public void setCanvas(JLabel canvas) { 
     this.canvas = canvas; 
    } 

    public boolean isSelected() { 
     return this.isSelected; 
    } 

    public void setSelected(boolean selected) { 
     this.isSelected = selected; 
    } 

    public boolean isHasFocus() { 
     return this.hasFocus; 
    } 

    public void setHasFocus(boolean hasFocus) { 
     this.hasFocus = hasFocus; 
    } 

    public Integer getIndex() { 
     return this.index; 
    } 

    public void setIndex(Integer index) { 
     this.index = index; 
    } 
} 

我试图用tableChange()侦听器,但它抛出计算器以前的错误显示我的表格

编辑 另外我忘了说:当我调试我的代码时,表格颜色很好。 并在除scrollPane视口外的任何位置重绘表格。

+0

解决。我只需要更新我的卷轴的视口 – Nataly

回答

1

只要修改一个适当的Swing组件,你需要告知组件重绘自己:

public void setOnCustomDrawCell(TNotifyEvent onCustomDrawCell) { 
    this.onCustomDrawCell = onCustomDrawCell; 
    repaint(); // added this 
} 
+0

不,它没有帮助,着色和以前一样工作。 – Nataly

+0

我给你了Swing组件如何工作的基本概念。所有滚动都会告诉表格重新绘制某些行。因此,在代码中的某处您必须更改表(或您的MyItem类)的数据,而不是重新绘制表。您发布的代码无效,因为我们不知道该代码的使用情况以及数据如何更改并添加到表格中。发布一个适当的[mcve],展示你正在尝试做什么的概念。 – camickr

+0

我尽可能提供了代码。 – Nataly