Question

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.

To be more specific I have a List of objects called "Artikl". Artikl has 4 values that I want to show in the table, String naziv, String drzava, Int kolicina and Float cijena.

Lets say the values of those variables are naziv="x", drzava="y", kolicina=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.

The problem arises when I use artiklModel.fireDataChanged(); The values shown in the table are naziv="x", drzava="y", kolicina=100, cijena=100.

I've been debugging for 2 days and cannot find the cause of this. 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:

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:

        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.

Was it helpful?

Solution

In your switch statement:

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

There is a break; missing, so for case COLUMN_KOLICINA the line value = artikl.getCijena()gets executed as well.

case COLUMN_KOLICINA:
    value = artikl.getKolicina();
    break; // THIS LINE MISSING
case COLUMN_CIJENA:
    value = artikl.getCijena();
    break;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top