我有图片中的数据。
在jtable中插入数据时,我需要按CTnumber对它们进行分组,就像用红色数字所示。组号首先显示在jLblGroupNr中,然后添加到jtable中。之后,我需要汇总所有组数据,但这是第一步。
private void jBtnAddActionPerformed(java.awt.event.ActionEvent evt) {
groupNumber();
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.addRow(new Object[]{jLblGroupNr.getText(), jTbCTnumer.getText(), jTbPrice.getText(), jTbWeight.getText()});
}
private void groupNumber() {
int rowC = jTable1.getRowCount();
if (rowC == 0) {
//private int brIm = 1;
jLblGroupNr.setText("1");
} else {
for (int i = 0; i < rowC; i++) {
String CTnumber = (String) jTable1.getValueAt(i, 1);
String CTimputNumber = jTbCTnumer.getText();
if (CTimputNumber.contentEquals(CTnumber)) {
jLblGroupNr.setText((String) jTable1.getValueAt(i, 0));
} else {
grNr++;
jLblGroupNr.setText(Integer.toString(grNr));
}
break;
}
}
}
喜爱
这是第一步,要进行分组,在此之后,我需要创建具有已计算列总和的另一个表。
[![在此处输入图片描述] [1]] [1]
private final static int PRODUCT_COL = 1;
private final static int QUANTITY_COL = 2;
public static int BrojImenovanja = 0;
private void solution(){
String CTnr = jTbCTnumber.getText().trim();
int price = Integer.parseInt(jTbPrice.getText());
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
int rowCount = model.getRowCount();
int existingCtRowIndex = -1;
for (int i = 0; i < rowCount; i++) {
if (model.getValueAt(i, PRODUCT_COL) != null && model.getValueAt(i, PRODUCT_COL).equals(CTnr)) {
existingCtRowIndex = i;
break;
}
}
if (existingCtRowIndex > -1) {
int brImen = Integer.parseInt(model.getValueAt(existingCtRowIndex, 0).toString());
jLblGroupNr.setText(Integer.toString(brImen));
model.addRow(new Object[]{brImen, CTnr, price, jTbWeight.getText()});
} else {
BrojImenovanja++;
jLblGroupNr.setText(Integer.toString(BrojImenovanja));
model.addRow(new Object[]{BrojImenovanja, CTnr, price, jTbWeight.getText()});
}
}
我希望这会对某人有所帮助。 问候