0

I have a JTable called tableOne. I filled this table like this:

JTable tableOne = new JTable(contents, header);
//contents is a String[][] and header is a String[] defined above. 

I also have a list of CustomObject Objects. All these objects have a String[][] named customObjectContents assigned to it. These 2D arrays are filled with data.

I now want to use the ListSelectionListener to change the the displayed data in tableOne to the contents of the individual object's customObjectsContents.

How would you suggest I approach this. I cannot manipulate the variable tableOne in the Listener since it is not final. (maybe I just need to declare it final, but then I cannot manipulate it anymore which seams paradox) So I may need some other form of java technology I might not even heard about and I have no idea where to look. In the end I want to use the objects variables to display them into the JTable.

The solution was actually to create a new table model. Instead of filling the table in this way:

JTable tableOne = new JTable(contents, header);

I resorted to filling it in this way

DefaultTableModel model =  new DefaultTableModel();
JTable tableOne = new JTable(model);

Other than the table itself, the model chan be changed in the listener. For my purpose of setting a new String[][] as content for the table, I used the following code:

model.setDataVector(customObject.contentsArray, header);

contentsArray is a String[][] stored in the object. Header is a general String[].

I hope this helps anyone who reads this post.

GalliadII
  • 162
  • 1
  • 14
  • 3
    I can't speak for others, but your question is very unclear to me, probably because you're really not showing enough pertinent code and might wish to show more, a [mre] would be preferred, and perhaps offer more clarity to your explanation of the code and the problem. Also, your statement about not being able to manipulate tableOne within a listener doesn't make sense either. You can change the state of any object within a listener, and I don't understand the restriction that you mention. – Hovercraft Full Of Eels Oct 10 '21 at 22:35
  • You cannot re-assign a local variable from within an anonymous inner class, if that is what you mean, but you can certainly change the state of local variables, and in fact, this is done all the time. – Hovercraft Full Of Eels Oct 10 '21 at 22:36
  • how is the question unclear? I can barely pin point what I want to do even more? I want to change the contents of the JTable with a Listener. – GalliadII Oct 10 '21 at 22:56
  • Its unclear because it appears you are trying to use the same table for two purposes. I would suggest you create a second table with its own TableModel. Then you simply use the `setModel(...)` method of the second table to update its data in your listener. – camickr Oct 10 '21 at 23:08
  • well that DOES point me in the right direction. But yes I would use the same table like I would use the same textfield or Label to display different info. Would that be a problem? Should the table be part of the customObject class or not? – GalliadII Oct 10 '21 at 23:08
  • 1
    So if you update the same table then how do you restore the original data. Usually when using a ListSelectionListener to do processing you have a Summary/Details type of design. So the main table has a summary of the data, then when you select a row the details are displayed. If you repopulate the original table with new data, how do you get back to the old data? Yes it can be done. As I stated you just invoke the setModel(...) method any time you want to change the data. Its a matter of using a UI that users are familiar with. – camickr Oct 10 '21 at 23:11
  • I will look into the setModel() method. Thank you. I also do not get why you decided to comment instead of using the answer function. It solves the problem and everything. Also I thought the data would be restored if I reselect the prior list element. – GalliadII Oct 10 '21 at 23:13
  • Here's a simple [example](https://stackoverflow.com/a/8260663/230513) that selects the displayed model. – trashgod Oct 10 '21 at 23:48

0 Answers0