Example usage for javax.swing.table DefaultTableColumnModel DefaultTableColumnModel

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

Introduction

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

Prototype

public DefaultTableColumnModel() 

Source Link

Document

Creates a default table column model.

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);/*from  w w w . ja va2s .c om*/
    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);/*  w w w .j  av  a2s . 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:FrozenColumnHeader.java

public void addNotify() {
    TableColumn column;//from  ww  w.j  ava 2 s .com
    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:MainClass.java

public MainClass() {
    super("Row Header Test");
    setSize(300, 200);/*from   w w w.j  av a 2  s .c  o m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    TableModel tm = new AbstractTableModel() {
        String data[] = { "", "a", "b", "c", "d", "e" };

        String headers[] = { "Row #", "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" };

        public int getColumnCount() {
            return data.length;
        }

        public int getRowCount() {
            return 1000;
        }

        public String getColumnName(int col) {
            return headers[col];
        }

        public Object getValueAt(int row, int col) {
            return data[col] + row;
        }
    };

    TableColumnModel cm = new DefaultTableColumnModel() {
        boolean first = true;

        public void addColumn(TableColumn tc) {
            if (first) {
                first = false;
                return;
            }
            tc.setMinWidth(150);
            super.addColumn(tc);
        }
    };

    TableColumnModel rowHeaderModel = new DefaultTableColumnModel() {
        boolean first = true;

        public void addColumn(TableColumn tc) {
            if (first) {
                tc.setMaxWidth(tc.getPreferredWidth());
                super.addColumn(tc);
                first = false;
            }
        }
    };

    JTable jt = new JTable(tm, cm);
    JTable headerColumn = new JTable(tm, rowHeaderModel);
    jt.createDefaultColumnsFromModel();
    headerColumn.createDefaultColumnsFromModel();

    jt.setSelectionModel(headerColumn.getSelectionModel());

    headerColumn.setBackground(Color.lightGray);
    headerColumn.setColumnSelectionAllowed(false);
    headerColumn.setCellSelectionEnabled(false);

    JViewport jv = new JViewport();
    jv.setView(headerColumn);
    jv.setPreferredSize(headerColumn.getMaximumSize());

    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JScrollPane jsp = new JScrollPane(jt);
    jsp.setRowHeader(jv);
    jsp.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerColumn.getTableHeader());
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:RowHeaderTable.java

public RowHeaderTable() {
    super("Row Header Test");
    setSize(300, 200);//ww  w . j av  a2  s.  c  o  m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    TableModel tm = new AbstractTableModel() {
        String data[] = { "", "a", "b", "c", "d", "e" };

        String headers[] = { "Row #", "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" };

        public int getColumnCount() {
            return data.length;
        }

        public int getRowCount() {
            return 1000;
        }

        public String getColumnName(int col) {
            return headers[col];
        }

        // Synthesize some entries using the data values & the row #
        public Object getValueAt(int row, int col) {
            return data[col] + row;
        }
    };

    // Create a column model for the main table. This model ignores the
    // first
    // column added, and sets a minimum width of 150 pixels for all others.
    TableColumnModel cm = new DefaultTableColumnModel() {
        boolean first = true;

        public void addColumn(TableColumn tc) {
            // Drop the first column . . . that'll be the row header
            if (first) {
                first = false;
                return;
            }
            tc.setMinWidth(150); // just for looks, really...
            super.addColumn(tc);
        }
    };

    // Create a column model that will serve as our row header table. This
    // model picks a maximum width and only stores the first column.
    TableColumnModel rowHeaderModel = new DefaultTableColumnModel() {
        boolean first = true;

        public void addColumn(TableColumn tc) {
            if (first) {
                tc.setMaxWidth(tc.getPreferredWidth());
                super.addColumn(tc);
                first = false;
            }
            // Drop the rest of the columns . . . this is the header column
            // only
        }
    };

    JTable jt = new JTable(tm, cm);

    // Set up the header column and get it hooked up to everything
    JTable headerColumn = new JTable(tm, rowHeaderModel);
    jt.createDefaultColumnsFromModel();
    headerColumn.createDefaultColumnsFromModel();

    // Make sure that selections between the main table and the header stay
    // in sync (by sharing the same model)
    jt.setSelectionModel(headerColumn.getSelectionModel());

    // Make the header column look pretty
    //headerColumn.setBorder(BorderFactory.createEtchedBorder());
    headerColumn.setBackground(Color.lightGray);
    headerColumn.setColumnSelectionAllowed(false);
    headerColumn.setCellSelectionEnabled(false);

    // Put it in a viewport that we can control a bit
    JViewport jv = new JViewport();
    jv.setView(headerColumn);
    jv.setPreferredSize(headerColumn.getMaximumSize());

    // With out shutting off autoResizeMode, our tables won't scroll
    // correctly (horizontally, anyway)
    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    // We have to manually attach the row headers, but after that, the
    // scroll
    // pane keeps them in sync
    JScrollPane jsp = new JScrollPane(jt);
    jsp.setRowHeader(jv);
    jsp.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerColumn.getTableHeader());
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:hermes.browser.dialog.GeneralRendererConfigPanel.java

public void setHermesConfig(HermesConfig hermesConfig) {
    this.hermesConfig = hermesConfig;

    initState();//from   w ww  .  j a v  a  2 s.  co m

    configModel = new ListConfigTableModel();

    for (MessageRenderer rConfig : HermesBrowser.getRendererManager().getRenderers()) {

        configModel.addItem(rConfig.getDisplayName());

    }

    classTable.setModel(configModel);

    DefaultTableColumnModel cm = new DefaultTableColumnModel();
    cm.addColumn(new TableColumn());

    classTable.setColumnModel(cm);
}

From source file:forge.itemmanager.views.ItemListView.java

@Override
public void setup(final ItemManagerConfig config, final Map<ColumnDef, ItemTableColumn> colOverrides) {
    final Iterable<T> selectedItemsBefore = getSelectedItems();
    final DefaultTableColumnModel colmodel = new DefaultTableColumnModel();

    //ensure columns ordered properly
    final List<ItemTableColumn> columns = new LinkedList<ItemTableColumn>();
    for (final ItemColumnConfig colConfig : config.getCols().values()) {
        if (colOverrides == null || !colOverrides.containsKey(colConfig.getDef())) {
            columns.add(new ItemTableColumn(new ItemColumn(colConfig)));
        } else {/*from   ww w  .  j a  va2s  .co  m*/
            columns.add(colOverrides.get(colConfig.getDef()));
        }
    }
    Collections.sort(columns, new Comparator<ItemTableColumn>() {
        @Override
        public int compare(final ItemTableColumn arg0, final ItemTableColumn arg1) {
            return Integer.compare(arg0.getIndex(), arg1.getIndex());
        }
    });

    //hide table header if only showing single string column
    final boolean hideHeader = (config.getCols().size() == 1 && config.getCols().containsKey(ColumnDef.STRING));

    getPnlOptions().removeAll();

    if (config.getShowUniqueCardsOption()) {
        final FCheckBox chkBox = new FCheckBox("Unique Cards Only", this.itemManager.getWantUnique());
        chkBox.setFont(ROW_FONT);
        chkBox.setToolTipText("Toggle whether to show unique cards only");
        chkBox.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(final ChangeEvent arg0) {
                final boolean wantUnique = chkBox.isSelected();
                if (itemManager.getWantUnique() == wantUnique) {
                    return;
                }
                itemManager.setWantUnique(wantUnique);
                itemManager.refresh();

                if (itemManager.getConfig() != null) {
                    itemManager.getConfig().setUniqueCardsOnly(wantUnique);
                }
            }
        });
        getPnlOptions().add(chkBox);
    }

    int modelIndex = 0;
    for (final ItemTableColumn col : columns) {
        col.setModelIndex(modelIndex++);
        if (col.isVisible()) {
            colmodel.addColumn(col);
        }

        if (!hideHeader) {
            final FCheckBox chkBox = new FCheckBox(
                    StringUtils.isEmpty(col.getShortName()) ? col.getLongName() : col.getShortName(),
                    col.isVisible());
            chkBox.setFont(ROW_FONT);
            chkBox.setToolTipText(col.getLongName());
            chkBox.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(final ChangeEvent arg0) {
                    final boolean visible = chkBox.isSelected();
                    if (col.isVisible() == visible) {
                        return;
                    }
                    col.setVisible(visible);

                    if (col.isVisible()) {
                        colmodel.addColumn(col);

                        //move column into proper position
                        final int oldIndex = colmodel.getColumnCount() - 1;
                        int newIndex = col.getIndex();
                        for (int i = 0; i < col.getIndex(); i++) {
                            if (!columns.get(i).isVisible()) {
                                newIndex--;
                            }
                        }
                        if (newIndex < oldIndex) {
                            colmodel.moveColumn(oldIndex, newIndex);
                        }
                    } else {
                        colmodel.removeColumn(col);
                    }
                    ItemManagerConfig.save();
                }
            });
            getPnlOptions().add(chkBox);
        }
    }

    if (hideHeader) {
        this.table.getTableHeader().setPreferredSize(new Dimension(0, 0));
    } else {
        this.table.getTableHeader().setPreferredSize(new Dimension(0, ROW_HEIGHT));
    }

    this.tableModel.addListeners();
    this.table.setModel(this.tableModel);
    this.table.setColumnModel(colmodel);

    this.tableModel.setup();
    this.refresh(selectedItemsBefore, 0, 0);
}

From source file:DefaultsDisplay.java

protected JTable createDefaultsTable() {
    JTable table = new JTable(new UIDefaultsTableModel());
    table.setRowHeight(rowHeight);/*from   w w w .j a  v  a 2s .com*/
    table.setShowHorizontalLines(false);
    table.setShowVerticalLines(false);
    table.setIntercellSpacing(new Dimension(0, 0));
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    initFilters(table);

    DefaultTableColumnModel columnModel = new DefaultTableColumnModel();

    Color rowColors[] = new Color[2];
    rowColors[0] = UIManager.getColor("Table.background");
    rowColors[1] = new Color((int) (rowColors[0].getRed() * .90), (int) (rowColors[0].getGreen() * .95),
            (int) (rowColors[0].getBlue() * .95));

    int width = 0;

    TableColumn column = new TableColumn();
    column.setCellRenderer(new KeyRenderer(rowColors));
    column.setModelIndex(UIDefaultsTableModel.KEY_COLUMN);
    column.setHeaderValue("Key");
    column.setPreferredWidth(250);
    columnModel.addColumn(column);
    width += column.getPreferredWidth();

    column = new TableColumn();
    column.setCellRenderer(new RowRenderer(rowColors));
    column.setModelIndex(UIDefaultsTableModel.TYPE_COLUMN);
    column.setHeaderValue("Type");
    column.setPreferredWidth(250);
    columnModel.addColumn(column);
    width += column.getPreferredWidth();

    column = new TableColumn();
    column.setCellRenderer(new ValueRenderer(rowColors));
    column.setModelIndex(UIDefaultsTableModel.VALUE_COLUMN);
    column.setHeaderValue("Value");
    column.setPreferredWidth(300);
    columnModel.addColumn(column);
    width += column.getPreferredWidth();

    table.setColumnModel(columnModel);

    table.setPreferredScrollableViewportSize(new Dimension(width, 12 * rowHeight));

    return table;

}

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

/**
 * Class Constructor./*  w  ww.  j a  va  2s. com*/
 *
 * @param model
 *
 * @see
 */
public RegistryObjectsTable(TableModel model) {
    // Gives a TableColumnModel so that AutoCreateColumnsFromModel will be false.
    super(model, new DefaultTableColumnModel());

    this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    if (model instanceof RegistryObjectsTableModel) {
        tableModel = (RegistryObjectsTableModel) model;
    } else if (model instanceof TableSorter) {
        tableModel = (RegistryObjectsTableModel) (((TableSorter) model).getModel());
    } else {
        Object[] unexpectedTableModelArgs = { model };
        MessageFormat form = new MessageFormat(resourceBundle.getString("error.unexpectedTableModel"));
        throw new IllegalArgumentException(form.format(unexpectedTableModelArgs));
    }

    setToolTipText(resourceBundle.getString("tip.registryObjectsTable"));

    setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    ListSelectionModel rowSM = getSelectionModel();
    stdRowHeight = getRowHeight();
    setRowHeight(stdRowHeight * 3);

    rowSM.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            ListSelectionModel lsm = (ListSelectionModel) e.getSource();

            if (!lsm.isSelectionEmpty()) {
                setSelectedRow(lsm.getMinSelectionIndex());
            } else {
                setSelectedRow(-1);
            }
        }
    });

    createPopup();

    addRenderers();

    // Add listener to self so that I can bring up popup menus on right mouse click
    popupListener = new PopupListener();
    addMouseListener(popupListener);

    //add listener for 'authenticated' bound property
    RegistryBrowser.getInstance().addPropertyChangeListener(RegistryBrowser.PROPERTY_AUTHENTICATED, this);

    //add listener for 'locale' bound property
    RegistryBrowser.getInstance().addPropertyChangeListener(RegistryBrowser.PROPERTY_LOCALE, this);
}

From source file:com.vgi.mafscaling.ClosedLoop.java

private JTable createAfrDataTable(JPanel panel, String tableName, int gridy) {
    final JTable afrTable = new JTable() {
        private static final long serialVersionUID = 6526901361175099297L;

        public boolean isCellEditable(int row, int column) {
            return false;
        };/*from w  w w  .j  ava2s  .c  om*/
    };
    DefaultTableColumnModel afrModel = new DefaultTableColumnModel();
    final TableColumn afrColumn = new TableColumn(0, 250);
    afrColumn.setHeaderValue(tableName);
    afrModel.addColumn(afrColumn);
    JTableHeader lblAfrTableName = afrTable.getTableHeader();
    lblAfrTableName.setColumnModel(afrModel);
    lblAfrTableName.setReorderingAllowed(false);
    DefaultTableCellRenderer headerRenderer = (DefaultTableCellRenderer) lblAfrTableName.getDefaultRenderer();
    headerRenderer.setHorizontalAlignment(SwingConstants.LEFT);

    GridBagConstraints gbc_lblAfrTableName = new GridBagConstraints();
    gbc_lblAfrTableName.insets = new Insets((gridy == 0 ? 0 : 5), 0, 0, 0);
    gbc_lblAfrTableName.anchor = GridBagConstraints.PAGE_START;
    gbc_lblAfrTableName.fill = GridBagConstraints.HORIZONTAL;
    gbc_lblAfrTableName.gridx = 0;
    gbc_lblAfrTableName.gridy = gridy;
    panel.add(lblAfrTableName, gbc_lblAfrTableName);

    afrTable.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            afrColumn.setWidth(afrTable.getWidth());
        }
    });
    afrTable.getTableHeader().setReorderingAllowed(false);
    afrTable.setColumnSelectionAllowed(true);
    afrTable.setCellSelectionEnabled(true);
    afrTable.setBorder(new LineBorder(new Color(0, 0, 0)));
    afrTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    afrTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    afrTable.setModel(new DefaultTableModel(AfrTableRowCount, AfrTableColumnCount));
    Utils.initializeTable(afrTable, ColumnWidth);

    if (tableName.equals(Afr1TableName)) {
        Format[][] formatMatrix = { { new DecimalFormat("#"), new DecimalFormat("0.00") } };
        NumberFormatRenderer renderer = (NumberFormatRenderer) afrTable.getDefaultRenderer(Object.class);
        renderer.setFormats(formatMatrix);
    } else if (tableName.equals(Afr2TableName)) {
        Format[][] formatMatrix = { { new DecimalFormat("#"), new DecimalFormat("0.00") },
                { new DecimalFormat("#"), new DecimalFormat("#") } };
        NumberFormatRenderer renderer = (NumberFormatRenderer) afrTable.getDefaultRenderer(Object.class);
        renderer.setFormats(formatMatrix);
    }

    GridBagConstraints gbc_afrTable = new GridBagConstraints();
    gbc_afrTable.insets = new Insets(0, 0, 0, 0);
    gbc_afrTable.anchor = GridBagConstraints.PAGE_START;
    gbc_afrTable.gridx = 0;
    gbc_afrTable.gridy = gridy + 1;
    panel.add(afrTable, gbc_afrTable);

    excelAdapter.addTable(afrTable, true, false);

    return afrTable;
}