Creating a JTable Component - Java Swing

Java examples for Swing:JTable

Description

Creating a JTable Component

Demo Code

import java.util.Arrays;
import java.util.Vector;

import javax.swing.JTable;

public class Main {
  public static void main(String[] argv) {
    // Create with initial data
    Object[][] cellData = { { "row1-col1", "row1-col2" },
        { "row2-col1", "row2-col2" } };
    String[] columnNames = { "col1", "col2" };

    JTable table = new JTable(cellData, columnNames);
    // 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);// w ww  .j  a  v a  2s  . co m
    }
    Vector columnNamesV = new Vector(Arrays.asList(columnNames));

    table = new JTable(rowData, columnNamesV);
  }
}

Related Tutorials