Example usage for javax.swing.table TableColumnModel addColumn

List of usage examples for javax.swing.table TableColumnModel addColumn

Introduction

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

Prototype

public void addColumn(TableColumn aColumn);

Source Link

Document

Appends aColumn to the end of the tableColumns array.

Usage

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);
    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);/*w ww .  j  a v a2s  .co m*/
    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);
    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);/*  w ww  . j  a v  a  2s  .  c  om*/
    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 w  ww. 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);

    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");
        }/*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);

    frame.getContentPane().add(table, 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");
        }//from   w  w  w  .  j  ava  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:FrozenColumnHeader.java

public void addNotify() {
    TableColumn column;// w w w . ja v a 2  s .  c  o  m
    super.addNotify();
    TableColumnModel mainModel = mainTable.getColumnModel();
    TableColumnModel headerModel = new DefaultTableColumnModel();
    int frozenWidth = 0;
    for (int i = 0; i < columnCount; i++) {
        column = mainModel.getColumn(0);
        mainModel.removeColumn(column);
        headerModel.addColumn(column);
        frozenWidth += column.getPreferredWidth() + headerModel.getColumnMargin();
    }
    headerTable.setColumnModel(headerModel);
    Component columnHeader = getColumnHeader().getView();
    getColumnHeader().setView(null);
    JScrollPane mainScrollPane = (JScrollPane) SwingUtilities.getAncestorOfClass(JScrollPane.class, mainTable);
    mainScrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, new JLabel("..."));

    headerTable.setPreferredScrollableViewportSize(new Dimension(frozenWidth, 0));
}

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
 *///w w  w .  j  ava 2s  .  com
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.aw.swing.mvp.binding.component.BndSJTable.java

public void resetHeader() {
    TableColumnModel colModel = jTable.getColumnModel();
    int colsNumber = getColumnsInfo().length;
    int colsModel = colModel.getColumnCount() + fixedColumns;
    if (colsNumber == colsModel) {
        if (getSelectorColumn() != null) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JTable fixedJTable = (JTable) jTable.getClientProperty(CP_FIXED_TABLE);
                    if (fixedJTable != null) {
                        fixedJTable.getTableHeader().repaint();
                    } else {
                        jTable.getTableHeader().repaint();
                    }//www  .j  a va  2s  .c  o  m
                }
            });
        }
        return;
    }
    if (colsNumber > colsModel) {
        int delta = colsNumber - colModel.getColumnCount();
        for (int i = 0; i < delta; i++) {
            colModel.addColumn(new TableColumn());
        }
    }
    for (int i = 0; i < colsNumber; i++) {
        TableColumn column = colModel.getColumn(i);
        column.setHeaderValue(getColumnsInfo()[i].getColumnHeader());
    }
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JTable fixedJTable = (JTable) jTable.getClientProperty(CP_FIXED_TABLE);
            if (fixedJTable != null) {
                fixedJTable.getTableHeader().repaint();
            }
            jTable.getTableHeader().repaint();
        }
    });
}