// This method from http://www.exampledepot.com/egs/javax.swing.table/PackCol.html public int 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); return width; }
/** * Calculates the maximum size dimensions for the specified panal given the components in the * specified parent container. */ public Dimension maximumLayoutSize(Container target) { synchronized (target.getTreeLock()) { Dimension dim = new Dimension(0, 0); int size = actions.size(); if ((grip != null) && grip.isVisible()) { Dimension d = grip.getPreferredSize(); dim.width += d.width; dim.width += hgap; } Component last = null; for (int i = 0; i < size; i++) { Component comp = (Component) actions.elementAt(i); if (comp.isVisible()) { Dimension d = comp.getPreferredSize(); dim.width += d.width; dim.height = Math.max(dim.height, d.height); dim.width += hgap; last = comp; } } if (last != null) { Dimension prefSize = last.getPreferredSize(); Dimension maxSize = last.getMaximumSize(); if (prefSize != maxSize) { dim.width = dim.width - prefSize.width + maxSize.width; dim.height = Math.max(dim.height, maxSize.height); } } Insets insets = target.getInsets(); dim.width += insets.left + insets.right; dim.height += insets.top + insets.bottom; return dim; } }
public void setColumnWidths() { // See // "http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#custom" int n = getModel().getColumnCount(); for (int j = 0; j < n; ++j) { TableColumn column = getColumnModel().getColumn(j); TableCellRenderer headerRenderer = column.getHeaderRenderer(); if (headerRenderer == null) headerRenderer = getTableHeader().getDefaultRenderer(); // the new 1.3 way Component columnComponent = headerRenderer.getTableCellRendererComponent( this, column.getHeaderValue(), false, false, -1, j); Component cellComponent = getDefaultRenderer(getColumnClass(j)) .getTableCellRendererComponent(this, getModel().getValueAt(0, j), false, false, 0, j); int wantWidth = Math.max( columnComponent.getPreferredSize().width + 10, // fudge factor ... seems to always be too small otherwise (on x86 Linux) cellComponent.getPreferredSize().width + 10 // fudge factor ... seems to always be too small otherwise (on Mac OS X) ); column.setPreferredWidth(wantWidth); } }
/** Lays out the container in the specified panel. */ public void layoutContainer(Container target) { synchronized (target.getTreeLock()) { Insets insets = target.getInsets(); Dimension dim = target.getSize(); int fullHeight = dim.height; int bottom = dim.height - insets.bottom - 1; int top = 0 + insets.top; int left = insets.left; int maxPosition = dim.width - (insets.left + insets.right) - hgap; int right = dim.width - insets.right; maxPosition = right; int height = bottom - top; int size = actions.size(); int w, h; if ((grip != null) && grip.isVisible()) { Dimension d = grip.getPreferredSize(); grip.setBounds(left, top + vgap, d.width, bottom - top - 2 * vgap); left += d.width; left += hgap; } for (int i = 0; i < size; i++) { left += hgap; Component comp = (Component) actions.elementAt(i); Dimension d; Dimension minSize = comp.getMinimumSize(); if (i == size - 1) d = comp.getMaximumSize(); else d = comp.getPreferredSize(); w = d.width; h = Math.min(height, d.height); if ((left < maxPosition) && (left + w > maxPosition)) { if (maxPosition - left >= minSize.width) w = maxPosition - left; } comp.setBounds(left, (fullHeight - d.height) / 2, w, h); // if (i == size - 1) // d = comp.getMaximumSize(); // else // d = comp.getPreferredSize(); // if ((left + d.width) > right) // w = right - left; // else // w = d.width; // h = Math.min (height, d.height); // comp.setBounds (left, (fullHeight - d.height)/2, w, h); left += d.width; } } }
public void layoutContainer(Container parent) { Dimension size = parent.getSize(); Insets insets = parent.getInsets(); int itop = insets.top; int ileft = insets.left; int ibottom = insets.bottom; int iright = insets.right; int rightWidth = right.getPreferredSize().width; int bottomHeight = bottom.getPreferredSize().height; int centerWidth = size.width - rightWidth - ileft - iright; int centerHeight = size.height - bottomHeight - itop - ibottom; center.setBounds(ileft, itop, centerWidth, centerHeight); right.setBounds(ileft + centerWidth, itop, rightWidth, centerHeight); // Lay out all status components, in order Enumeration status = leftOfScrollBar.elements(); while (status.hasMoreElements()) { Component comp = (Component) status.nextElement(); Dimension dim = comp.getPreferredSize(); comp.setBounds(ileft, itop + centerHeight, dim.width, bottomHeight); ileft += dim.width; } bottom.setBounds( ileft, itop + centerHeight, size.width - rightWidth - ileft - iright, bottomHeight); }
@Override public Insets getBorderInsets(Component c) { Dimension size = comp.getPreferredSize(); Insets insets = border.getBorderInsets(c); insets.top = Math.max(insets.top, size.height); return insets; }
private void disposeAndUpdate(boolean update) { if (myView != null) { boolean visible = myView.isVisible(); myView.setVisible(false); Container container = myContent.getParent(); if (container != null) { container.remove(myContent); } if (myView instanceof Window) { myViewBounds = myView.getBounds(); Window window = (Window) myView; if (!push(UIUtil.getWindow(myOwner), window)) { window.dispose(); } } else { Container parent = myView.getParent(); if (parent == null) { myViewBounds = new Rectangle(myContent.getPreferredSize()); } else { myViewBounds = new Rectangle(myView.getBounds()); parent.remove(myView); Point point = new Point(myViewBounds.x, myViewBounds.y); SwingUtilities.convertPointToScreen(point, parent); myViewBounds.x = point.x; myViewBounds.y = point.y; } } myView = null; if (update && visible) { setVisible(true); } } }
private static Pair<Image, Point> createDragImage( final Tree tree, final Component c, Point dragOrigin, boolean adjustToPathUnderDragOrigin) { if (c instanceof JComponent) { ((JComponent) c).setOpaque(true); } c.setForeground(tree.getForeground()); c.setBackground(tree.getBackground()); c.setFont(tree.getFont()); c.setSize(c.getPreferredSize()); final BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = (Graphics2D) image.getGraphics(); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f)); c.paint(g2); g2.dispose(); Point point = new Point(-image.getWidth(null) / 2, -image.getHeight(null) / 2); if (adjustToPathUnderDragOrigin) { TreePath path = tree.getPathForLocation(dragOrigin.x, dragOrigin.y); if (path != null) { Rectangle bounds = tree.getPathBounds(path); point = new Point(bounds.x - dragOrigin.x, bounds.y - dragOrigin.y); } } return new Pair<Image, Point>(image, point); }
private int getHeaderHeight() { if ((header == null) || (header.getTable() == null)) { return 0; } int height = 0; boolean accomodatedDefault = false; TableColumnModel columnModel = header.getColumnModel(); for (int column = 0; column < columnModel.getColumnCount(); column++) { TableColumn aColumn = columnModel.getColumn(column); boolean isDefault = (aColumn.getHeaderRenderer() == null); if (!isDefault || !accomodatedDefault) { Component comp = getHeaderRenderer(column); int rendererHeight = comp.getPreferredSize().height; height = Math.max(height, rendererHeight); // Configuring the header renderer to calculate its preferred size // is expensive. Optimise this by assuming the default renderer // always has the same height as the first non-zero height that // it returns for a non-null/non-empty value. if (isDefault && rendererHeight > 0) { Object headerValue = aColumn.getHeaderValue(); if (headerValue != null) { headerValue = headerValue.toString(); if (headerValue != null && !headerValue.equals("")) { accomodatedDefault = true; } } } } } return height + 2; }
private static Point fixPopupLocation(final Component contents, final int x, final int y) { if (!(contents instanceof JToolTip)) return new Point(x, y); final PointerInfo info; try { info = MouseInfo.getPointerInfo(); } catch (InternalError e) { // http://www.jetbrains.net/jira/browse/IDEADEV-21390 // may happen under Mac OSX 10.5 return new Point(x, y); } int deltaY = 0; if (info != null) { final Point mouse = info.getLocation(); deltaY = mouse.y - y; } final Dimension size = contents.getPreferredSize(); final Rectangle rec = new Rectangle(new Point(x, y), size); ScreenUtil.moveRectangleToFitTheScreen(rec); if (rec.y < y) { rec.y += deltaY; } return rec.getLocation(); }
@Override public int getRowHeight() { if (myRowHeightIsComputing) { return super.getRowHeight(); } if (myRowHeight < 0) { try { myRowHeightIsComputing = true; for (int row = 0; row < getRowCount(); row++) { for (int column = 0; column < getColumnCount(); column++) { final TableCellRenderer renderer = getCellRenderer(row, column); if (renderer != null) { final Object value = getValueAt(row, column); final Component component = renderer.getTableCellRendererComponent(this, value, true, true, row, column); if (component != null) { final Dimension size = component.getPreferredSize(); myRowHeight = Math.max(size.height, myRowHeight); } } } } } finally { myRowHeightIsComputing = false; } } if (myMinRowHeight == null) { myMinRowHeight = getFontMetrics(UIManager.getFont("Label.font")).getHeight(); } return Math.max(myRowHeight, myMinRowHeight); }
private int getColumnHeaderWidth(JTable table, int colIndex) { TableColumn col = table.getColumnModel().getColumn(colIndex); TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) renderer = table.getTableHeader().getDefaultRenderer(); Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0); return comp.getPreferredSize().width; }
@Override public Dimension preferredLayoutSize(Container parent) { Component toolbar = parent.getComponent(0); Dimension toolbarSize = toolbar.isVisible() ? toolbar.getPreferredSize() : new Dimension(); Dimension contentSize = parent.getComponent(1).getPreferredSize(); return new Dimension( Math.max(toolbarSize.width, contentSize.width), toolbarSize.height + contentSize.height); }
public void doLayout() { if (view != null) { view.setSize(view.getPreferredSize()); visibleRect = getVisibleRect(); view.setLocation(visibleRect.x, visibleRect.y); } glassPane.setLocation(0, 0); glassPane.setSize(getWidth(), getHeight()); }
private int calcMaxContentColumnWidth(int columnIndex, int maxRowsToCheck) { int maxWidth = 0; for (int row = 0; row < maxRowsToCheck; row++) { TableCellRenderer renderer = getCellRenderer(row, columnIndex); Component comp = prepareRenderer(renderer, row, columnIndex); maxWidth = Math.max(comp.getPreferredSize().width, maxWidth); } return maxWidth + UIUtil.DEFAULT_HGAP; }
@Override public void layoutContainer(Container parent) { int width = parent.getWidth(); int height = parent.getHeight(); Component toolbar = parent.getComponent(0); Dimension toolbarSize = toolbar.isVisible() ? toolbar.getPreferredSize() : new Dimension(); toolbar.setBounds(0, 0, width, toolbarSize.height); parent.getComponent(1).setBounds(0, toolbarSize.height, width, height - toolbarSize.height); }
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { Insets borderInsets = border.getBorderInsets(c); Insets insets = getBorderInsets(c); int temp = (insets.top - borderInsets.top) / 2; border.paintBorder(c, g, x, y + temp, width, height - temp); Dimension size = comp.getPreferredSize(); rect = new Rectangle(offset, 0, size.width, size.height); SwingUtilities.paintComponent(g, comp, (Container) c, rect); }
/** * This method gets the width of a cell, specified by a column and row number. * * @param row The column number. * @param column The row number. * @return The width of the cell data. */ private int getCellDataWidth(int row, int column) { // Inovke the renderer for the cell to calculate the preferred width TableCellRenderer cellRenderer = table.getCellRenderer(row, column); Component c = table.prepareRenderer(cellRenderer, row, column); int width = c.getPreferredSize().width + table.getIntercellSpacing().width; return width; }
@Override public void doLayout() { int x = 0; for (int i = 0; i < getComponentCount(); i++) { final Component each = getComponent(i); final Dimension eachSize = each.getPreferredSize(); each.setBounds(x, 0, eachSize.width, getHeight()); x += each.getBounds().width; } }
protected void setResultsData() { getTable().setModel(getTableModel()); int maxScoreColWidth = 0; for (int x = 0; x < getTableModel().getColumnCount(); x++) { if (x != 1) { getTable() .getColumnModel() .getColumn(x) .setCellRenderer(new ResultsTableCellRenderer(false)); } TableColumn column = getTable().getColumnModel().getColumn(x); Component comp; column.setHeaderRenderer(new ResultsTableCellRenderer(true)); comp = column .getHeaderRenderer() .getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); int width = comp.getPreferredSize().width; for (int y = 0; y < getTableModel().getRowCount(); y++) { comp = getTable() .getDefaultRenderer(getTableModel().getColumnClass(x)) .getTableCellRendererComponent( getTable(), getTableModel().getValueAt(y, x), false, false, 0, x); if (comp.getPreferredSize().width > width) { width = comp.getPreferredSize().width; } } TableColumn col = getTable().getColumnModel().getColumn(x); col.setPreferredWidth(width); col.setMinWidth(width); col.setWidth(width); if (x >= 3 && width > maxScoreColWidth) { maxScoreColWidth = width; } } }
private static int columnMaxWidth(@NotNull JTable table, int col) { TableColumn column = table.getColumnModel().getColumn(col); int width = 0; for (int row = 0; row < table.getRowCount(); row++) { Component component = table.prepareRenderer(column.getCellRenderer(), row, col); int rendererWidth = component.getPreferredSize().width; width = Math.max(width, rendererWidth + table.getIntercellSpacing().width); } return width; }
// not used public Component add(Component c) { // System.out.println("Add " + c); if (c instanceof AbstractButton) { // System.out.println("Add to button group " + c); c.setVisible(true); bg.add((AbstractButton) c); } if (c instanceof Anchorable && ((Anchorable) c).isAnchored()) { c.setSize(c.getPreferredSize()); } return super.add(c); }
@Override public Dimension getPreferredSize() { Dimension size = new Dimension(); for (int i = 0; i < getComponentCount(); i++) { final Component each = getComponent(i); final Dimension eachSize = each.getPreferredSize(); size.width += eachSize.width; size.height = Math.max(size.height, eachSize.height); } return size; }
private int getColumnCellWidth(JTable table, int colIndex) { int width = 0; for (int r = 0; r < table.getRowCount(); r++) { TableCellRenderer renderer = table.getCellRenderer(r, colIndex); Component comp = renderer.getTableCellRendererComponent( table, table.getValueAt(r, colIndex), false, false, r, colIndex); width = Math.max(width, comp.getPreferredSize().width); } return width; }
/* * Calculated the width based on the column name */ private int getColumnHeaderWidth(int column) { if (!isColumnHeaderIncluded) return 0; TableColumn tableColumn = table.getColumnModel().getColumn(column); Object value = tableColumn.getHeaderValue(); TableCellRenderer renderer = tableColumn.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component c = renderer.getTableCellRendererComponent(table, value, false, false, -1, column); return c.getPreferredSize().width; }
/** * this creates a panel where the right component is its preferred size. This is useful for * putting on a button on the right and a text field on the left. */ public static JComponent createSideBySideComponent( Component leftComponent, Component rightComponent) { JPanel xLayoutPanel = new JPanel(); xLayoutPanel.setLayout(new BoxLayout(xLayoutPanel, BoxLayout.X_AXIS)); Dimension preferredSize = leftComponent.getPreferredSize(); leftComponent.setMaximumSize(new Dimension(Integer.MAX_VALUE, preferredSize.height)); xLayoutPanel.add(leftComponent); xLayoutPanel.add(Box.createHorizontalStrut(5)); xLayoutPanel.add(rightComponent); return xLayoutPanel; }
public void updateColumnSizes() { final JTableHeader header = getTableHeader(); final TableCellRenderer defaultRenderer = header == null ? null : header.getDefaultRenderer(); final RowSorter<? extends TableModel> sorter = getRowSorter(); final List<? extends RowSorter.SortKey> current = sorter == null ? null : sorter.getSortKeys(); ColumnInfo[] columns = getListTableModel().getColumnInfos(); for (int i = 0; i < columns.length; i++) { final ColumnInfo columnInfo = columns[i]; final TableColumn column = getColumnModel().getColumn(i); // hack to get sort arrow included into the renderer component if (sorter != null && columnInfo.isSortable()) { sorter.setSortKeys( Collections.singletonList(new RowSorter.SortKey(i, SortOrder.ASCENDING))); } final Component headerComponent = defaultRenderer == null ? null : defaultRenderer.getTableCellRendererComponent( this, column.getHeaderValue(), false, false, 0, 0); if (sorter != null && columnInfo.isSortable()) { sorter.setSortKeys(current); } final Dimension headerSize = headerComponent == null ? new Dimension(0, 0) : headerComponent.getPreferredSize(); final String maxStringValue; final String preferredValue; if (columnInfo.getWidth(this) > 0) { int width = columnInfo.getWidth(this); column.setMaxWidth(width); column.setPreferredWidth(width); column.setMinWidth(width); } else if ((maxStringValue = columnInfo.getMaxStringValue()) != null) { int width = getFontMetrics(getFont()).stringWidth(maxStringValue) + columnInfo.getAdditionalWidth(); width = Math.max(width, headerSize.width); column.setPreferredWidth(width); column.setMaxWidth(width); } else if ((preferredValue = columnInfo.getPreferredStringValue()) != null) { int width = getFontMetrics(getFont()).stringWidth(preferredValue) + columnInfo.getAdditionalWidth(); width = Math.max(width, headerSize.width); column.setPreferredWidth(width); } } }
/** * Get the dimension of this ColumnGroup. * * @param table the table the header is being rendered in * @return the dimension of the ColumnGroup */ public Dimension getSize(JTable table) { Component comp = renderer.getTableCellRendererComponent(table, getHeaderValue(), false, false, -1, -1); int height = comp.getPreferredSize().height; int width = 0; Iterator iter = v.iterator(); while (iter.hasNext()) { Object obj = iter.next(); if (obj instanceof TableColumn) { TableColumn aColumn = (TableColumn) obj; width += aColumn.getWidth(); } else { width += ((ColumnGroup) obj).getSize(table).width; } } return new Dimension(width, height); }
public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus) { ListCellRenderer renderer = comboBox.getRenderer(); Component c; Dimension d; c = renderer.getListCellRendererComponent( listBox, comboBox.getSelectedItem(), -1, false, false); c.setFont(comboBox.getFont()); if (comboBox.isEnabled()) { c.setForeground(comboBox.getForeground()); c.setBackground(comboBox.getBackground()); } else { c.setForeground(UIManager.getColor("ComboBox.disabledForeground")); c.setBackground(UIManager.getColor("ComboBox.disabledBackground")); } d = c.getPreferredSize(); currentValuePane.paintComponent(g, c, comboBox, bounds.x, bounds.y, bounds.width, d.height); }
@Override public void layoutContainer(final Container parent) { assert parent.getComponentCount() == 2; // 1. info; 2. progress Component infoPanel = parent.getComponent(0); Component progressPanel = parent.getComponent(1); int progressPrefWidth = progressPanel.getPreferredSize().width; final Dimension size = parent.getSize(); int maxProgressWidth = (int) (size.width * 0.8); int minProgressWidth = (int) (size.width * 0.5); if (progressPrefWidth > myProgressWidth) { myProgressWidth = progressPrefWidth; } if (myProgressWidth > maxProgressWidth) { myProgressWidth = maxProgressWidth; } if (myProgressWidth < minProgressWidth) { myProgressWidth = minProgressWidth; } infoPanel.setBounds(0, 0, size.width - myProgressWidth, size.height); progressPanel.setBounds(size.width - myProgressWidth, 0, myProgressWidth, size.height); }