0

I have a problem in the display of two different JTables which are created by the same AbstractTableModel. I don't really think that is important to show the code of the AbstractTableModel, but if I am asked for I may present it as well.

I just call two times the same class that extends this AbstractTableModel for two arraylists that I am using to create the tables.

final SwitchTableModel model = new SwitchTableModel(user_decide);
final SwitchTableModel model1 = new SwitchTableModel(duplicates);
JTable table = new JTable(model);
JTable table1 = new JTable(model1);

JFrame frame = new JFrame ("Results");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel grid = new JPanel();
grid.add(toolbar);
grid.add(toolbar1);
grid.add(table);
grid.add(table1);
frame.add(grid);
frame.pack();
frame.setVisible(true);

I also create the toolbars which are the same, I also think that this is irrelevant, that is why I don't post the code - I would do it if you think it is needed.

The problem is that in the end I see the same JTable two times, so I suppose it has something to do with the way that I call the class.

Dimitra Micha
  • 2,089
  • 8
  • 27
  • 31
  • The problem comes from either `SwitchTableModel` either from `user_decide`/`duplicates`. so yes, I would suggest to add the codes that is related to those. – Guillaume Polet Feb 20 '13 at 14:27
  • but the thing is that if I change the order of the first two lines, then it shows 2 times only the last. – Dimitra Micha Feb 20 '13 at 14:28
  • 1
    Post the code of your `SwitchTableModel`. I would suspect an inappropriate `static` keyword somewhere, but really hard to guess without the code. – Guillaume Polet Feb 20 '13 at 14:30
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 20 '13 at 14:35
  • @Guillaume Polet Before I post a part of the code, which is quite big, I would like to say that I use a `public static int [][] data;` outside of the class `SwitchTableModel` and I alter it inside the class. This is where I store values of the JTable. Do you think that this is playing some role? – Dimitra Micha Feb 20 '13 at 14:40
  • Thank you:) This was the problem. – Dimitra Micha Feb 20 '13 at 14:54

3 Answers3

2

The problem comes from an inappropriate static keyword.:

public static int [][] data;

static means that the value of that variable will be the same for all your instances. Instead, put your data inside your SwitchTableModel and don't make it static. This will solve your issues immediately.

Something like:

public class SwitchTableModel extends AbstractTableModel {
    private int[][] data;
    //... the rest of your current code.
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
0

I think both the value passed to the SwitchTableModel user_decide and duplicates are having same values. Otherwise there is no issue in the above code.

-1

Try adding two seperate scroll panes to your panel, then add the JTables to the scroll panes. Other then that, your not clear on what exactly your display problem is, wheather the JTables are not showing up, or if the data is not different... ect