Example usage for javax.swing.table TableColumn setMinWidth

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

Introduction

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

Prototype

@BeanProperty(description = "The minimum width of the column.")
public void setMinWidth(int minWidth) 

Source Link

Document

Sets the TableColumn's minimum width to minWidth, adjusting the new minimum width if necessary to ensure that 0 <= minWidth <= maxWidth.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int rows = 3;
    int cols = 3;
    JTable table = new JTable(rows, cols);

    TableColumn col = table.getColumnModel().getColumn(0);
    col.setMinWidth(100);
    col.setMaxWidth(100);/*from w  ww.j  a v a2s . c o m*/
    col.setPreferredWidth(100);

}

From source file:org.drugis.mtc.gui.results.ResultsComponentFactory.java

private static JTable createTableWithoutHeaders(NetworkRelativeEffectTableModel dm) {
    final JTable table = new JTable(dm);
    table.setTableHeader(null);//w  ww  . ja  v a2  s  . c o m
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    for (final TableColumn c : Collections.list(table.getColumnModel().getColumns())) {
        c.setMinWidth(170);
        c.setPreferredWidth(170);
    }

    TableCopyHandler.registerCopyAction(table);
    return table;
}

From source file:MainClass.java

public MainClass() {
    super("Row Header Test");
    setSize(300, 200);/*w  w w .  j a v a2 s  .  c  om*/
    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);/* w ww  . j a v  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];
        }

        // 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:net.sf.profiler4j.console.ClassListPanel.java

/**
 * This method initializes classesTable//from w  w  w .  j  a  v  a 2 s.c  om
 * 
 * @return javax.swing.JTable
 */
private JTable getClassesTable() {
    if (classesTable == null) {
        classesTable = new JTable();
        classesTable.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        classesTable.setFont(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 12));
        classesTable.setModel(getClassListTableModel());
        classesTable.setRowHeight(20);
        classesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                if (classesTable.getSelectedRowCount() > 0) {
                    addAsRuleButton.setEnabled(true);
                } else {
                    addAsRuleButton.setEnabled(false);
                }

            }
        });
        TableColumn c = classesTable.getColumnModel().getColumn(0);
        c.setMinWidth(50);
        c.setMaxWidth(50);

        c = classesTable.getColumnModel().getColumn(1);
        c.setMinWidth(300);
        c.setCellRenderer(new DefaultTableCellRenderer() {
            Font f1 = new Font("Tahoma", java.awt.Font.PLAIN, 12);
            Font f2 = new Font("Tahoma", java.awt.Font.BOLD, 12);

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

                if (isSelected) {
                    setFont(f2);
                } else {
                    setFont(f1);
                }
                if (classListTableModel.getRow(row).info.isInstrumented()) {
                    if (isSelected) {
                        setForeground(Color.YELLOW);
                        setBackground(Color.BLUE);
                    } else {
                        setBackground(Color.decode("#bbffbb"));
                        setForeground(Color.BLACK);
                    }
                } else {
                    if (isSelected) {
                        setForeground(Color.WHITE);
                        setBackground(Color.BLUE);
                    } else {
                        setBackground(Color.WHITE);
                        setForeground(Color.BLACK);
                    }
                }
                setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
                return this;
            }
        });

    }
    return classesTable;
}

From source file:edu.ku.brc.af.ui.forms.formatters.DataObjFieldFormatMultiplePanel.java

/**
 * //from   www  .j av a2 s .  c o  m
 */
private void setFormatSwitchTblColumnProperties() {
    // set details of 1st column (field values)
    TableColumnModel model = formatSwitchTbl.getColumnModel();
    TableColumn column = model.getColumn(model.getColumnIndex(FIELD_VALUE_COL));
    column.setMinWidth(20);
    column.setMaxWidth(300);
    column.setPreferredWidth(70);
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
    renderer.setHorizontalAlignment(SwingConstants.CENTER);
    column.setCellRenderer(renderer);

    // set details of 3rd column (ellipsis buttons)
    column = model.getColumn(model.getColumnIndex(ELLIPSIS_BUTTON_COL));
    column.setCellRenderer(new EditDataObjFormatButtonRenderer());
    column.setCellEditor(new EditDataObjFormatButtonEditor(createCheckBox()));
    column.setMinWidth(20);
    column.setMaxWidth(20);
    column.setPreferredWidth(20);
}

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);
    }/* ww  w  .  ja  v  a 2 s  .c  o  m*/
    column.setHeaderValue(model.getColumnName(index));
    return column;
}

From source file:org.drugis.addis.gui.builder.NetworkMetaAnalysisView.java

private void setColumnWidths(final JTable table) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    for (final TableColumn c : Collections.list(table.getColumnModel().getColumns())) {
        c.setMinWidth(170);
        c.setPreferredWidth(170);//from   ww w  .  j av a2  s .  c o  m
    }
}

From source file:net.sf.keystore_explorer.gui.dialogs.extensions.DViewExtensions.java

private void initComponents() {
    ExtensionsTableModel extensionsTableModel = new ExtensionsTableModel();
    jtExtensions = new JKseTable(extensionsTableModel);

    TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(extensionsTableModel);
    sorter.setComparator(2, new ObjectIdComparator());
    jtExtensions.setRowSorter(sorter);//  w  ww . ja  v  a 2 s .c om

    jtExtensions.setShowGrid(false);
    jtExtensions.setRowMargin(0);
    jtExtensions.getColumnModel().setColumnMargin(0);
    jtExtensions.getTableHeader().setReorderingAllowed(false);
    jtExtensions.setAutoResizeMode(JKseTable.AUTO_RESIZE_ALL_COLUMNS);
    jtExtensions.setRowHeight(Math.max(18, jtExtensions.getRowHeight()));

    for (int i = 0; i < jtExtensions.getColumnCount(); i++) {
        TableColumn column = jtExtensions.getColumnModel().getColumn(i);
        column.setHeaderRenderer(
                new ExtensionsTableHeadRend(jtExtensions.getTableHeader().getDefaultRenderer()));
        column.setCellRenderer(new ExtensionsTableCellRend());
    }

    TableColumn criticalCol = jtExtensions.getColumnModel().getColumn(0);
    criticalCol.setResizable(false);
    criticalCol.setMinWidth(28);
    criticalCol.setMaxWidth(28);
    criticalCol.setPreferredWidth(28);

    ListSelectionModel selectionModel = jtExtensions.getSelectionModel();
    selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    selectionModel.addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent evt) {
            if (!evt.getValueIsAdjusting()) {
                try {
                    CursorUtil.setCursorBusy(DViewExtensions.this);
                    updateExtensionValue();
                } finally {
                    CursorUtil.setCursorFree(DViewExtensions.this);
                }
            }
        }
    });

    jspExtensionsTable = PlatformUtil.createScrollPane(jtExtensions,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    jspExtensionsTable.getViewport().setBackground(jtExtensions.getBackground());

    jpExtensionsTable = new JPanel(new BorderLayout(5, 5));
    jpExtensionsTable.setPreferredSize(new Dimension(500, 200));
    jpExtensionsTable.add(jspExtensionsTable, BorderLayout.CENTER);

    jpExtensionValue = new JPanel(new BorderLayout(5, 5));

    jlExtensionValue = new JLabel(res.getString("DViewExtensions.jlExtensionValue.text"));

    jpExtensionValue.add(jlExtensionValue, BorderLayout.NORTH);

    jepExtensionValue = new JEditorPane();
    jepExtensionValue.setFont(new Font(Font.MONOSPACED, Font.PLAIN, LnfUtil.getDefaultFontSize()));
    jepExtensionValue.setEditable(false);
    jepExtensionValue.setToolTipText(res.getString("DViewExtensions.jtaExtensionValue.tooltip"));
    // JGoodies - keep uneditable color same as editable
    jepExtensionValue.putClientProperty("JTextArea.infoBackground", Boolean.TRUE);

    // for displaying URLs in extensions as clickable links
    jepExtensionValue.setContentType("text/html");
    jepExtensionValue.addHyperlinkListener(this);
    // use default font and foreground color from the component
    jepExtensionValue.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);

    jspExtensionValue = PlatformUtil.createScrollPane(jepExtensionValue,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    jpExtensionValueTextArea = new JPanel(new BorderLayout(5, 5));
    jpExtensionValueTextArea.setPreferredSize(new Dimension(500, 200));
    jpExtensionValueTextArea.add(jspExtensionValue, BorderLayout.CENTER);

    jpExtensionValue.add(jpExtensionValueTextArea, BorderLayout.CENTER);

    jbAsn1 = new JButton(res.getString("DViewExtensions.jbAsn1.text"));

    PlatformUtil.setMnemonic(jbAsn1, res.getString("DViewExtensions.jbAsn1.mnemonic").charAt(0));
    jbAsn1.setToolTipText(res.getString("DViewExtensions.jbAsn1.tooltip"));
    jbAsn1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            try {
                CursorUtil.setCursorBusy(DViewExtensions.this);
                asn1DumpPressed();
            } finally {
                CursorUtil.setCursorFree(DViewExtensions.this);
            }
        }
    });

    jpExtensionValueAsn1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    jpExtensionValueAsn1.add(jbAsn1);

    jpExtensionValue.add(jpExtensionValueAsn1, BorderLayout.SOUTH);

    jpExtensions = new JPanel(new GridLayout(2, 1, 5, 5));
    jpExtensions.setBorder(new CompoundBorder(new EmptyBorder(5, 5, 5, 5),
            new CompoundBorder(new EtchedBorder(), new EmptyBorder(5, 5, 5, 5))));

    jpExtensions.add(jpExtensionsTable);
    jpExtensions.add(jpExtensionValue);

    jbOK = new JButton(res.getString("DViewExtensions.jbOK.text"));
    jbOK.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            okPressed();
        }
    });

    jpOK = PlatformUtil.createDialogButtonPanel(jbOK, false);

    extensionsTableModel.load(extensions);

    if (extensionsTableModel.getRowCount() > 0) {
        jtExtensions.changeSelection(0, 0, false, false);
    }

    getContentPane().add(jpExtensions, BorderLayout.CENTER);
    getContentPane().add(jpOK, BorderLayout.SOUTH);

    setResizable(false);

    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent evt) {
            closeDialog();
        }
    });

    getRootPane().setDefaultButton(jbOK);

    pack();

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            jbOK.requestFocus();
        }
    });
}

From source file:net.sf.profiler4j.console.ProjectDialog.java

/**
 * This method initializes rulesTable/*w w w .  ja  v  a 2s  .c o m*/
 * 
 * @return javax.swing.JTable
 */
private JTable getRulesTable() {
    if (rulesTable == null) {
        rulesTable = new JTable();
        rulesTable.setModel(ruleTableModel);
        rulesTable.setRowMargin(4);
        rulesTable.setRowHeight(24);
        rulesTable.setFont(new Font("Monospaced", Font.PLAIN, 14));
        TableColumn c;

        c = rulesTable.getColumnModel().getColumn(0);
        c.setMinWidth(300);

        c = rulesTable.getColumnModel().getColumn(1);
        c.setMinWidth(80);
        c.setMaxWidth(80);

        JComboBox editorCb = new JComboBox();
        for (Rule.Action a : Rule.Action.values()) {
            editorCb.addItem(a);
        }
        c.setCellEditor(new DefaultCellEditor(editorCb));
        c.setCellRenderer(new DefaultTableCellRenderer() {
            Font font = new Font("Monospaced", Font.BOLD, 13);

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                    boolean hasFocus, int row, int column) {
                super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                Rule.Action r = (Rule.Action) value;
                setHorizontalAlignment(CENTER);
                setFont(font);
                if (r == Rule.Action.ACCEPT) {
                    setBackground(Color.GREEN);
                } else {
                    setBackground(Color.RED);
                }
                if (isSelected) {
                    setForeground(Color.YELLOW);
                } else {
                    setForeground(Color.BLACK);
                }
                return this;
            }
        });

    }
    return rulesTable;
}