I m使用一个配有电池使器的桌子,使我能够在每个具有价值的囚室里放置一个子子“x”,以便通过向纽伦施压来删除电池的价值。 编辑将行动听众指派到纽特,在纽特州受到压力时,应采取适当行动。
表格模型中每个栏目都包含一系列的数值。 我成功地设法从阵列清单中删除了正确价值,在变动之后,将 but子和桌旁的所有其他细胞重新抬头,但价值被删除的囚室保持不变。 如果重新启动申请或增加申请表的数值,则表格按预期复读,一切照旧。
那么,问题就好像牢房的开端人忘记了囚室的最初价值,因此,即使桌子重开,仍然继续将这一价值印制到囚室。 是否有任何人的想法是,如何适当更新表格,以便正确显示价值观?
表:
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();
}
}
预 收