List of usage examples for java.awt Component getPreferredSize
public Dimension getPreferredSize()
From source file:Main.java
/** * Makes all specified component sizes equal. * * @param components components to modify *//*from w w w.j a v a2 s . co m*/ public static void equalizeComponentsSize(final Component... components) { final Dimension maxSize = new Dimension(0, 0); for (final Component c : components) { if (c != null) { final Dimension ps = c.getPreferredSize(); maxSize.width = Math.max(maxSize.width, ps.width); maxSize.height = Math.max(maxSize.height, ps.height); } } for (final Component c : components) { if (c != null) { c.setPreferredSize(maxSize); } } }
From source file:Main.java
/** * Resizes the columns to match content while keeping the table the same * size. This means that the last column may be larger than the content. *//*ww w .ja v a2 s . c o m*/ public static void resizeTableColumns(JTable table) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); int totalWidth = 0; for (int i = 0; i < table.getColumnCount(); i++) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(i); if (i == table.getColumnCount() - 1) { col.setPreferredWidth(table.getWidth() - totalWidth); table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); return; } int width = 0; TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, -1, i); width = Math.max(width, comp.getPreferredSize().width); for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, i); comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i), false, false, r, i); width = Math.max(width, comp.getPreferredSize().width); } totalWidth += width + 2; col.setPreferredWidth(width + 2); } }
From source file:Main.java
/** * Pack specified column. Sets the preferred width of the specified visible * column so it is will be just wide enough to show the column head and the * widest cell value in the column + {@code margin} each side. * /* w ww. ja va2 s . c o m*/ * Taken from http://www.exampledepot.com/egs/javax.swing.table/PackCol.html * * NB: If a table has more than 100 rows, then only the width of the column * header will be checked and not the widest cell value. Otherwise it could * take too long. * * @param table * table with column to pack * @param vColIndex * column to pack * @param margin * margin to leave at each side of the column (resulting in an * additional width of 2*margin pixels). */ public 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; // because checking the width of each row can be time consuming, // only do so if less than 101 rows. boolean checkDataWidth = (table.getRowCount() < 101); if (checkDataWidth) { // Get maximum width from all 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); }
From source file:Main.java
/** * Calculates the optimal width for the column of the given table. The * calculation is based on the preferred width of the header and cell * renderer.//from w w w . j a va2s. co m * <br> * Taken from the newsgoup de.comp.lang.java with some modifications.<br> * Taken from FOPPS/EnhancedTable - http://fopps.sourceforge.net/<br> * * @param table the table to calculate the column width * @param col the column to calculate the widths * @return the width, -1 if error */ public static int calcColumnWidth(JTable table, int col) { int width = calcHeaderWidth(table, col); if (width == -1) return width; TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); int rowCount = data.getRowCount(); TableColumn column = columns.getColumn(col); try { for (int row = rowCount - 1; row >= 0; --row) { Component c = table.prepareRenderer(table.getCellRenderer(row, col), row, col); width = Math.max(width, c.getPreferredSize().width + 10); } } catch (Exception e) { e.printStackTrace(); } return width; }
From source file:Main.java
/** * Calculates the optimal width for the header of the given table. The * calculation is based on the preferred width of the header renderer. * * @param table the table to calculate the column width * @param col the column to calculate the widths * @return the width, -1 if error *//*from w w w. jav a 2 s . com*/ public static int calcHeaderWidth(JTable table, int col) { if (table == null) return -1; if (col < 0 || col > table.getColumnCount()) { System.out.println("invalid col " + col); return -1; } JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) defaultHeaderRenderer = header.getDefaultRenderer(); TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); TableColumn column = columns.getColumn(col); 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, col); width = c.getPreferredSize().width + 5; } return width; }
From source file:Main.java
public static void initComponentHeight(final Component... components) { if (components == null) { return;//from w w w . jav a 2 s . c o m } for (final Component component : components) { if ((component instanceof JComboBox) || (component instanceof JButton)) { component.setPreferredSize(new Dimension(component.getPreferredSize().width, 22)); } else if (component instanceof JTextField) { final String lf = UIManager.getLookAndFeel().getClass().getName(); int i = 22; if (lf.equals(LAF_METAL)) { i = 23; } component.setPreferredSize(new Dimension(component.getPreferredSize().width, i)); } } }
From source file:Main.java
/** * Auto fit the column of a table.//from w w w . j av a2 s . c o m * @param table the table for which to auto fit the columns. * @param columnIndex the index of the column to auto fit. * @param maxWidth the maximum width that a column can take (like Integer.MAX_WIDTH). */ public static void autoFitTableColumn(JTable table, int columnIndex, int maxWidth) { TableModel model = table.getModel(); TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); int rowCount = table.getRowCount(); for (int i = columnIndex >= 0 ? columnIndex : model.getColumnCount() - 1; i >= 0; i--) { TableColumn column = table.getColumnModel().getColumn(i); int headerWidth = headerRenderer .getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0) .getPreferredSize().width; int cellWidth = 0; for (int j = 0; j < rowCount; j++) { Component comp = table.getDefaultRenderer(model.getColumnClass(i)) .getTableCellRendererComponent(table, table.getValueAt(j, i), false, false, 0, i); int preferredWidth = comp.getPreferredSize().width; // Artificial space to look nicer. preferredWidth += 10; cellWidth = Math.max(cellWidth, preferredWidth); } // Artificial space for the sort icon. headerWidth += 20; column.setPreferredWidth(Math.min(Math.max(headerWidth, cellWidth) + table.getRowMargin(), maxWidth)); if (columnIndex >= 0) { break; } } }
From source file:Main.java
public static void padPreferredWidthDeep(Component component, int padding) { if (component instanceof Container) { for (Component child : ((Container) component).getComponents()) { padPreferredWidthDeep(child, padding); }// w w w . j a v a2 s .c o m } component.setPreferredSize(new Dimension((int) component.getPreferredSize().getWidth() + padding, (int) component.getPreferredSize().getHeight())); }
From source file:Main.java
public static JPanel createKV(final Component key, final Component value, final int keyWidth, final boolean fill) { initComponentHeight(key, value);//from w w w . java 2s. c o m if (keyWidth > 0) { key.setPreferredSize(new Dimension(keyWidth, key.getPreferredSize().height)); } final JPanel jp = new JPanel(new GridBagLayout()); final GridBagConstraints gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.WEST; gbc.gridx = 0; gbc.gridy = 0; gbc.insets = new Insets(0, 0, 0, 4); jp.add(key, gbc); gbc.gridx = 1; gbc.insets = new Insets(0, 0, 0, 0); gbc.weightx = 1.0; if (fill) { gbc.fill = GridBagConstraints.HORIZONTAL; } jp.add(value, gbc); return jp; }
From source file:Main.java
/** * Packs all table rows to their preferred height. * * @param table table to process/*from ww w . j a va 2s. c o m*/ */ public static void packRowHeights(final JTable table) { for (int row = 0; row < table.getRowCount(); row++) { int maxHeight = 0; for (int column = 0; column < table.getColumnCount(); column++) { final TableCellRenderer cellRenderer = table.getCellRenderer(row, column); final Object valueAt = table.getValueAt(row, column); final Component renderer = cellRenderer.getTableCellRendererComponent(table, valueAt, false, false, row, column); final int heightPreferable = renderer != null ? renderer.getPreferredSize().height : 0; maxHeight = Math.max(heightPreferable, maxHeight); } table.setRowHeight(row, maxHeight); } }