我的代码仅适用于第1行。如果它不在JTable中,我想添加一行。 Class_assing_tb
是我的JTable。
当我点击"添加项目"我希望将我从JCombobox获得的值添加到JTable。按钮。它最多只能输入4个项目。我想这样做,如果我添加一个已经在JTable中的项目,我想给出一条消息" Denied"否则将该项添加到JTable。
int count = Class_assing_tb.getRowCount();
if (count == 0) {
addrow(); //this is to Command For Add new Row
} else if (count == 4) {
System.out.println("maximum Row Count");
// msg ****** Maximum Classes
} else {
int a = Class_assing_tb.getRowCount();
DefaultTableModel tm2 = (DefaultTableModel) Class_assing_tb.getModel();
loop:
for (int i = 0; i < a; i++) {
//System.out.println("Row Count is" + a);
// System.out.println("Sttate is"+i);
if (tm2.getValueAt(i, 0).equals(mng_stu_classatnd.getSelectedItem()) & tm2.getValueAt(i, 2).equals(mng_stu_batch.getSelectedItem()) & tm2.getValueAt(i, 3).equals(mng_stu_type.getSelectedItem())) {
System.out.println("Denied");
break loop;
} else {
addrow();//this is to Command For Add new Row
// continue loop;
}
}
}
答案 0 :(得分:1)
尝试这样的事情:
//全球宣言
private Vector<Vector<String>> data; //used for data from database
private Vector<String> header; //used to store data header
//仅在表单加载
上显示标题//create header for the table
header = new Vector<String>();
header.add("Column");
model=new DefaultTableModel(data,header);
table = new JTable(model);
//在actionPerformed()
中public void actionPerformed(ActionEvent ae){
if(ae.getSource()==add){
int count=table.getRowCount();
for(int i=1;i<=count;i++){
if(table.getValueAt(i,0).equals(jComboBox.getSelectedItem())){
JOptionPane.showMessageDialog((Component) null,"Duplicate...");
return;
}
}
if(count==4){
JOptionPane.showMessageDialog((Component) null,"Maximum Limit is crossed...");
}else{
Object d= jComboBox.getSelectedItem();
model.addRow(d);
}
}
}