1

I am looking for methods to allow transposing my jTable on my UI. It could be by clicking a button or other action. My main question is how should I build the data model(abstractTableModel) for such purpose? And is there any ready-to-use method support such transposing in Java?

nachokk
  • 14,363
  • 4
  • 24
  • 53
AnneS
  • 1,493
  • 3
  • 14
  • 19
  • It depends. The responsibility of the table model it to model your data so it can be displayed. Once you want to start visualizing that data set, I would revert back to the original data - personally – MadProgrammer Jul 16 '13 at 03:56

1 Answers1

4

If you're talking about rows/column inversion, it is fairly simple in Swing:

  1. Create your normal table model class representing columns and rows.
  2. Create your inverted table model class which is a wrapper around your normal one. The only difference is that you revert calls for columns and rows. For example in getColumnCount method of your wrapper model you should call getRowCount of the internal one.
  3. Once you have your two models instantiated it is just a matter of replacing them in your JTable using setModel method.

Hope that helps..

Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60
  • Thanks so much! I am new to Java, so after I searched a little bit of wrapper, I am still not quite sure how it works. If I just create a corresponding TableModel, it would be the same? I am just curious whether creating a new tableModel is a neat way for invert tables or not. – AnneS Jul 17 '13 at 18:14
  • Yes. You have to create 2 table models. One for normal data view and the other one is for the inverted one. Since inversion does not change the data, all you have to do is just invert the method calls. For that reason your main model is passed into the inversion model. – Eugene Ryzhikov Jul 17 '13 at 18:32