如何删除所有列?感谢。
答案 0 :(得分:8)
如果您使用的是DefaultTableModel,请将column count设置为零。
答案 1 :(得分:1)
更快的方式::动态删除JTable上的所有列
//JTable variable name is jTable1_info....
model_info=(DefaultTableModel)jTable1_info.getModel();
//Clear all the column name on jtable jpanel-4
model_info.setColumnCount(0);
此示例之前的代码
//import the DefaultTableModel
import javax.swing.table.DefaultTableModel;
public final class homepage extends javax.swing.JFrame implements ActionListener
{
DefaultTableModel model_info;
public homepage() throws InterruptedException
{
initComponents();
create_column();//call method create jtable column names
}
}
private void create_column()
{
model_info=(DefaultTableModel)jTable1_info.getModel();
model_info.addColumn("Column_Name");
model_info.addColumn("Column_type");
model_info.addColumn("Column_size");
}
答案 2 :(得分:0)
TableColumnModel cm = jTableThatUCreated.getColumnModel();
while (cm.getColumnCount()!=0) {
TableColumn column = cm.getColumn(0);
cm.removeColumn(column);
}
Java 7不会将columnColumnNumber(0)设置为先前的答案。