简体   繁体   English

JTable fireDataChanged重复的单元格值

[英]JTable fireDataChanged duplicating cell values

I tried searching for the answer but I never found anyone having this problem. 我试着寻找答案,但我从来没有发现任何人有这个问题。

Basically what's happening is that the JTable that's supposed to show my objects is duplicating the value of last cell value into the cell before that. 基本上发生的事情是,应该显示我的对象的JTable在此之前将最后一个单元格值的值复制到单元格中。

To be more specific I have a List of objects called "Artikl". 更具体地说,我有一个名为“Artikl”的对象列表。 Artikl has 4 values that I want to show in the table, String naziv, String drzava, Int kolicina and Float cijena. Artikl有4个值,我想在表格中显示,String naziv,String drzava,Int kolicina和Float cijena。

Lets say the values of those variables are naziv="x" , drzava="y" , kolicina=2 , cijena=100 让我们说这些变量的值是naziv="x"drzava="y"kolicina=2kolicina=2 cijena=100

When adding the values to the object Artikl, and then adding the object to the List, everything works fine, the values are as they should be. 将值添加到对象Artikl,然后将对象添加到List时,一切正常,值应该是正确的。

The problem arises when I use artiklModel.fireDataChanged(); 当我使用artiklModel.fireDataChanged();时出现问题artiklModel.fireDataChanged(); The values shown in the table are naziv="x" , drzava="y" , kolicina=100 , cijena=100 . 表中显示的值是naziv="x"drzava="y"kolicina=100kolicina=100 cijena=100

I've been debugging for 2 days and cannot find the cause of this. 我已经调试了2天,但找不到原因。 I'll attach the necessary code below (I cannot add much more since it's a college project). 我将在下面附上必要的代码(由于这是一个大学项目,我不能添加更多)。

public class Artikl {

private int rbr;
private float cijena;
private String naziv;
private String drzava;
int kolicina;

public int getRbr(){
    return rbr;
}

public void setRbr(int rbr){
    this.rbr = rbr;
}

public int getKolicina(){
    return kolicina;
}

public void setKolicina(int kolicina){
    this.kolicina = kolicina;
}

public float getCijena(){
    return cijena;
}

public void setCijena(float cijena){
    this.cijena = cijena;
}

public String getNaziv(){
    return naziv;
}

public void setNaziv(String naziv){
    this.naziv = naziv;
}

public String getDrzava(){
    return drzava;
}

public void setDrzava(String drzava){
    this.drzava = drzava;
}
}

This is my TableModel: 这是我的TableModel:

public class ArtiklModel extends AbstractTableModel{
private static final int COLUMN_NAZIV         = 0;
private static final int COLUMN_DRZAVA        = 1;
private static final int COLUMN_KOLICINA      = 2;
private static final int COLUMN_CIJENA        = 3;

private List<Artikl> listArtikl;
private String[] columnNames;

public ArtiklModel() {
    // initializes contact list
    this.listArtikl = new ArrayList<Artikl>();
    // define column names
    columnNames = new String[] {"Naziv",
    "Država","Količina", "Cijena"};
}

@Override
public int getColumnCount() {
    return columnNames.length;
}

public String getColumnName(int column) {
       return columnNames[column];
}   

@Override
public int getRowCount() {
    return listArtikl.size();
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Object value = null;
    Artikl artikl = listArtikl.get(rowIndex);

    switch (columnIndex) {
    case COLUMN_NAZIV:
        value = artikl.getNaziv();
        break;
    case COLUMN_DRZAVA:
        value = artikl.getPorijeklo();
        break;
    case COLUMN_KOLICINA:
        value = artikl.getKolicina();
    case COLUMN_CIJENA:
        value = artikl.getCijena();
        break;
    }

return value;
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
    return false;
}   

public void addArtikl(Artikl novi) {
    this.listArtikl.add(novi);
}

public void removeArtikl(int rowIndex) {
    this.listArtikl.remove(rowIndex);
}

public List<Artikl> getArtikli(){
    return listArtikl;
}
}

This is part of the Main Form where I create the table and put it into my form: 这是主表单的一部分,我在其中创建表并将其放入我的表单中:

        final JTable tablicaArtikla = new JTable();
    final ArtiklModel model1 = new ArtiklModel();
    tablicaArtikla.setModel(model1);

    final JScrollPane pane = new JScrollPane(tablicaArtikla);

    GridBagConstraints gbc_table_1 = new GridBagConstraints();
    gbc_table_1.gridheight = 8;
    gbc_table_1.insets = new Insets(0, 0, 5, 0);
    gbc_table_1.fill = GridBagConstraints.BOTH;
    gbc_table_1.gridx = 5;
    gbc_table_1.gridy = 1;

    StavkeRacunaTab.add(pane, gbc_table_1);

The form has TextBoxes and Comboboxes for adding values, and when I click on a button, those values should be added to the List and then shown in the table: 表单有TextBoxes和Comboboxes用于添加值,当我单击一个按钮时,这些值应该添加到List中,然后显示在表中:

        btnDodajArtikl.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent arg0) {

                inHelper.DodajUTablicu(txtKolicina.getText(), txtNaziv.getText(), txtPojedinacnaCijena.getText(), cboDrzava.getSelectedItem().toString(), model1);
                txtKolicina.setText("");
                txtNaziv.setText("");
                txtPojedinacnaCijena.setText("");

                model1.fireDataChanged();              
            }


        }
    });

This next part is in another class, I use it to update my list and show the result in the table: 下一部分是在另一个类中,我用它来更新我的列表并在表中显示结果:

    public void DodajUTablicu(String kolicina, String naziv, String cijena, String drzava, ArtiklModel artiklModel){

    Artikl artikl = new Artikl();
    artikl.setKolicina(Integer.parseInt(kolicina));
    artikl.setCijena(Float.parseFloat(cijena));
    artikl.setDrzava(drzava);
    artikl.setRbr(rbr);
    artikl.setNaziv(naziv);

    artiklModel.addArtikl(artikl);

    rbr++;
}

Does anyone have any idea? 有人有什么主意吗? I'm slowly going crazy. 我慢慢发疯了。

In your switch statement: 在你的switch语句中:

case COLUMN_KOLICINA:
    value = artikl.getKolicina();
case COLUMN_CIJENA:
    value = artikl.getCijena();
    break;

There is a break; break; missing, so for case COLUMN_KOLICINA the line value = artikl.getCijena() gets executed as well. 缺少,所以对于case COLUMN_KOLICINA ,行value = artikl.getCijena()被执行。

case COLUMN_KOLICINA:
    value = artikl.getKolicina();
    break; // THIS LINE MISSING
case COLUMN_CIJENA:
    value = artikl.getCijena();
    break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM