Reputation: 1
I created a table based on JXTable and AbstractTableModel using Java Swing. Table and model data are provided by CrmListeTableModel class. However, currentKartListeTable and currentKartListeTableModel data row counts (getRowCount()) are not the same. This incompatibility causes inconsistencies in data update operations.
public class CrmListeTableModel extends AbstractTableModel {
public ArrayList<Cari> veri = new ArrayList<Cari>();
public int getRowCount() {
return veri.size();
}
// Other necessary methods
}
public class CariKartListeTable extends JXTable {
private CrmListeTableModel cariKartListeTableModel;
public CariKartListeTable(CrmListeTableModel model, BaseFrame frame) {
super(model);
this.cariKartListeTableModel = model;
}
public void cariKartListeTable_mouseClicked(MouseEvent e) {
int selectedRow = convertRowIndexToModel(getSelectedRow());
if (e.getClickCount() == 2) {
Cari satir = (Cari) cariKartListeTableModel.veri.get(selectedRow);
System.out.println(satir.getCariAciklama());
}
}
}
public void tabloAyarla() throws ClassNotFoundException, SQLException,
InstantiationException, IllegalAccessException {
tabloSatirlar = TableKolonDatabaseIslem.listeGetir(
TabloKodlari.cariListeTabloKod, kullaniciBilgi.getKod());
cariKartListeTableModel = new CrmListeTableModel();
cariKartListeTable = new CariKartListeTable(cariKartListeTableModel,this);
cariKartListeTableModel.tabloSatirlar = tabloSatirlar;
//cariKartListeTable.tabloSatirlar = tabloSatirlar;
cariKartListeTable.addMouseListener(new CariKartListeTableMouseListener());
cariKartListeTable.setRowHeight(25);
// TableFilterHeader ekleyin ve filtreleme modunu etkinleştirin
TableFilterHeader filterHeader = new TableFilterHeader(cariKartListeTable, AutoChoices.ENABLED);
filterHeader.setTable(cariKartListeTable);
siparisFisListeSP = new JScrollPane(cariKartListeTable);
for (int i = 0; i < tabloSatirlar.size(); i++) {
TableKolon tabloSatir = (TableKolon) tabloSatirlar.get(i);
System.out.println(" tablo size" + tabloSatirlar.size());
TableColumn col = cariKartListeTable.getColumnModel().getColumn(i);
col.setPreferredWidth(tabloSatir.getGenislik());
}
cariKartListeTableModel.fireTableStructureChanged();
}
public void cariKartListeTable_mouseClicked(MouseEvent e) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
System.out.println(cariKartListeTable.getModel().getRowCount());//1
System.out.println(cariKartListeTableModel.getRowCount());//983
int selectedRow = cariKartListeTable.convertRowIndexToModel(cariKartListeTable.getSelectedRow());
if (e.getClickCount() == 2) {
Cari satir = (Cari) cariKartListeTableModel.veri.get(selectedRow);
System.out.println(satir.getCariAciklama());
if (ap.durumKontrol("orderFrame") == true) {
CariKartFrame cariKartFrame = (CariKartFrame) ap.pencereler
.get("orderTeklifFrame");
cariKartFrame.ekranGetir(satir.getId());
cariKartFrame.toFront();
} else {
CariKartFrame cariKartFrame = new CariKartFrame(ap,
"orderFrame", bolum);
ap.dp.add(cariKartFrame);
ap.durumCubukButonEkle("orderFrame", cariKartFrame, "Order");
cariKartFrame.ekranGetir(satir.getId());
cariKartFrame.setVisible(true);
}
}
}
Expected Behavior and Actual Result: The expected behavior is that the number of data in the table and model are the same and the data accessed through the model matches the selected rows in the table. However, the values of cariKartListeTable.getModel().getRowCount() and cariKartListeTableModel.getRowCount() are different.
Additional Information:
I am using Java 7 and Swing. I encounter this problem when I sort or filter the table. I Want to Ask: How can I solve the data incompatibility between the table and the model? What should I do to ensure data synchronization in such a case?
Upvotes: 0
Views: 20