gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:07:25 25 4
gpt4 key购买 nike

我在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com