问题
I have no ideas what to do. I am creating an application. I need to work with the table, so I am using JTable. But I have a lot problems with it.
It seems to work, but when I try to delete column it this column disappears (only in GUI), but all the information still exists. Also columncount doesn't change.
I have searched and tried a lot of different code, but nothing changed.
public void addTblCol(JTable table,String name) {
DefaultTableModel model = (DefaultTableModel)table.getModel();
TableColumn col = new TableColumn(model.getColumnCount());
col.setHeaderValue(name);
table.addColumn(col);
model.addColumn(name);
this.realColCnt++;
};
public void delTblCol(JTable table,int index) {
DefaultTableModel model = (DefaultTableModel)table.getModel();
TableColumn col = table.getColumnModel().getColumn(index);
table.removeColumn(col);
table.revalidate();
this.realColCnt--;
};
回答1:
The DefaultTableModel
supports a setColumnCount()
method which effectively will allow you to remove columns from the end of the model only.
If you want to remove columns from the middle of the model, then you will need to:
- extend the
DefaultTableModel
and create your ownremoveColumn(int column)
method. - This method would need to loop through every row in the Vector and use the
Vector.remove(int)
method to remove the column for ever row. - Finally once this is done you would need to invoke the
fireTableStructureChanged()
method to tell the table that a column has been removed so the table can be repainted.
回答2:
Some general information related to your question.
The JTable API for public void removeColumn(TableColumn aColumn)
explicitly states:
Removes aColumn from this JTable's array of columns. Note: this method does not remove the column of data from the model; it just removes the TableColumn that was responsible for displaying it.
So the behavior you're experiencing is to be expected. If you're trying to remove data from the model, then you're going to have to change your TableModel's data and ColumnModel. Again for more specific help, you'll need to tell us more.
Consider creating a custom table model and giving it a removeColumn(...)
method that removes all the data from a single column, and then calls the appropriate fireXXX(...)
method.
Edit
You state in comment:
THx for answer , I am newbie . This prog is such for studying I have wasted two day for creating it.And now again have problems with it. What is the easiest way ?
That all depends on what you want to do. If you want to just change the display, then remove the column as you're doing and leave the data alone.
回答3:
Based on camickr's solution, I wrote this code to remove
a column from the JTable
.
public class CustomTableModel extends DefaultTableModel {
public void removeColumn(int column) {
// for each row, remove the column
Vector rows = dataVector;
for (Object row : rows) {
((Vector) row).remove(column);
}
// remove the header
columnIdentifiers.remove(column);
// notify
fireTableStructureChanged();
}
}
Note that it does not check if the column can or cannot be removed.
回答4:
jTable1.removeColumn(jTable1.getColumnModel().getColumn(0));
- replace jTable1 with your table name.
- change index number inside "getColumn(0)" according to your table.
来源:https://stackoverflow.com/questions/19758002/how-to-insert-delete-column-to-jtable-java