我正在研究我的CapStone项目本学期毕业,并且遇到了我正在研究的程序的一些问题。该代码适用于Serendipity书商计划。我将为一个虚构的书店设计一个销售点系统。
目前,当用户编辑给定行的“Qty”单元格时,我很难尝试更新“总计”列(第5列)。 “数量”字段还需要验证其单元格中的值是否大于库存数量(第1列)。
我还阅读了Java Tutorials中的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;
}
}
由于这是家庭作业,我不希望被交给我的代码的工作版本,错误得到解决。我想要一些关于我做错了什么的指导,以便让编辑器监听器实际被调用。
如果我能提供任何其他信息可以帮助我找到解决方案,请告诉我。
感谢您花时间看看这个!
答案 0 :(得分:5)
您不应该自己向单元格编辑器添加显式键侦听器并处理这些事件。
默认情况下,基于列类型(如果模型将单元格返回为可编辑),jtable将自动选择适当的编辑器。如果您需要自己的自定义编辑器(用于着色等),那么您还需要将其实现为单元格编辑器,然后将其设置为列模型。
每当修改了值(可能在焦点丢失时触发)时,在jtable中安装了编辑器,jtable将自动调用模型的setValueAt()。更新另一个依赖列的所有逻辑都应属于此处。
答案 1 :(得分:5)