gpt4 book ai didi

java - 在单元格编辑器中调用操作后单元格不会更新

转载 作者:行者123 更新时间:2023-12-03 18:18:38 26 4
gpt4 key购买 nike

我正在使用一个带有单元格渲染器的表格,它允许我在每个单元格中放置一个按钮“x”,其中包含一个值,这样我就可以通过按下按钮来删除单元格的值。单元格编辑器将 Action 监听器分配给按钮,并在按下按钮时调用适当的 Action 。

作为 DefaultTableModel 的表模型中的每一列都由值的数组列表填充。我成功地通过按下按钮从数组列表中删除了正确的值,并且表中的每个其他单元格在更改后刷新,但值被删除的单元格保持不变。如果我重新启动应用程序或向数组列表添加一个值,表格会按预期刷新,一切看起来都应该如此。

Mo 问题是,单元格渲染器似乎不会忘记单元格的初始值,因此即使重新绘制表格,它仍会继续将此值打印到单元格中。有谁知道我如何正确刷新表格以便正确显示值?

表类:

public class Table extends JTable {

public static DefaultTableModel model = new DefaultTableModel();
private Days d;
private JFrame frame;
private AddCellRenderer renderer;
private AddCellEditor editor;

public Table(Days d, JFrame frame) {
// Assign the table model to the table
super(model);
this.d = d;
this.frame = frame;

// Define dimensions of table
model.setRowCount(11);
model.setColumnCount(5);

// Block resizing and reordering of headers
this.setColumnSelectionAllowed(false);
this.setRowSelectionAllowed(false);
this.setCellSelectionEnabled(true);
this.setTableHeader(null);

// Define first row height
this.setRowHeight(38);
// Define all other row's heights
this.setRowHeight(0, 45);

this.d.setTable(this);

for (int i = 0; i < 5; i++) {
editor = new AddCellEditor(new JCheckBox(), d.getVisibleDays().get(i), this);
renderer = new AddCellRenderer(d.getVisibleDays().get(i), editor);
this.getColumnModel().getColumn(i).setCellRenderer(renderer);
this.getColumnModel().getColumn(i).setCellEditor(editor);
}

this.refresh();
}

public void refresh() {

// Empty table
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 11; j++) {
this.getModel().setValueAt(null, j, i);
}
}

// Populate table with entries for each day
for (int i = 0; i < 5; i++) {

Iterator<String> iterator = d.getVisibleDays().get(i).getEntryListIterator();

int j = 0;

while(iterator.hasNext()){
String s = iterator.next();
this.getModel().setValueAt(s, (j+1), i);
j++;
}

this.getModel().setValueAt(d.getVisibleDays().get(i).getDayName(), 0, i);
}
}

public void modelClearValueAt(int row, int column) {
this.getModel().setValueAt(null, row, column);
}
}

单元格编辑器类:

public class AddCellEditor extends DefaultCellEditor {

private JPanel headerPanel;
private JPanel entryPanel;
private JLabel dayName;
private JLabel entryValue;
protected JButton addButton;
private JButton removeButton;
private String label;
private int row;
private int column;
private Day day;
private String date;
private Table table;
private AddCellRenderer renderer;

public AddCellEditor(JCheckBox checkBox, Day day, Table table) {
super(checkBox);
this.day = day;
this.table = table;
date = day.getDayDate();
headerPanel = new JPanel();
headerPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
entryPanel = new JPanel();
entryPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
dayName = new JLabel();
entryValue = new JLabel();
addButton = new JButton();
removeButton = new JButton();
headerPanel.add(dayName);
headerPanel.add(addButton);
entryPanel.add(entryValue);
entryPanel.add(removeButton);
addButton.setOpaque(true);
addButton.setPreferredSize(new Dimension(16, 16));
removeButton.setOpaque(true);
removeButton.setPreferredSize(new Dimension(16, 16));
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Add pressed");
addItem();
fireEditingStopped();
}
});
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Remove pressed");
removeItem();
getTable().refresh();
fireEditingStopped();
}
});

}

public void setRenderer(AddCellRenderer renderer) {
this.renderer = renderer;
}

public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {

this.row = row;
this.column = column;

if (row == 0) {
return this.headerPanel;
}
if (row != 0 && value != null) {
return this.entryPanel;
}
return null;
}

public void addItem() {
String input = JOptionPane.showInputDialog("Add entry for " + label + ":");

if ((input != null) && (input.length() > 0)) {
System.out.println("Added: " + input + "Item");
day.addEntry(input, column);
}
}

public void removeItem() {
table.modelClearValueAt(row, column);
day.removeEntry(row-1);
System.out.println("Item removed");
}

public Table getTable() {
return this.table;
}

public boolean stopCellEditing() {
return super.stopCellEditing();
}

protected void fireEditingStopped() {
super.fireEditingStopped();
}
}

提前致谢

最佳答案

如果您使用的是 DefaultTableModel,则必须删除模型本身持有的值,而不是用于构建模型的 ArrayList 中的值。您可以通过调用 DefaultTableModel 对象的 setValueAt(...) 方法来完成此操作。

您可能希望发布 SSCCE以获得更具体的帮助。

关于java - 在单元格编辑器中调用操作后单元格不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8862949/

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