Example usage for javax.swing.table TableColumn TableColumn

List of usage examples for javax.swing.table TableColumn TableColumn

Introduction

In this page you can find the example usage for javax.swing.table TableColumn TableColumn.

Prototype

public TableColumn(int modelIndex) 

Source Link

Document

Cover method, using a default width of 75, a null renderer and a null editor.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    model = (DefaultTableModel) table.getModel();
    TableColumn col = new TableColumn(model.getColumnCount());

    // Ensure that auto-create is off
    if (table.getAutoCreateColumnsFromModel()) {
        throw new IllegalStateException();
    }/*from   w  w w . j  a va 2 s  . c o  m*/
    col.setHeaderValue("Col3");
    table.addColumn(col);
    model.addColumn("Col3", new Object[] { "v3" });

}

From source file:Main.java

public static void main(String args[]) {
    Object rows[][] = { { "one", "ichi" }, { "two", "ni" }, { "three", "san" }, { "four", "shi" },
            { "five", "go" }, { "six", "roku" }, { "seven", "shichi" }, { "eight", "hachi" }, { "nine", "kyu" },
            { "ten", "ju" } };
    Object headers[] = { "English", "Japanese" };
    String title = (args.length == 0 ? "JTable Sample" : args[0]);
    JFrame frame = new JFrame(title);

    TableColumnModel columnModel = new DefaultTableColumnModel();
    TableColumn firstColumn = new TableColumn(1);
    firstColumn.setHeaderValue(headers[1]);
    columnModel.addColumn(firstColumn);// ww w .  ja  v a 2  s . com
    TableColumn secondColumn = new TableColumn(0);
    secondColumn.setHeaderValue(headers[0]);
    columnModel.addColumn(secondColumn);
    ListSelectionModel selectionModel = new DefaultListSelectionModel();
    selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    //    JTable table = new JTable(model, columnModel, selectionModel);
    JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:TableColumnTester.java

public static void main(String args[]) {
    Object rows[][] = { { "one", "ichi" }, { "two", "ni" }, { "three", "san" }, { "four", "shi" },
            { "five", "go" }, { "six", "roku" }, { "seven", "shichi" }, { "eight", "hachi" }, { "nine", "kyu" },
            { "ten", "ju" } };
    Object headers[] = { "English", "Japanese" };
    String title = (args.length == 0 ? "JTable Sample" : args[0]);
    JFrame frame = new JFrame(title);
    TableModel model = new DefaultTableModel(rows, headers);
    TableColumnModel columnModel = new DefaultTableColumnModel();
    TableColumn firstColumn = new TableColumn(1);
    firstColumn.setHeaderValue(headers[1]);
    columnModel.addColumn(firstColumn);/*from w  ww.  j av  a 2 s .  c o  m*/
    TableColumn secondColumn = new TableColumn(0);
    secondColumn.setHeaderValue(headers[0]);
    columnModel.addColumn(secondColumn);
    ListSelectionModel selectionModel = new DefaultListSelectionModel();
    selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    //    JTable table = new JTable(model, columnModel, selectionModel);
    JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    Object rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    Object headers[] = { "Upper", "Lower" };

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(rows, headers);

    TableColumnModelListener tableColumnModelListener = new TableColumnModelListener() {
        public void columnAdded(TableColumnModelEvent e) {
            System.out.println("Added");
        }//www .  j  a  va  2  s  .  c  o m

        public void columnMarginChanged(ChangeEvent e) {
            System.out.println("Margin");
        }

        public void columnMoved(TableColumnModelEvent e) {
            System.out.println("Moved");
        }

        public void columnRemoved(TableColumnModelEvent e) {
            System.out.println("Removed");
        }

        public void columnSelectionChanged(ListSelectionEvent e) {
            System.out.println("Selection Changed");
        }
    };

    TableColumnModel columnModel = table.getColumnModel();
    columnModel.addColumnModelListener(tableColumnModelListener);

    columnModel.setColumnMargin(12);

    TableColumn column = new TableColumn(1);
    columnModel.addColumn(column);

    JScrollPane pane = new JScrollPane(table);
    frame.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:ColumnModelSample.java

public static void main(String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" } };
    final Object headers[] = { "English", "#" };
    JFrame frame = new JFrame("Scrollless Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(rows, headers);

    TableColumnModelListener tableColumnModelListener = new TableColumnModelListener() {
        public void columnAdded(TableColumnModelEvent e) {
            System.out.println("Added");
        }//from  www .ja  va 2  s . c o  m

        public void columnMarginChanged(ChangeEvent e) {
            System.out.println("Margin");
        }

        public void columnMoved(TableColumnModelEvent e) {
            System.out.println("Moved");
        }

        public void columnRemoved(TableColumnModelEvent e) {
            System.out.println("Removed");
        }

        public void columnSelectionChanged(ListSelectionEvent e) {
            System.out.println("Selection Changed");
        }
    };
    TableColumnModel columnModel = table.getColumnModel();
    columnModel.addColumnModelListener(tableColumnModelListener);

    columnModel.setColumnMargin(12);

    TableColumn column = new TableColumn(1);
    columnModel.addColumn(column);

    JScrollPane pane = new JScrollPane(table);
    frame.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    Object rows[][] = { { "one", "ichi - \u4E00" }, { "two", "ni - \u4E8C" }, { "three", "san - \u4E09" },
            { "four", "shi - \u56DB" }, { "five", "go - \u4E94" }, { "six", "roku - \u516D" },
            { "seven", "shichi - \u4E03" }, { "eight", "hachi - \u516B" }, { "nine", "kyu - \u4E5D" },
            { "ten", "ju - \u5341" } };
    Object headers[] = { "English", "Japanese" };
    JFrame frame = new JFrame("Scrollless Table");
    JTable table = new JTable(rows, headers);

    TableColumnModelListener tableColumnModelListener = new TableColumnModelListener() {
        public void columnAdded(TableColumnModelEvent e) {
            System.out.println("Added");
        }/*w w  w  .j a  v a 2 s  . c o m*/

        public void columnMarginChanged(ChangeEvent e) {
            System.out.println("Margin");
        }

        public void columnMoved(TableColumnModelEvent e) {
            System.out.println("Moved");
        }

        public void columnRemoved(TableColumnModelEvent e) {
            System.out.println("Removed");
        }

        public void columnSelectionChanged(ListSelectionEvent e) {
            System.out.println("Selection Changed");
        }
    };

    TableColumnModel columnModel = table.getColumnModel();
    columnModel.addColumnModelListener(tableColumnModelListener);

    columnModel.setColumnMargin(12);

    TableColumn column = new TableColumn(1);
    columnModel.addColumn(column);

    frame.getContentPane().add(table, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:com.projity.pm.graphic.spreadsheet.SpreadSheet.java

public void createDefaultColumnsFromModel(ArrayList fieldArray) {
    // Remove any current columns
    TableColumnModel cm = getColumnModel();
    while (cm.getColumnCount() > 0) {
        cm.removeColumn(cm.getColumn(0));
    }/*from www .j ava  2  s .  com*/

    // Create new columns from the data model info
    int colCount = fieldArray.size();
    for (int i = 0; i < colCount; i++) {
        TableColumn newColumn = new TableColumn(i);
        addColumn(newColumn);
    }

    //      TableModel m = getModel();
    //      if (m != null) {
    //         // Remove any current columns
    //         TableColumnModel cm = getColumnModel();
    //         while (cm.getColumnCount() > 0) {
    //            cm.removeColumn(cm.getColumn(0));
    //         }
    //
    //         // Create new columns from the data model info
    //         for (int i = 0; i < m.getColumnCount(); i++) {
    //            TableColumn newColumn = new TableColumn(i);
    //            addColumn(newColumn);
    //         }
    //      }
}

From source file:org.jdal.swing.ListTableModel.java

/**
 * Create a TableColumnModel for JTable. 
 * Try to use sizes and cell renderers from property descriptors.
 * @return a new TableColumnModel based on PropertyDescriptors
 *///from w  w w  . ja  v a2  s .  c  o m
public TableColumnModel getTableColumnModel() {
    TableColumnModel tcm = new DefaultTableColumnModel();
    int baseIndex = 0;
    if (usingChecks) {
        TableColumn tableColumn = new TableColumn(0);
        tableColumn.setMaxWidth(50);
        tcm.addColumn(tableColumn);
        baseIndex++;
    }
    for (int i = 0; i < columnNames.size(); i++) {
        String name = this.columnNames.get(i);
        TableColumn tableColumn = new TableColumn(baseIndex + i);
        tableColumn.setHeaderValue(displayNames.get(i));

        if (pds != null && pds.size() > 0) {
            PropertyDescriptor descriptor = pds.get(i);
            // property values for TableColumns
            if (descriptor != null) {
                Integer maxWidth = getColumnWidth(name);
                if (maxWidth != null)
                    tableColumn.setMaxWidth(maxWidth.intValue());
                tableColumn.setCellRenderer(getColumnRenderer(name));
                tableColumn.setCellEditor(getColumnEditor(name));
            }
        }

        tcm.addColumn(tableColumn);
    }

    if (usingActions) {
        baseIndex += columnNames.size();
        for (int i = 0; i < actions.size(); i++) {
            TableColumn tableColumn = new TableColumn(baseIndex + i);
            tableColumn.setCellRenderer(new ActionCellRenderer());
            tableColumn.setMaxWidth(50);
            //   tableColumn.setCellEditor(new ActionCellEditor())
            tcm.addColumn(tableColumn);
        }
    }

    return tcm;
}

From source file:com.hp.alm.ali.idea.ui.MultipleItemsDialog.java

private TableColumn createColumn(int index, TableModel model, int preferredWidth, Integer minWidth) {
    TableColumn column = new TableColumn(index);
    column.setPreferredWidth(preferredWidth);
    if (minWidth != null) {
        column.setMinWidth(minWidth);/*from  w  ww .j  av  a 2  s  .  c o m*/
    }
    column.setHeaderValue(model.getColumnName(index));
    return column;
}

From source file:it.cnr.icar.eric.client.ui.swing.RegistryObjectsTable.java

/**
 * Creates default columns for the table from
 * the data model using the <code>getColumnCount</code> method
 * defined in the <code>TableModel</code> interface.
 *
 * Clears any existing columns before creating the
 * new columns based on information from the model.
 *
 * Overrides base class behaviour by setting the column width as a % of the
 * viewport width./* w  w  w . j  ava  2s.  c o  m*/
 */
public void createDefaultColumnsFromModel() {
    TableModel m = getModel();
    if (m != null) {
        // Remove any current columns
        TableColumnModel cm = getColumnModel();
        while (cm.getColumnCount() > 0) {
            cm.removeColumn(cm.getColumn(0));
        }

        // get parent width
        int parentWidth = 0;
        Component parent = getParent();
        if (parent != null) {
            parentWidth = parent.getWidth();
        }

        // Create new columns from the data model info
        int columnCount = m.getColumnCount();
        for (int i = 0; i < m.getColumnCount(); i++) {
            int width = tableModel.getColumnWidth(i);
            if (width == 0) {
                width = parentWidth / columnCount;
            } else {
                //Width is a % of the viewport width
                width = (width * parentWidth) / 100;
            }
            TableColumn newColumn = new TableColumn(i);
            newColumn.setPreferredWidth(width);
            addColumn(newColumn);
        }
    }
}