e921. inserting a column in a jtable component.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 29 行

TXT
29
字号
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 + =
减小字号Ctrl + -
显示快捷键?