我该如何刷新表?

时间:2017-10-17 16:18:08

标签: java swing jtable

我在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()侦听器,但在显示我的表之前抛出stackOverflow错误

修改 我也忘了说:当我调试我的代码时,表格很好。 除了scrollPane视口之外,表格会重新绘制。

1 个答案:

答案 0 :(得分:1)

每当你更改一个正确的Swing组件时,你需要告诉组件重绘自己:

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