如何使用JTable

来源:互联网 发布:最流行的网络用语英文 编辑:程序博客网 时间:2024/04/29 14:00

本文翻译自Java年鉴1.4版

创建表格组件(Creating a JTable Component)
The following creates a table that uses an efficient underlying storage implementation. Although cell values can be changed, rows and columns cannot be added or deleted.
下列代码使用了一种有效的方式创建表格。尽管单元格的值能变化,行和列不能被添加或删除。
    // Create with initial data
    Object[][] cellData = {
        {"row1-col1", "row1-col2"},
        {"row2-col1", "row2-col2"}};
    String[] columnNames = {"col1", "col2"};
   
    JTable table = new JTable(cellData, columnNames);

The following creates tables that allow rows and columns to be added or deleted:
下列代码创建的表格允许增删行或列。
    // Create a table with empty cells
    int rows = 10;
    int cols = 5;
    table = new JTable(rows, cols);
   
    // Create a table with initial data
    Vector rowData = new Vector();
    for (int i=0; i<cellData.length; i++) {
        Vector colData = new Vector(Arrays.asList(cellData[i]));
        rowData.add(colData);
    }
    Vector columnNamesV = new Vector(Arrays.asList(columnNames));
   
    table = new JTable(rowData, columnNamesV);


取得表格的行数和列数(Getting the Number of Rows and Columns in a JTable Component)
    int rows = table.getRowCount();
    int cols = table.getColumnCount();

向表格添加一行(Appending a Row to a JTable Component)
To add a row of data to a JTable component, you need to add it to its table model. A simple implementation of a table model that supports appending row data is DefaultTableModel. By default, a table uses a DefaultTableModel.
如果你想要向表格添加一行数据,你需要把这行数据添加到表格的模块部分。一种支持行添加的简单实现是DefaultTableModel。表格缺省使用的就是DefaultTableModel。


    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
   
    // 创建两列(Create a couple of columns)
    model.addColumn("Col1");
    model.addColumn("Col2");
   
    // 添加一行(Append a row)
    model.addRow(new Object[]{"v1", "v2"});
    // 现在就有两行两列了(there are now 2 rows with 2 columns)
   
    // 添加比列个数少的数据行(Append a row with fewer values than columns.)
    // 这时会从左到右依次添加数据,没有对应数据项的单元格将被赋空The left-most fields in the new row are populated
    // with the supplied values (left-to-right) and fields
    // without values are set to null.
    model.addRow(new Object[]{"v1"});
    // 现在有了三行两列there are now 3 rows with 2 columns
   
    // 添加比列个数多的数据行(Append a row with more values than columns.)
    // 多余的数据将被忽略(The extra values are ignored.)
    model.addRow(new Object[]{"v1", "v2", "v3"});
    // 现在有了四行两列(there are now 4 rows with 2 columns)


在表格中插入一行(Inserting a Row in a JTable Component)
To insert a row of data to a JTable component, you need to insert it to its table model. A simple implementation of a table model that supports the insertion of row data is DefaultTableModel.
如果你想要向表格插入一行数据,你需要把这行数据插入到表格的模块部分。一种支持行插入的简单实现是DefaultTableModel。表格缺省使用的就是DefaultTableModel。
When inserting a row using DefaultTableModel.insertRow(), the position of the new row must be specified. Positions are locations between rows. For example, if there are 2 rows in a table, there are 3 possible positions - - 0, 1,and 2. Inserting a row at position 0 makes the new row the first row. Inserting a row at position 2 makes the new row the last row.
当使用DefaultTableModel.insertRow()方法插入数据时,你需要指定新行的位置。位置是行与行之间的定位点。比如说,一个两行的表格就有三个可能的插入位置,0,1和2。在位置0插入时,新数据行会出现在表格的第一行。在位置3插入时,新行会出现在表格的最后一行。

When inserting a row with fewer values than columns, the left-most fields in the new row are populated with the supplied values (left-to-right) and the fields without values are set to null. When inserting a row with more values than columns, the extra values are ignored.
添加比列个数少的数据行时,会从左到右依次添加数据,没有对应数据项的单元格将被赋空;添加比列个数多的数据行时,多余的数据将被忽略。

    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
   
    // 新建两列(Create a couple of columns)
    model.addColumn("Col1");
    model.addColumn("Col2");
   
    // 创建第一行(Create the first row)
    model.insertRow(0, new Object[]{"r1"});
   
    // 在第一行插入(Insert a row so that it becomes the first row)
    model.insertRow(0, new Object[]{"r2"});
   
    // 在位置2插入一行(Insert a row at position p)
    int p = 2;
    model.insertRow(p, new Object[]{"r3"});
   
    // 在第二行前插入一行(Insert a row before the second row)
    int r = 1;
    model.insertRow(r, new Object[]{"r4"});
    // 新行将成为第二行(the new row is now the second row)
   
    // 在第二行后插入一行(Insert a row after the second row)
    r = 1;
    model.insertRow(r+1, new Object[]{"r5"});
    // 新行将成为第三行(the new row is now the third row)
   
    // 使用插入的方法添加一行(Append a row)
    model.insertRow(model.getRowCount(), new Object[]{"r5"});

从表格中删除一行(Removing a Row from a JTable Component)
To remove a row of data from a JTable component, you need to remove it from its table model. A simple implementation of a table model that supports the removal of row data is DefaultTableModel.
如果你想要从表格删除一行数据,你需要把这行数据从表格的模块部分删除。一种支持行删除的简单实现是DefaultTableModel。表格缺省使用的就是DefaultTableModel。

When removing a row using DefaultTableModel.removeRow(), the index of the row must be specified. Row indices start from 0. For example, if there are 2 rows in a table, the index of the first row is 0 and the index of the second row is 1. Removing a row at index 0 removes the first row.
当使用DefaultTableModel.removeRow()方法删除数据时,你需要指定删除行的序号。行序号从零开始,比如一个两行的表格,它的第一行的序号是0,第二行的序号是1。删除序号零所在的行就是删除第一行。

    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
   
    // 建立一些数据(Create some data)
    model.addColumn("Col1");
    model.addRow(new Object[]{"r1"});
    model.addRow(new Object[]{"r2"});
    model.addRow(new Object[]{"r3"});
   
    // 删除第一行(Remove the first row)
    model.removeRow(0);
   
    // 删除最后一行(Remove the last row)
    model.removeRow(model.getRowCount()-1);

在表格中进行行移动(Moving a Row in a JTable Component)
To move a row of data to a JTable component, you need to move it in its table model. A simple implementation of a table model that supports the moving of row data is DefaultTableModel.
如果你想要从表格移动一行数据,应该把这行数据在表格的模块部分移动。一种支持行移动的简单实现是DefaultTableModel。表格缺省使用的就是DefaultTableModel。

When moving one or more rows using DefaultTableModel.moveRow(), the set of the rows to be moved and the destination position must be specified. The contiguous set of rows to be moved is specified with the index of the starting row and the index of the end row. Row indices start from 0. For example, if there are 2 rows in a table, the index of the first row is 0 and the index of the second row is 1.


The destination is also a position. Positions are locations between rows. For example, if there are 2 rows in a table, there are 3 possible positions - - 0, 1,and 2. The important thing to remember about the destination position is that it specifies the position in the row data after the rows to be moved are taken out of the row data. For example, if you want to move the first row to the end of the table, the destination position is not getRowCount(), it is getRowCount()-1. Similarly, if you want to move the first 2 lines to the end of the table, the destination position is getRowCount()-2.

The way in which the parameters are interpreted is somewhat awkward. The more conventional method is to either make the end index exclusive (like String.substring()) or replace end with a length. Also, it is easier to specify the destination as it exists before the operation, and not have to pretend that the rows to be moved are taken out. A version of moveRow() with more conventional parameter interpretation is provided in betterMoveRow().

    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
   
    // Create some data
    model.addColumn("Col1");
    model.addRow(new Object[]{"r1"});
    model.addRow(new Object[]{"r2"});
    model.addRow(new Object[]{"r3"});
   
    // Move the first row to the end of the table
    model.moveRow(0, 0, model.getRowCount()-1);
    betterMoveRow(model, 0, 1, model.getRowCount());
   
    // Move the last row to the beginning of the table
    model.moveRow(model.getRowCount()-1, model.getRowCount()-1, 0);
    betterMoveRow(model, model.getRowCount()-1, model.getRowCount(), 0);
   
    // Move the first two rows to the end of the table
    model.moveRow(0, 1, model.getRowCount()-2);
    betterMoveRow(model, 0, 2, model.getRowCount());
   
    // Move the last two rows to the start of the table
    model.moveRow(model.getRowCount()-2, model.getRowCount()-1, 0);
    betterMoveRow(model, model.getRowCount()-2, model.getRowCount(), 0);
   
    // A better version of moveRow().
    // Moves all rows contained between the positions start and end
    // to the position specified by dest.
    public static void betterMoveRow(DefaultTableModel model, int start, int end, int dest) {
        int count = end - start;
        if (count <= 0) {
            return;
        }
        if (dest > start) {
            dest = Math.max(start, dest-count);
        }
        end--;
        model.moveRow(start, end, dest);
    }

原创粉丝点击