📄 e921. inserting a column in a jtable component.txt
字号:
There is no insertColumn method like DefaultTableModel.insertRow() for inserting rows. In order to insert a column at a particular position, you must append the column using DefaultTable.addColumn() and then move the new column into the desired position. See e920 Appending a Column to a JTable Component and e923 Moving a Column in a JTable Component for more information.
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
// Disable autoCreateColumnsFromModel
table.setAutoCreateColumnsFromModel(false);
// Add data
// Insert a new column at position 2 (becomes 3rd column)
insertColumn(table, "New Column", null, 2);
// Insert a new column at the end
insertColumn(table, "New Column", null, table.getColumnCount());
// Creates a new column at position vColIndex
public void insertColumn(JTable table, Object headerLabel,
Object[] values, int vColIndex) {
// This method is defined in
// e920 Appending a Column to a JTable Component
betterAddColumn(table, headerLabel, values);
table.moveColumn(table.getColumnCount()-1, vColIndex);
}
Related Examples
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -