Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Possible to replace TableModel in an existing JTable?

Is it possible to replace the entire TableModel in an existing JTable or do I have to recreate the JTable?

like image 604
opike Avatar asked May 30 '11 16:05

opike


People also ask

Can I add buttons in JTable?

We can add or insert a JButton to JTable cell by customizing the code either in DefaultTableModel or AbstractTableModel and we can also customize the code by implementing TableCellRenderer interface and need to override getTableCellRendererComponent() method.

How do I remove a selection from a JTable?

To completely remove the selection capability from JTable component we can change its focusable state to false by calling the setFocusable(boolean focusable) method.

How do you refresh a Java model?

getModel(); tableModel. setRowCount(0); and then restructure the table model again so it will refresh the jTable.


2 Answers

You can set a new model using the JTable.setModel() method

Check the Javadocs for details

like image 176
a_horse_with_no_name Avatar answered Oct 05 '22 19:10

a_horse_with_no_name


When you change the model you are stuck with old column titles. getColumnName() only gets called on initialization.

like image 37
Al morce Avatar answered Oct 05 '22 18:10

Al morce