I am making a program in which I need to use many JTables. So I need to clone a JTable many times. By cloning I mean a copy with same model, same columnModel, same font size, same color etc.
I have found how to clone a jtable.
But the problem is only the text of the both is same. Not the font or colors.
I also tried this:-
JTable t1 = new JTable(tm.getTable().getModel(), tm.getTable().getColumnModel());
But this is also not working.
I also tried:-
JTable ti = tm;
But this will work. But the problem is changes made to tm
will also happen with ti
.
Is there any way to clone a jtable with all its properties?
CodePudding user response:
Do this:
JTable t1 = new JTable(tm.getTable().getModel(), tm.getTable().getColumnModel());
And then, take every relevant attribute that the first table has, and assign it to your second table.
For example:
t1.setFont(tm.getFont());
// And so on
OR
You can always deep clone the object through serializing. Check this article for more. It may be an overkill, but that is for you to decide.