Hi,
I'm working on a simple program to take orders for a company, and I'm using a JTable to display a list of products, very simple - only a few columns and all non-editable text at the moment.

I am adding items to a JTable during runtime and I've run into something weird that has been puzzling me for a good few hours.

Here is the (important part of) my Table Model:

public class OrderTableModel extends AbstractTableModel {

    // blah...

    public void addOrder(PipeOrder order){

    // add order to the array of order items

    fireTableDataChanged();
    orders[orderCount].getPipe().print();
    }

What happens though, is when the item is added, all the other items in the table are set to the same attributes as the one i've just added!

E.g.

Before: 
col 1   | col 2   | col 3   |
  A           A            A

Adding {B, B, B}...

After:
col 1   | col 2   | col 3   |
  B         B         B
  B         B         B

This is driving me up the wall, does anyone have any idea what might be wrong?
I've tested my getValueAt() method by setting the value returned to the row and column index (e.g. x,y), and I know the method is being called with the right coordiantes. Also, at the print method in the code above, the right data is output to the console, it just isn't being displayed properly in the table.

Any suggestions or tips would be really helpful!

Thanks :)

PS: first time poster, so Hi everyone!

Well, that was my first thought. I put in a few System.out.println()s though and as far as I can tell, getValueAt() works fine. In fact, everything works fine, the table is displaying the data that it is given to display.
When I call print on the object before it is added to the arraylist, it has the correct attributes - however, when I iterate through the arraylist AFTER it has been added and call print on each of them, the one most recently added overwrites all the others.
I took this to my tutor (this is in fact a second year software engineering degree assignment - lame) and he can't see what is wrong with it at all.
So, the problem appears to be in the arraylist somewhere. The object is added to the arraylist simply with the add() method - that is the only call that alters it in any of my code.

This is a really weird problem :-S Sorry if I've not been wonderfully clear on the details, tell me what code to post and it shall be posted,

Matt

This article has been dead for over six months. Start a new discussion instead.