e912. copying a row or column in a jtable component.txt

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

TXT
42
字号
This example demonstrates how to copy a column or row of data in a DefaultTableModel. 
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    
    // Create some data
    
    // Get all the table data
    Vector data = model.getDataVector();
    
    // Copy the second row
    Vector row = (Vector)data.elementAt(1);
    row = (Vector)row.clone();
    
    // Overwrite the first row with the copy
    Vector firstRow = (Vector)data.elementAt(0);
    for (int i=0; i<row.size(); i++) {
        firstRow.set(i, row.get(i));
    }
    
    // Append the copy to the end of the table.
    // Note that a copy is NOT made of 'row'.
    model.addRow(row);
    
    
    // Copy the first column
    int mColIndex = 0;
    java.util.List colData = new ArrayList(table.getRowCount());
    for (int i=0; i<table.getRowCount(); i++) {
        row = (Vector)data.elementAt(i);
        colData.add(row.get(mColIndex));
    }
    
    // Copy it to the second column
    for (int i=0; i<colData.size(); i++) {
        row = (Vector)data.elementAt(i);
        row.set(1, colData.get(i));
    }
    
    // Append a new column with copied data
    model.addColumn("Col3", colData.toArray());

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?