List of usage examples for javax.swing.table TableColumn getPreferredWidth
public int getPreferredWidth()
TableColumn
. From source file:com.googlecode.vfsjfilechooser2.filepane.VFSFilePane.java
private void fixNameColumnWidth(int viewWidth) { TableColumn nameCol = detailsTable.getColumnModel().getColumn(COLUMN_FILENAME); int tableWidth = detailsTable.getPreferredSize().width; if (tableWidth < viewWidth) { nameCol.setPreferredWidth((nameCol.getPreferredWidth() + viewWidth) - tableWidth); }// w w w . j a v a2 s. co m }
From source file:edu.ku.brc.specify.tasks.subpane.ESResultsTablePanel.java
/** * @param table/* w w w. j av a 2s . co m*/ * @param model */ protected void autoResizeColWidth(final JTable table, final DefaultTableModel model) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setModel(model); int margin = 5; DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); int preferredWidthTotal = 0; int renderedWidthTotal = 0; int[] colWidths = new int[table.getColumnCount()]; int[] strWidths = new int[table.getColumnCount()]; for (int i = 0; i < table.getColumnCount(); i++) { int vColIndex = i; TableColumn col = colModel.getColumn(vColIndex); int width = 0; TableCellRenderer headerRenderer = col.getHeaderRenderer(); if (headerRenderer instanceof JLabel) { ((JLabel) headerRenderer).setHorizontalAlignment(SwingConstants.CENTER); } // Get width of column header TableCellRenderer renderer = col.getCellRenderer(); 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 int strWidth = 0; boolean isString = model.getColumnClass(i) == String.class; for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, vColIndex); Object objVal = table.getValueAt(r, vColIndex); if (isString && objVal != null) { strWidth = Math.max(strWidth, ((String) objVal).length()); } comp = renderer.getTableCellRendererComponent(table, objVal, false, false, r, vColIndex); width = Math.max(width, comp.getPreferredSize().width); } // Add margin width += 2 * margin; preferredWidthTotal += col.getPreferredWidth(); colWidths[i] = width; strWidths[i] = strWidth; renderedWidthTotal += width; } if (renderedWidthTotal > preferredWidthTotal) { for (int i = 0; i < table.getColumnCount(); i++) { TableColumn col = colModel.getColumn(i); TableCellRenderer renderer = col.getCellRenderer(); if (renderer != null) { ((JLabel) renderer).setHorizontalAlignment( strWidths[i] > 20 ? SwingConstants.LEFT : SwingConstants.CENTER); //((JLabel)renderer).setHorizontalAlignment(SwingConstants.LEFT); } if (model.getColumnCount() > 3 && renderedWidthTotal > preferredWidthTotal) { col.setPreferredWidth(colWidths[i]); } } } ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()) .setHorizontalAlignment(SwingConstants.LEFT); // table.setAutoCreateRowSorter(true); table.getTableHeader().setReorderingAllowed(false); }
From source file:edu.ku.brc.ui.UIHelper.java
/** * @param table//www. java2s .com * @param model * @return */ public static JTable autoResizeColWidth(final JTable table, final DefaultTableModel model) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setModel(model); int margin = 5; DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); int preferredWidthTotal = 0; int renderedWidthTotal = 0; int[] colWidths = new int[table.getColumnCount()]; for (int i = 0; i < table.getColumnCount(); i++) { int vColIndex = i; 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; preferredWidthTotal += col.getPreferredWidth(); colWidths[i] = width; renderedWidthTotal += width; } if (renderedWidthTotal > preferredWidthTotal) { for (int i = 0; i < table.getColumnCount(); i++) { colModel.getColumn(i).setPreferredWidth(colWidths[i]); } } ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()) .setHorizontalAlignment(SwingConstants.LEFT); // table.setAutoCreateRowSorter(true); table.getTableHeader().setReorderingAllowed(false); return table; }
From source file:edu.ku.brc.ui.UIHelper.java
/** * Calculates and sets the each column to it preferred size. NOTE: This * method also sets the table height to 10 rows. * //from w w w. j a v a 2s. com * @param table the table to fix up * @param numRowsHeight the number of rows to make the table height (or null not to set it) */ public static void calcColumnWidths(final JTable table, final Integer numRowsHeight, final Integer maxWidth) { if (table != null) { JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) { defaultHeaderRenderer = header.getDefaultRenderer(); } TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); int margin = columns.getColumnMargin(); // only JDK1.3 int rowCount = data.getRowCount(); int totalWidth = 0; for (int i = columns.getColumnCount() - 1; i >= 0; --i) { TableColumn column = columns.getColumn(i); int columnIndex = column.getModelIndex(); int width = -1; TableCellRenderer h = column.getHeaderRenderer(); if (h == null) h = defaultHeaderRenderer; if (h != null) // Not explicitly impossible { Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, i); width = c.getPreferredSize().width; } for (int row = rowCount - 1; row >= 0; --row) { TableCellRenderer r = table.getCellRenderer(row, i); Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false, false, row, i); width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer if (maxWidth != null) { width = Math.min(width, maxWidth); } } if (width >= 0) { column.setPreferredWidth(width + margin); // <1.3: without margin } else { // ??? } totalWidth += column.getPreferredWidth(); } // If you like; This does not make sense for two many columns! Dimension size = table.getPreferredScrollableViewportSize(); //if (totalWidth > size.width) { if (numRowsHeight != null) { size.height = Math.min(size.height, table.getRowHeight() * numRowsHeight); } size.width = totalWidth; table.setPreferredScrollableViewportSize(size); } } }
From source file:ua.com.fielden.platform.example.swing.egi.EgiExample.java
private void addTotalsFooterTo(final EntityGridInspector egi, final JPanel topPanel) { // final JPanel panel = new JPanel(new MigLayout("insets 0", "[]", "[]0[]push[]")); // panel.add(egi.getTableHeader(), "grow, wrap"); // panel.add(egi, "grow, wrap"); final JScrollPane scrollPane = new JScrollPane(egi); topPanel.add(scrollPane, "grow, wrap"); final JPanel footer = new JPanel(new MigLayout("nogrid, insets 0")); // footer.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED)); final List<JComponent> totalsComponents = new ArrayList<JComponent>(); for (int i = 0; i < egi.getColumnCount(); i++) { final TableColumn column = egi.getColumnModel().getColumn(i); final JComponent totalsComponent = i % 2 == 0 ? new JTextField("totals " + i) : new JLabel(); totalsComponent.setPreferredSize(new Dimension(column.getPreferredWidth(), 30)); footer.add(totalsComponent, "grow"); totalsComponents.add(totalsComponent); }//from www . j av a 2 s . c o m final JScrollPane footerPane = new JScrollPane(footer, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); topPanel.add(footerPane, "grow, wrap, h 40::"); topPanel.add(scrollPane.getHorizontalScrollBar(), "grow, wrap"); scrollPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() { @Override public void adjustmentValueChanged(final AdjustmentEvent e) { footerPane.getViewport().setViewPosition(new Point(e.getValue(), 0)); } }); egi.getColumnModel().addColumnModelListener(new TableColumnModelListener() { @Override public void columnAdded(final TableColumnModelEvent e) { } @Override public void columnMarginChanged(final ChangeEvent e) { final TableColumn column = egi.getTableHeader().getResizingColumn(); if (column != null) { final JComponent totalsComponent = totalsComponents .get(egi.convertColumnIndexToView(column.getModelIndex())); totalsComponent.setPreferredSize(new Dimension(column.getWidth(), totalsComponent.getHeight())); footer.revalidate(); } } @Override public void columnMoved(final TableColumnModelEvent e) { final JComponent fromComponent = totalsComponents.get(e.getFromIndex()); totalsComponents.set(e.getFromIndex(), totalsComponents.get(e.getToIndex())); totalsComponents.set(e.getToIndex(), fromComponent); footer.removeAll(); for (int i = 0; i < egi.getColumnCount(); i++) { footer.add(totalsComponents.get(i), "grow, gap 0 0 0 0"); } footer.revalidate(); } @Override public void columnRemoved(final TableColumnModelEvent e) { } @Override public void columnSelectionChanged(final ListSelectionEvent e) { } }); // // return panel; }