Search in sources :

Example 21 with DefaultTableColumnModel

use of javax.swing.table.DefaultTableColumnModel in project zencash-swing-wallet-ui by ZencashOfficial.

the class AddressBookPanel method buildTablePanel.

private JScrollPane buildTablePanel() {
    table = new JTable(new AddressBookTableModel(), new DefaultTableColumnModel());
    TableColumn nameColumn = new TableColumn(0);
    TableColumn addressColumn = new TableColumn(1);
    table.addColumn(nameColumn);
    table.addColumn(addressColumn);
    // one at a time
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.getSelectionModel().addListSelectionListener(new AddressListSelectionListener());
    table.addMouseListener(new AddressMouseListener());
    // TODO: isolate in utility
    TableCellRenderer renderer = table.getCellRenderer(0, 0);
    Component comp = renderer.getTableCellRendererComponent(table, "123", false, false, 0, 0);
    table.setRowHeight(new Double(comp.getPreferredSize().getHeight()).intValue() + 2);
    JScrollPane scrollPane = new JScrollPane(table);
    return scrollPane;
}
Also used : JScrollPane(javax.swing.JScrollPane) TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableColumnModel(javax.swing.table.DefaultTableColumnModel) JTable(javax.swing.JTable) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn)

Example 22 with DefaultTableColumnModel

use of javax.swing.table.DefaultTableColumnModel in project mafscaling by vimsh.

the class LogStats method createDataPanel.

private void createDataPanel(JPanel dataPanel) {
    TableColumnModel dataTableModel = new DefaultTableColumnModel();
    dataTableModel.addColumn(new TableColumn(0, 250));
    dataTable = new JTable() {

        private static final long serialVersionUID = 6526901361175099297L;

        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    dataTable.setColumnSelectionAllowed(true);
    dataTable.setCellSelectionEnabled(true);
    dataTable.setBorder(new LineBorder(new Color(0, 0, 0)));
    dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    dataTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    dataTable.setModel(new DefaultTableModel(DataTableRowCount, DataTableColumnCount));
    dataTable.setTableHeader(null);
    Utils.initializeTable(dataTable, ColumnWidth);
    Format[][] formatMatrix = { { new DecimalFormat("0.00"), new DecimalFormat("0.00") } };
    NumberFormatRenderer renderer = (NumberFormatRenderer) dataTable.getDefaultRenderer(Object.class);
    renderer.setFormats(formatMatrix);
    GridBagConstraints gbc_dataTable = new GridBagConstraints();
    gbc_dataTable.insets = insets3;
    gbc_dataTable.anchor = GridBagConstraints.PAGE_START;
    gbc_dataTable.fill = GridBagConstraints.BOTH;
    gbc_dataTable.weightx = 1.0;
    gbc_dataTable.weighty = 1.0;
    gbc_dataTable.gridx = 0;
    gbc_dataTable.gridy = 1;
    gbc_dataTable.gridwidth = 14;
    JScrollPane scrollPane = new JScrollPane(dataTable);
    dataPanel.add(scrollPane, gbc_dataTable);
    excelAdapter.addTable(dataTable, false, true, true, true, true, true, true, true, true);
}
Also used : JScrollPane(javax.swing.JScrollPane) GridBagConstraints(java.awt.GridBagConstraints) DefaultTableColumnModel(javax.swing.table.DefaultTableColumnModel) LineBorder(javax.swing.border.LineBorder) Color(java.awt.Color) DefaultTableModel(javax.swing.table.DefaultTableModel) DecimalFormat(java.text.DecimalFormat) TableColumnModel(javax.swing.table.TableColumnModel) DefaultTableColumnModel(javax.swing.table.DefaultTableColumnModel) TableColumn(javax.swing.table.TableColumn) JTable(javax.swing.JTable)

Example 23 with DefaultTableColumnModel

use of javax.swing.table.DefaultTableColumnModel in project mafscaling by vimsh.

the class ACompCalc method createDataTable.

private JTable createDataTable(JPanel panel, String tableName, int colCount, int rowCount, int gridx, int gridy, boolean isOriginalTable, boolean extendRows, boolean extendCols) {
    final JTable table;
    ExcelAdapter excelAdapter;
    if (isOriginalTable) {
        table = new JTable() {

            private static final long serialVersionUID = 3218402382894083287L;

            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        excelAdapter = new ExcelAdapter() {

            protected void onPaste(JTable table, boolean extendRows, boolean extendCols) {
                if (table.getSelectedRows() == null || table.getSelectedRows().length == 0 || table.getSelectedColumns() == null || table.getSelectedColumns().length == 0)
                    return;
                if (table.getSelectedRows()[0] != 0 || table.getSelectedColumns()[0] != 0) {
                    JOptionPane.showMessageDialog(null, "Please paste copied table into the first cell", "Error", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                super.onPaste(table, extendRows, extendCols);
                validateTable(table);
                clearRunTables();
            }
        };
        excelAdapter.addTable(table, false, true, false, false, true, true, false, extendRows, extendCols);
        excelAdapterList.add(excelAdapter);
    } else {
        table = new JTable() {

            private static final long serialVersionUID = -3754572906310312568L;

            public boolean isCellEditable(int row, int column) {
                return isNonOriginalTableCellEditable(row, column);
            }

            public Component prepareEditor(TableCellEditor editor, int row, int column) {
                Component c = super.prepareEditor(editor, row, column);
                if (c instanceof JTextComponent)
                    ((JTextComponent) c).selectAll();
                return c;
            }
        };
        excelAdapter = new ExcelAdapter();
        excelAdapter.addTable(table, false, true, false, false, true, true, false, false, false);
        excelAdapterList.add(excelAdapter);
    }
    DefaultTableColumnModel tableModel = new DefaultTableColumnModel();
    final TableColumn tableColumn = new TableColumn(0, (colCount == 1 ? ColumnWidth : TableWidth));
    tableColumn.setHeaderValue(tableName);
    tableModel.addColumn(tableColumn);
    JTableHeader lblTableHeaderName = table.getTableHeader();
    lblTableHeaderName.setColumnModel(tableModel);
    lblTableHeaderName.setReorderingAllowed(false);
    DefaultTableCellRenderer headerRenderer = (DefaultTableCellRenderer) lblTableHeaderName.getDefaultRenderer();
    headerRenderer.setHorizontalAlignment(SwingConstants.LEFT);
    GridBagConstraints gbc_lblTableName = new GridBagConstraints();
    gbc_lblTableName.insets = new Insets((gridy == 0 ? 0 : 5), 0, 0, 0);
    gbc_lblTableName.anchor = GridBagConstraints.NORTHWEST;
    gbc_lblTableName.fill = GridBagConstraints.HORIZONTAL;
    gbc_lblTableName.gridx = gridx;
    gbc_lblTableName.gridy = gridy;
    panel.add(lblTableHeaderName, gbc_lblTableName);
    table.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            tableColumn.setWidth(table.getWidth());
        }
    });
    table.addMouseListener(new MouseAdapter() {

        public void mousePressed(MouseEvent event) {
            JTable eventTable = (JTable) event.getSource();
            int colIdx = eventTable.getSelectedColumn();
            int rowIdx = eventTable.getSelectedRow();
            JTable[] tables = new JTable[] { origTable, newTable, corrTable, corrCountTable };
            for (JTable t : tables) {
                if (t == null || t == eventTable)
                    continue;
                if (t.getColumnCount() - 1 >= colIdx && t.getRowCount() - 1 >= rowIdx) {
                    t.setColumnSelectionInterval(colIdx, colIdx);
                    t.setRowSelectionInterval(rowIdx, rowIdx);
                }
            }
        }

        public void mouseReleased(MouseEvent event) {
            JTable eventTable = (JTable) event.getSource();
            int[] cols = eventTable.getSelectedColumns();
            int[] rows = eventTable.getSelectedRows();
            int lastColIdx = cols.length - 1;
            int lastRowIdx = rows.length - 1;
            JTable[] tables = new JTable[] { origTable, newTable, corrTable, corrCountTable };
            for (JTable t : tables) {
                if (t == null || t == eventTable)
                    continue;
                if (t.getColumnCount() - 1 >= cols[lastColIdx] && t.getRowCount() - 1 >= rows[lastRowIdx]) {
                    t.setColumnSelectionInterval(cols[0], cols[lastColIdx]);
                    t.setRowSelectionInterval(rows[0], rows[lastRowIdx]);
                }
            }
        }
    });
    table.setName(tableName);
    table.getTableHeader().setReorderingAllowed(false);
    table.setColumnSelectionAllowed(true);
    table.setCellSelectionEnabled(true);
    table.setBorder(new LineBorder(new Color(0, 0, 0)));
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    table.setModel(new DefaultTableModel(rowCount, colCount));
    table.putClientProperty("terminateEditOnFocusLost", true);
    Utils.initializeTable(table, ColumnWidth);
    formatTable(table);
    GridBagConstraints gbc_table = new GridBagConstraints();
    gbc_table.insets = insets0;
    gbc_table.anchor = GridBagConstraints.NORTHWEST;
    gbc_table.gridx = gridx;
    gbc_table.gridy = gridy + 1;
    panel.add(table, gbc_table);
    return table;
}
Also used : GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) MouseEvent(java.awt.event.MouseEvent) DefaultTableColumnModel(javax.swing.table.DefaultTableColumnModel) LineBorder(javax.swing.border.LineBorder) Color(java.awt.Color) DefaultTableModel(javax.swing.table.DefaultTableModel) MouseAdapter(java.awt.event.MouseAdapter) JTableHeader(javax.swing.table.JTableHeader) JTextComponent(javax.swing.text.JTextComponent) TableColumn(javax.swing.table.TableColumn) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) JTable(javax.swing.JTable) TableCellEditor(javax.swing.table.TableCellEditor) ComponentEvent(java.awt.event.ComponentEvent) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) ComponentAdapter(java.awt.event.ComponentAdapter)

Example 24 with DefaultTableColumnModel

use of javax.swing.table.DefaultTableColumnModel in project gitblit by gitblit.

the class Utils method packColumn.

// Sets the preferred width of the visible column specified by vColIndex.
// The column will be just wide enough to show the column head and the
// widest cell in the column. margin pixels are added to the left and right
// (resulting in an additional width of 2*margin pixels).
private static void packColumn(JTable table, int vColIndex, int margin) {
    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
    TableColumn col = colModel.getColumn(vColIndex);
    int width = 0;
    // Get width of column header
    TableCellRenderer renderer = col.getHeaderRenderer();
    if (renderer == null) {
        renderer = table.getTableHeader().getDefaultRenderer();
    }
    Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
    width = comp.getPreferredSize().width;
    // Get maximum width of column data
    for (int r = 0; r < table.getRowCount(); r++) {
        renderer = table.getCellRenderer(r, vColIndex);
        comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false, r, vColIndex);
        width = Math.max(width, comp.getPreferredSize().width);
    }
    // Add margin
    width += 2 * margin;
    // Set the width
    col.setPreferredWidth(width);
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableColumnModel(javax.swing.table.DefaultTableColumnModel) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn)

Example 25 with DefaultTableColumnModel

use of javax.swing.table.DefaultTableColumnModel in project zaproxy by zaproxy.

the class ParamsPanel method setParamsTableColumnSizes.

private void setParamsTableColumnSizes() {
    for (int i = 0; i < paramsTable.getColumnCount(); i++) {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) paramsTable.getColumnModel();
        TableColumn col = colModel.getColumn(i);
        int width = 0;
        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = paramsTable.getTableHeader().getDefaultRenderer();
        }
        Component comp = renderer.getTableCellRendererComponent(paramsTable, col.getHeaderValue(), false, false, 0, 0);
        width = comp.getPreferredSize().width;
        col.setPreferredWidth(width + 2);
    }
    // value
    paramsTable.getColumnModel().getColumn(6).setPreferredWidth(999);
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) DefaultTableColumnModel(javax.swing.table.DefaultTableColumnModel) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn)

Aggregations

DefaultTableColumnModel (javax.swing.table.DefaultTableColumnModel)25 TableColumn (javax.swing.table.TableColumn)18 TableCellRenderer (javax.swing.table.TableCellRenderer)11 TableColumnModel (javax.swing.table.TableColumnModel)8 Component (java.awt.Component)6 JTableHeader (javax.swing.table.JTableHeader)6 JTable (javax.swing.JTable)5 DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer)5 JScrollPane (javax.swing.JScrollPane)4 DefaultTableModel (javax.swing.table.DefaultTableModel)4 Color (java.awt.Color)3 GridBagConstraints (java.awt.GridBagConstraints)3 LineBorder (javax.swing.border.LineBorder)3 Insets (java.awt.Insets)2 ComponentAdapter (java.awt.event.ComponentAdapter)2 ComponentEvent (java.awt.event.ComponentEvent)2 MouseAdapter (java.awt.event.MouseAdapter)2 MouseEvent (java.awt.event.MouseEvent)2 DecimalFormat (java.text.DecimalFormat)2 TableCellUtilities (pcgen.gui2.util.table.TableCellUtilities)2