java - 我应该如何刷新表格?

标签 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;
    }
}

使用我的 table

 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视口(viewport)之外的任何地方重新绘制表格。

最佳答案

每当您更改 Swing 组件的属性时,您都需要告诉组件重新绘制自身:

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

关于java - 我应该如何刷新表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46794747/

相关文章:

java - Docker CMD + ENTRYPOINT 与命令行行为不匹配

java - 尝试在 Java 中运行 PostFixCalculator 时出现 NullPointerException

java - 我们如何读取控制台中的上一行?

java - 当 JVM 在运行时内存不足时会发生什么?

javascript - Swing html中的Java回调

java - 严重 : null java. sql.SQLException

java - 如何从jTable中删除数据库中的数据?

java - 在java中使用MaskFormatter将值设置为Jformattedtextfield

java - 调整窗口大小时绘制的图形闪烁然后消失

Java JTable 通过始终对固定列进行排序并允许用户对任何辅助列进行排序来将行卡住到顶部