尽管值已保存,但JComboBox TableCellRenderer似乎失去了它的值

时间:2016-12-14 21:11:33

标签: java swing jtable tablecellrenderer

为了得到这个结果:

enter image description here

我在下面创建了代码。但是一旦组合框关闭,每当我点击面板中的任何地方时,数字似乎都会回到0(虽然它在后面的代码中发生了变化,因为我看到使用public class MenuItem { public boolean getIsChecked() { return isChecked; } public void setIsChecked(boolean checked) { isChecked = checked; } public int getDesiredAmount() { return desiredAmount; } public void setDesiredAmount(int desiredAmount) { this.desiredAmount = desiredAmount; } public String getDescription() { return description; } public ItemType getItemType() { return itemType; } public BigDecimal getPrice() { return price; } private boolean isChecked; private int desiredAmount; private final String description; private final ItemType itemType; private final BigDecimal price; public MenuItem(String description, ItemType type, BigDecimal price) { this.description = description; itemType = type; this.price = price; } } )。

这是模型:

JPanel

这是我使用的public class MenuPanelTable extends JPanel { private static final Integer[] allowedAmounts = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; public MenuPanelTable(MenuItem[] items) { super(new BorderLayout()); setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); JTable table = new JTable(new TableModel(items)); table.setGridColor(Color.LIGHT_GRAY); table.setRowHeight(table.getRowHeight() + 5); TableColumn amountRow = table.getColumnModel().getColumn(1); DefaultCellEditor cellEditor = new DefaultCellEditor(new JComboBox<>(allowedAmounts)); cellEditor.setClickCountToStart(1); amountRow.setCellEditor(cellEditor); amountRow.setCellRenderer(new AmountsComboBoxRenderer(allowedAmounts)); add(new JScrollPane(table), BorderLayout.CENTER); } } class AmountsComboBoxRenderer implements TableCellRenderer { private final Integer[] allowedAmounts; public AmountsComboBoxRenderer(Integer[] allowedAmounts) { this.allowedAmounts = allowedAmounts; } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { out.println(String.format("(%d)->%s, [%s][%s]", row + 1, value.toString(), isSelected ? "S" : " ", hasFocus ? "F" : " ")); JComboBox<Integer> cb = new JComboBox<>(allowedAmounts); if (hasFocus) cb.setSelectedItem(value); return cb; } } class TableModel extends AbstractTableModel { private final MenuItem[] items; public TableModel(MenuItem[] items) { this.items = items; } @Override public int getRowCount() { return items.length; } @Override public int getColumnCount() { return 5; } @Override public Object getValueAt(int rowIndex, int columnIndex) { switch (columnIndex){ case 0 : return items[rowIndex].getIsChecked(); case 1 : return items[rowIndex].getDesiredAmount(); case 2 : return items[rowIndex].getDescription(); case 3 : return items[rowIndex].getItemType(); case 4 : return items[rowIndex].getPrice(); default: return null; } } @Override public void setValueAt(Object aValue, int rowIndex, int columnIndex) { switch (columnIndex){ case 0 : boolean v = (boolean) aValue; if (items[rowIndex].getIsChecked() == v) return; items[rowIndex].setIsChecked(v); fireTableCellUpdated(rowIndex, columnIndex); break; case 1 : int amount = (int) aValue; if (items[rowIndex].getDesiredAmount() == amount) return; items[rowIndex].setDesiredAmount(amount); fireTableCellUpdated(rowIndex, columnIndex); break; } } @Override public String getColumnName(int column) { switch (column) { case 0: return "?"; case 1: return "#"; case 2: return "Course"; case 3: return "Type"; case 4: return "Price"; default: return null; } } @Override public Class<?> getColumnClass(int columnIndex) { switch (columnIndex) { case 0: return Boolean.class; case 1: return Integer.class; case 2: return String.class; case 3: return String.class; case 4: return BigDecimal.class; default: return Object.class; } } @Override public boolean isCellEditable(int rowIndex, int columnIndex) { return columnIndex < 2; } }

{{1}}

如何让GUI保持在选定的值?

1 个答案:

答案 0 :(得分:2)

从渲染器类中删除@if ($errors->has('message')) <span class="help-block"> <strong>{{ $errors->first('message') }}</strong> </span> @endif 。毕竟,即使单元格没有焦点,您也希望显示该值。