我正在开展我的 CapStone 项目,以便本学期毕业,并且在我正在从事的项目中遇到了一些问题。该代码适用于 Serendipity Booksellers Program。我要为一家虚构的书店设计一个销售点系统。
目前,当用户编辑给定行的“数量”单元格时,我试图让“总计”列(第 5 列)更新。“数量”字段还需要验证其单元格中的值不大于库存数量(第 1 列)。
我还阅读了 Java 教程中的How to Use Tables,昨晚在 Google 上搜索了几个小时,试图找到一些关于如何让 CellEditorListener 工作的有用代码。
我之前也尝试过让 CashierPanel 类实现 CellEditorListener,并通过以下方式注册它:
CellEditorListener listener = null;
table.getDefaultEditor(String.class).addCellEditorListener(listener);
这种方法也不起作用。
这是我现在拥有的代码(217 行):
/* Imported Dependencies */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import net.miginfocom.swing.MigLayout;
public class CashierPanel extends JFrame {
/**
* Serial Version UID
*/
private static final long serialVersionUID = 6922826100788686147L;
/* Private Fields */
private JPanel searchPanel;
private JLabel isbn_lbl;
private JLabel title_lbl;
private JTextField isbn_tf;
private JTextField title_tf;
private JTextField currentCell;
private BookCollection myCollection;
private CashierTableModel tableData;
private CellEditorListener listener;
/**
* Constructor
*/
public CashierPanel() {
// Set window properties
setTitle("Testing this table");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(585, 300);
setLayout(new BorderLayout());
tableData = new CashierTableModel();
final JTable table = new JTable(tableData);
table.setRowHeight(25);
table.getTableHeader().setReorderingAllowed(false);
table.setCellSelectionEnabled(true);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
add(new JScrollPane(table), BorderLayout.CENTER);
// Define a cell renderer to render text to center of cell
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
// Set column properties
int[] sizes = new int[] { 50, 50, 87, 275, 50, 50 };
String[] columnNames = new String[] { "Qty", "Stock", "ISBN", "Title",
"Price", "Total" };
TableColumn[] column = new TableColumn[columnNames.length];
for (byte i = 0; i < columnNames.length; i++) {
column[i] = table.getColumnModel().getColumn(i);
column[i].setMinWidth(sizes[i]);
column[i].setHeaderValue(columnNames[i]);
if (i != 3) {
column[i].setMaxWidth(sizes[i]);
column[i].setCellRenderer(centerRenderer);
}
}
/* Make the quantity field only display digits */
currentCell = new JTextField();
column[0].setCellEditor(new DefaultCellEditor(currentCell));
// Add a key listener to the current cell
currentCell.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
// if the key typed is not a digit or backspace
if (!Character.isDigit(e.getKeyChar())
&& e.getKeyChar() != KeyEvent.VK_BACK_SPACE) {
currentCell.setEditable(false);
currentCell.setBackground(Color.WHITE);
} else {
currentCell.setEditable(true);
}
}
});
/* Search panel */
searchPanel = new JPanel();
myCollection = new BookCollection();
isbn_lbl = new JLabel("Search by ISBN:");
isbn_tf = new JTextField();
isbn_tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
ArrayList<BookData> data = myCollection.select(isbn_tf.getText(), BookCollection.FIELD_ISBN);
tableData.addNewRow();
table.setValueAt((new BigDecimal("1").toString()), tableData.getRowCount() - 1, 0);
table.setValueAt(Integer.toString(data.get(0).getQtyOnHand()), tableData.getRowCount() - 1, 1);
table.setValueAt(data.get(0).getIsbn(),tableData.getRowCount() - 1, 2);
table.setValueAt(data.get(0).getTitle(),tableData.getRowCount() - 1, 3);
table.setValueAt(data.get(0).getRetail().setScale(2, RoundingMode.HALF_UP).toString(), tableData.getRowCount() - 1, 4);
table.setValueAt(table.getValueAt(tableData.getRowCount() - 1, 4), tableData.getRowCount() - 1, 5);
isbn_tf.setText("");
}
});
title_lbl = new JLabel("Search by Title:");
title_tf = new JTextField();
title_tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
ArrayList<BookData> data = myCollection.select(title_tf.getText(), BookCollection.FIELD_TITLE);
tableData.addNewRow();
table.setValueAt((new BigDecimal("1").toString()), tableData.getRowCount(), 0);
table.setValueAt(Integer.toString(data.get(0).getQtyOnHand()), tableData.getRowCount() - 1, 1);
table.setValueAt(data.get(0).getIsbn(), tableData.getRowCount(), 2);
table.setValueAt(data.get(0).getTitle(), tableData.getRowCount(), 3);
table.setValueAt(data.get(0).getRetail().setScale(2, RoundingMode.HALF_UP).toString(), tableData.getRowCount(), 4);
table.setValueAt(data.get(0).getRetail().setScale(2, RoundingMode.HALF_UP).toString(), tableData.getRowCount(), 5);
title_tf.setText("");
}
});
searchPanel.setLayout(new MigLayout("", "[78px][202px,grow]", "[24px][29px]"));
searchPanel.add(isbn_lbl, "cell 0 0, alignx left, aligny center");
searchPanel.add(isbn_tf, "cell 1 0,growx, aligny center");
searchPanel.add(title_lbl, "cell 0 1, alignx left, aligny center");
searchPanel.add(title_tf, "cell 1 1, growx, aligny center");
add(searchPanel, BorderLayout.SOUTH);
};
public static void main(String[] args) {
CashierPanel myCashierPanel = new CashierPanel();
myCashierPanel.setVisible(true);
}
}
class CashierTableModel extends AbstractTableModel implements CellEditorListener{
/**
*
*/
private static final long serialVersionUID = -1500952132717353189L;
private ArrayList<Object[]> data = new ArrayList<Object[]>();
public void addNewRow() {
data.add(new String[] { null, null, null, null, null, null });
fireTableDataChanged();
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return 6;
}
public Object getValueAt(int row, int col) {
return data.get(row)[col];
}
public void setValueAt(Object value, int row, int col) {
data.get(row)[col] = value;
fireTableCellUpdated(row, col);
}
public boolean isCellEditable(int row, int col) {
if (col > 0) {
return false;
} else {
return true;
}
}
public void editingCanceled(ChangeEvent e) {
for (int i = 0; i <= getRowCount() - 1; i++) {
BigDecimal[] data = new BigDecimal[3];
data[0] = data[0].add((BigDecimal) getValueAt(i, 1));
data[1] = data[1].add((BigDecimal) getValueAt(i, 4));
data[2] = data[2].add(calculateTotal(data[0], data[1]));
setValueAt(data[2], i, 5);
}
}
public void editingStopped(ChangeEvent e) {
for (int i = 0; i <= getRowCount() - 1; i++) {
BigDecimal[] data = new BigDecimal[3];
data[0] = data[0].add((BigDecimal) getValueAt(i, 1));
data[1] = data[1].add((BigDecimal) getValueAt(i, 4));
data[2] = data[2].add(calculateTotal(data[0], data[1]));
setValueAt(data[2], i, 5);
}
}
public BigDecimal calculateTotal(BigDecimal qty, BigDecimal retail) {
BigDecimal total = new BigDecimal("0.00");
total = total.add(qty.multiply(retail));
return total;
}
}
由于这是家庭作业,我不想收到我的代码的工作版本,并解决了错误。我只是想要一些关于我做错了什么来让编辑器监听器被实际调用的指导。
如果我可以提供任何其他信息来帮助指导我找到解决方案,请告诉我。
感谢您花时间看这个!