List of usage examples for java.awt Component getPreferredSize
public Dimension getPreferredSize()
From source file:org.kineticsystem.commons.layout.TetrisLayout.java
/** * Lay out components in the specified container. * @param parent The container which needs to be laid out. *///from w w w . j av a 2s . c o m public void layoutContainer(Container parent) { /* * This method synchronizes on the tree lock of the component. This tree * lock is an object that can be used to provide thread-safe access to * the layout manager in case different threads are simultaneously * adding or removing components. The tree lock object is used as a * synchronization point for all of the methods associated with laying * out containers and invalidating components, and it is good * programming practice to use it to ensure a thread-safe * implementation. */ synchronized (parent.getTreeLock()) { // Layout components. if (containerSize == null) { setup(parent); } Insets insets = parent.getInsets(); int componentNumber = parent.getComponentCount(); if (componentNumber == 0) { return; } Dimension size = parent.getSize(); cGaps[0].setIncSize(cGaps[0].getSize()); for (int i = 1; i < cBars.length + 1; i++) { cGaps[i].setIncSize(cGaps[i].getSize() + cGaps[i - 1].getIncSize()); } rGaps[0].setIncSize(rGaps[0].getSize()); for (int i = 1; i < rBars.length + 1; i++) { rGaps[i].setIncSize(rGaps[i].getSize() + rGaps[i - 1].getIncSize()); } int actualGapFreeWidth = Math .max(size.width - insets.left - insets.right - cGaps[cBars.length].getIncSize(), 0); int actualGapFreeHeight = Math .max(size.height - insets.top - insets.bottom - rGaps[rBars.length].getIncSize(), 0); for (Map.Entry<Component, Cell> entry : constraintsMap.entrySet()) { Component comp = (Component) entry.getKey(); Cell cell = (Cell) entry.getValue(); if (cell != null) { // Actual cell position. int x0 = (int) Math.round(cSizes[cell.getCol()].evaluate(actualGapFreeWidth)); int y0 = (int) Math.round(rSizes[cell.getRow()].evaluate(actualGapFreeHeight)); int x1 = (int) Math.round(cSizes[cell.getCol() + cell.getCols()].evaluate(actualGapFreeWidth)); int y1 = (int) Math.round(rSizes[cell.getRow() + cell.getRows()].evaluate(actualGapFreeHeight)); // Actual cell dimension. int w = x1 - x0; int h = y1 - y0; // Component position correction. int xCorrection = insets.left + cGaps[cell.getCol()].getIncSize(); int yCorrection = insets.top + rGaps[cell.getRow()].getIncSize(); // Component dimension correction. int wCorrection = cGaps[cell.getCol() + cell.getCols() - 1].getIncSize() - cGaps[cell.getCol()].getIncSize(); int hCorrection = rGaps[cell.getRow() + cell.getRows() - 1].getIncSize() - rGaps[cell.getRow()].getIncSize(); // Preferred component dimension. int wComp = comp.getPreferredSize().width; int hComp = comp.getPreferredSize().height; int fill = cell.getFill(); int anchor = cell.getAnchor(); switch (fill) { case Cell.NONE: { if (wComp > w) { wComp = w; } if (hComp > h) { hComp = h; } switch (anchor) { case Cell.FIRST_LINE_START: x0 += xCorrection; y0 += yCorrection; break; case Cell.PAGE_START: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection; break; case Cell.FIRST_LINE_END: x0 += xCorrection + w + wCorrection - wComp; y0 += yCorrection; break; case Cell.LINE_START: x0 += xCorrection; y0 += yCorrection + (h - hComp) / 2; break; case Cell.CENTER: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection + (h - hComp) / 2; break; case Cell.LINE_END: x0 += xCorrection + w + wCorrection - wComp; y0 += yCorrection + (h - hComp) / 2; break; case Cell.LAST_LINE_START: x0 += xCorrection; y0 += yCorrection + h + hCorrection - hComp; break; case Cell.PAGE_END: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection + h + hCorrection - hComp; break; case Cell.LAST_LINE_END: x0 += xCorrection + w + wCorrection - wComp; y0 += yCorrection + h - hCorrection - hComp; break; } w = Math.min(w, wComp) + wCorrection; h = Math.min(h, hComp) + hCorrection; break; } case Cell.PROPORTIONAL: { double ratio = Math.min((double) ((double) w / (double) wComp), (double) ((double) h / (double) hComp)); wComp = (int) Math.round(wComp * ratio); hComp = (int) Math.round(hComp * ratio); switch (anchor) { case Cell.FIRST_LINE_START: x0 += xCorrection; y0 += yCorrection; break; case Cell.PAGE_START: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection; break; case Cell.FIRST_LINE_END: x0 += xCorrection + wCorrection + w - wComp; y0 += yCorrection; break; case Cell.LINE_START: x0 += xCorrection; y0 += yCorrection + (h - hComp) / 2; break; case Cell.CENTER: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection + (h - hComp) / 2; break; case Cell.LINE_END: x0 += xCorrection + wCorrection + w - wComp; y0 += yCorrection + (h - hComp) / 2; break; case Cell.LAST_LINE_START: x0 += xCorrection; y0 += yCorrection + hCorrection + h - hComp; break; case Cell.PAGE_END: x0 += xCorrection + (w - wComp) / 2; y0 += yCorrection + hCorrection + h - hComp; break; case Cell.LAST_LINE_END: x0 += xCorrection + wCorrection + w - wComp; y0 += yCorrection + hCorrection + h - hComp; break; } w = Math.min(w, wComp) + wCorrection; h = Math.min(h, hComp) + hCorrection; break; } case Cell.BOTH: { x0 += xCorrection; y0 += yCorrection; w += wCorrection; h += hCorrection; break; } case Cell.HORIZONTAL: { if (hComp > h) { hComp = h; } switch (anchor) { case Cell.FIRST_LINE_START: case Cell.PAGE_START: case Cell.FIRST_LINE_END: y0 += yCorrection; break; case Cell.LINE_START: case Cell.CENTER: case Cell.LINE_END: y0 += yCorrection + (h - hComp) / 2; break; case Cell.LAST_LINE_START: case Cell.PAGE_END: case Cell.LAST_LINE_END: y0 += yCorrection + h + hCorrection - hComp; break; } x0 += xCorrection; w += wCorrection; h = Math.min(h, hComp) + hCorrection; break; } case Cell.VERTICAL: { if (wComp > w) { wComp = w; } switch (anchor) { case Cell.FIRST_LINE_START: case Cell.LINE_START: case Cell.LAST_LINE_START: x0 += xCorrection; break; case Cell.PAGE_START: case Cell.CENTER: case Cell.PAGE_END: x0 += xCorrection + (w - wComp) / 2; break; case Cell.FIRST_LINE_END: case Cell.LINE_END: case Cell.LAST_LINE_END: x0 += xCorrection + w + wCorrection - wComp; break; } y0 += yCorrection; w = Math.min(w, wComp) + wCorrection; h += hCorrection; break; } } comp.setBounds(x0, y0, w, h); } } } }
From source file:org.lockss.devtools.plugindef.EDPInspectorTableModel.java
public void setColumnSize(JTable table, int col) { TableColumn column = null;//from w w w . j a v a 2 s . c o m Component comp = null; int headerWidth = 0; int cellWidth = 0; String longestStr = ""; String curString = ""; for (int row = 0; row < inspectorEntries.length; row++) { curString = data[row][col].toString(); if (curString.length() > longestStr.length()) { longestStr = curString; } } TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); column = table.getColumnModel().getColumn(col); comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); headerWidth = comp.getPreferredSize().width; comp = table.getDefaultRenderer(getColumnClass(col)).getTableCellRendererComponent(table, longestStr, false, false, 0, col); cellWidth = comp.getPreferredSize().width; column.setPreferredWidth(Math.max(headerWidth, cellWidth)); }
From source file:org.omegat.gui.align.AlignPanelController.java
private static void resizeRows(JTable table) { for (int row = 0; row < table.getRowCount(); row++) { int max = 0; for (int col = BeadTableModel.COL_SRC; col < table.getColumnCount(); col++) { int colWidth = table.getColumnModel().getColumn(col).getWidth(); TableCellRenderer cellRenderer = table.getCellRenderer(row, col); Component c = table.prepareRenderer(cellRenderer, row, col); c.setBounds(0, 0, colWidth, Integer.MAX_VALUE); int height = c.getPreferredSize().height; max = Math.max(max, height); }//from w w w.j a v a 2 s. c om table.setRowHeight(row, max); } }
From source file:org.opendatakit.briefcase.ui.FormTransferTable.java
public FormTransferTable(JButton btnSelectOrClearAllForms, FormStatus.TransferType transferType, JButton btnTransfer) {/* www . j a v a 2 s .c o m*/ super(new FormTransferTableModel(btnSelectOrClearAllForms, transferType, btnTransfer)); AnnotationProcessor.process(this);// if not using AOP // set the button column renderer to a custom renderer getColumn(getColumnName(FormTransferTableModel.BUTTON_COLUMN)).setCellRenderer(new JTableButtonRenderer()); addMouseListener(new JTableButtonMouseListener()); TableColumnModel columns = this.getColumnModel(); // determine width of "Selected" column header TableCellRenderer headerRenderer = this.getTableHeader().getDefaultRenderer(); Component comp = headerRenderer.getTableCellRendererComponent(null, columns.getColumn(0).getHeaderValue(), false, false, 0, 0); int headerWidth = comp.getPreferredSize().width; columns.getColumn(0).setMinWidth(headerWidth); columns.getColumn(0).setMaxWidth(headerWidth); columns.getColumn(0).setPreferredWidth(headerWidth); // create a detail button (that we'll throw away) // so we can get the needed column and row dimensions. comp = new DetailButton(null); int buttonWidth = comp.getPreferredSize().width; int buttonHeight = comp.getPreferredSize().height; columns.getColumn(FormTransferTableModel.BUTTON_COLUMN).setMinWidth(buttonWidth); columns.getColumn(FormTransferTableModel.BUTTON_COLUMN).setMaxWidth(buttonWidth); columns.getColumn(FormTransferTableModel.BUTTON_COLUMN).setPreferredWidth(buttonWidth); // set the row height to be big enough to include a button and have space above and below it setRowHeight(buttonHeight); // btn used is arbitrary... // and scale the others to be wider... columns.getColumn(1).setPreferredWidth(5 * headerWidth); columns.getColumn(2).setPreferredWidth(5 * headerWidth); this.setFillsViewportHeight(true); }
From source file:org.pentaho.ui.xul.swing.SwingElement.java
public void layout() { super.layout(); double totalFlex = 0.0; if (isVisible() == false) { resetContainer();/*from w w w . j a v a 2 s. co m*/ return; } for (Element comp : getChildNodes()) { // if (comp.getManagedObject() == null) { // continue; // } if (((XulComponent) comp).getFlex() > 0) { flexLayout = true; totalFlex += ((XulComponent) comp).getFlex(); } } double currentFlexTotal = 0.0; Align alignment = (getAlign() != null) ? Align.valueOf(this.getAlign().toUpperCase()) : null; for (int i = 0; i < getChildNodes().size(); i++) { XulComponent comp = (XulComponent) getChildNodes().get(i); gc.fill = GridBagConstraints.BOTH; if (comp instanceof XulSplitter) { JPanel prevContainer = container; container = new ScrollablePanel(new GridBagLayout()); container.setOpaque(false); final JSplitPane splitter = new JSplitPane( (this.getOrientation() == Orient.VERTICAL) ? JSplitPane.VERTICAL_SPLIT : JSplitPane.HORIZONTAL_SPLIT, prevContainer, container); splitter.setContinuousLayout(true); final double splitterSize = currentFlexTotal / totalFlex; splitter.setResizeWeight(splitterSize); if (totalFlex > 0) { splitter.addComponentListener(new ComponentListener() { public void componentHidden(ComponentEvent arg0) { } public void componentMoved(ComponentEvent arg0) { } public void componentShown(ComponentEvent arg0) { } public void componentResized(ComponentEvent arg0) { splitter.setDividerLocation(splitterSize); splitter.removeComponentListener(this); } }); } if (!flexLayout) { if (this.getOrientation() == Orient.VERTICAL) { // VBox and such gc.weighty = 1.0; } else { gc.weightx = 1.0; } prevContainer.add(Box.createGlue(), gc); } setManagedObject(splitter); } Object maybeComponent = comp.getManagedObject(); if (maybeComponent == null || !(maybeComponent instanceof Component)) { continue; } if (this.getOrientation() == Orient.VERTICAL) { // VBox and such gc.gridheight = comp.getFlex() + 1; gc.gridwidth = GridBagConstraints.REMAINDER; gc.weighty = (totalFlex == 0) ? 0 : (comp.getFlex() / totalFlex); } else { gc.gridwidth = comp.getFlex() + 1; gc.gridheight = GridBagConstraints.REMAINDER; gc.weightx = (totalFlex == 0) ? 0 : (comp.getFlex() / totalFlex); } currentFlexTotal += comp.getFlex(); if (this.getOrientation() == Orient.VERTICAL) { // VBox and such if (alignment != null) { gc.fill = GridBagConstraints.NONE; switch (alignment) { case START: gc.anchor = GridBagConstraints.WEST; break; case CENTER: gc.anchor = GridBagConstraints.CENTER; break; case END: gc.anchor = GridBagConstraints.EAST; break; } } } else { if (alignment != null) { gc.fill = GridBagConstraints.NONE; switch (alignment) { case START: gc.anchor = GridBagConstraints.NORTH; break; case CENTER: gc.anchor = GridBagConstraints.CENTER; break; case END: gc.anchor = GridBagConstraints.SOUTH; break; } } } Component component = (Component) maybeComponent; if (comp.getWidth() > 0 || comp.getHeight() > 0) { Dimension minSize = component.getMinimumSize(); Dimension prefSize = component.getPreferredSize(); if (comp.getWidth() > 0) { minSize.width = comp.getWidth(); prefSize.width = comp.getWidth(); } if (comp.getHeight() > 0) { minSize.height = comp.getHeight(); prefSize.height = comp.getHeight(); } component.setMinimumSize(minSize); component.setPreferredSize(prefSize); } container.add(component, gc); if (i + 1 == getChildNodes().size() && !flexLayout) { if (this.getOrientation() == Orient.VERTICAL) { // VBox and such gc.weighty = 1.0; } else { gc.weightx = 1.0; } container.add(Box.createGlue(), gc); } } }
From source file:org.pentaho.ui.xul.swing.tags.SwingGrid.java
@Override public void layout() { if (this.getChildNodes().size() < 2) { logger.warn("Grid does not contain Column and Row children"); return;/*from w ww . j a v a2 s . c om*/ } XulComponent columns = this.getChildNodes().get(0); XulComponent rows = this.getChildNodes().get(1); int colCount = 0; int rowCount = 0; float colFlexTotal = 0; float rowTotalFlex = 0; for (XulComponent col : columns.getChildNodes()) { if (col.getFlex() > 0) { colFlexTotal += col.getFlex(); } colCount++; } for (XulComponent row : rows.getChildNodes()) { if (row.getFlex() > 0) { rowTotalFlex += row.getFlex(); } rowCount++; } for (XulComponent row : rows.getChildNodes()) { gc.gridx = 0; for (XulComponent xulComp : row.getChildNodes()) { gc.weightx = 0.0; gc.gridwidth = 1; gc.gridheight = 1; gc.weighty = 0.0; gc.anchor = GridBagConstraints.NORTHWEST; gc.fill = GridBagConstraints.NONE; Component comp = (Component) xulComp.getManagedObject(); float colFlex = columns.getChildNodes().get(gc.gridx).getFlex(); int rowFlex = row.getFlex(); Align colAlignment = null; Align rowAlignment = null; String colAlignmentStr = xulComp.getAlign(); String rowAlignStr = row.getAlign(); if (colAlignmentStr != null) { colAlignment = Align.valueOf(colAlignmentStr); } if (rowAlignStr != null) { rowAlignment = Align.valueOf(rowAlignStr); } if (colFlex > 0) { gc.weightx = (colFlex / colFlexTotal); } if (rowFlex > 0) { gc.weighty = (rowFlex / rowTotalFlex); } if (colAlignment == Align.STRETCH && xulComp.getFlex() > 0) { gc.fill = GridBagConstraints.BOTH; } else if (colAlignment == Align.STRETCH) { gc.fill = GridBagConstraints.HORIZONTAL; } else if (xulComp.getFlex() > 0) { gc.fill = GridBagConstraints.VERTICAL; } if (row.getChildNodes().indexOf(xulComp) + 1 == row.getChildNodes().size()) { gc.gridwidth = GridBagConstraints.REMAINDER; } else { gc.gridwidth = 1; } if (rows.getChildNodes().indexOf(row) + 1 == rows.getChildNodes().size()) { gc.gridheight = GridBagConstraints.REMAINDER; } else { gc.gridheight = 1; } // gc.gridheight = row.getFlex() + 1; if (colAlignment != null && rowAlignment != null) { switch (rowAlignment) { case START: switch (colAlignment) { case START: gc.anchor = GridBagConstraints.NORTHWEST; break; case CENTER: gc.anchor = GridBagConstraints.NORTH; break; case END: gc.anchor = GridBagConstraints.NORTHEAST; break; } break; case CENTER: switch (colAlignment) { case START: gc.anchor = GridBagConstraints.WEST; break; case CENTER: gc.anchor = GridBagConstraints.CENTER; break; case END: gc.anchor = GridBagConstraints.EAST; break; } break; case END: switch (colAlignment) { case START: gc.anchor = GridBagConstraints.SOUTHWEST; break; case CENTER: gc.anchor = GridBagConstraints.SOUTH; break; case END: gc.anchor = GridBagConstraints.SOUTHEAST; break; } } } else if (rowAlignment != null) { switch (rowAlignment) { case START: gc.anchor = GridBagConstraints.NORTHWEST; break; case CENTER: gc.anchor = GridBagConstraints.WEST; break; case END: gc.anchor = GridBagConstraints.SOUTHWEST; break; } } else if (colAlignment != null) { switch (colAlignment) { case START: gc.anchor = GridBagConstraints.NORTHWEST; break; case CENTER: gc.anchor = GridBagConstraints.NORTH; break; case END: gc.anchor = GridBagConstraints.NORTHEAST; break; } } if (comp.getWidth() > 0 || comp.getHeight() > 0) { Dimension minSize = comp.getMinimumSize(); Dimension prefSize = comp.getPreferredSize(); if (comp.getWidth() > 0) { minSize.width = comp.getWidth(); prefSize.width = comp.getWidth(); } if (comp.getHeight() > 0) { minSize.height = comp.getHeight(); prefSize.height = comp.getHeight(); } comp.setMinimumSize(minSize); comp.setPreferredSize(prefSize); } else { comp.setPreferredSize(comp.getMinimumSize()); } grid.add(comp, gc); gc.gridx++; } gc.gridy++; } if (rowTotalFlex == 0) { // Add in an extra row at the bottom to push others up gc.gridy++; gc.weighty = 1; gc.fill = gc.REMAINDER; grid.add(Box.createGlue(), gc); } this.initialized = true; }
From source file:org.svv.acmate.gui.ACTestingPanel.java
private void resizeColumnWidth(JTable table) { final TableColumnModel columnModel = table.getColumnModel(); for (int column = 0; column < table.getColumnCount(); column++) { int width = 80; // Min width for (int row = 0; row < table.getRowCount(); row++) { TableCellRenderer renderer = table.getCellRenderer(row, column); Component comp = table.prepareRenderer(renderer, row, column); width = Math.max(comp.getPreferredSize().width + 1, width); }//from w ww . java 2 s .com columnModel.getColumn(column).setPreferredWidth(width); } }
From source file:org.tellervo.desktop.io.ImportDialog.java
@SuppressWarnings("unused") private static void updateRowHeights(JTable table) { try {/*ww w . j a v a 2s.c o m*/ for (int row = 0; row < table.getRowCount(); row++) { int rowHeight = table.getRowHeight(); for (int column = 0; column < table.getColumnCount(); column++) { Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column); rowHeight = Math.max(rowHeight, comp.getPreferredSize().height); } table.setRowHeight(row, rowHeight); } } catch (ClassCastException e) { } }
From source file:org.wandora.application.gui.OccurrenceTableSingleType.java
protected void updateRowHeights() { for (int row = 0; row < data.length; row++) { try {// w w w . j a v a 2 s. c o m int rowHeight = 16; if (defaultRowHeight == 0) { Component comp = this.prepareRenderer(this.getCellRenderer(row, 1), row, 1); rowHeight = Math.min(2000, Math.max(rowHeight, comp.getPreferredSize().height)); } else { rowHeight = rowHeight * defaultRowHeight + 6; } setRowHeight(row, rowHeight + 2); } catch (Exception e) { e.printStackTrace(); } } }
From source file:org.yccheok.jstock.gui.JTableUtilities.java
public static void makeTableColumnWidthFit(JTable jTable, int col, int margin, boolean locking) { // strategy - get max width for cells in column and // make that the preferred width TableColumnModel columnModel = jTable.getColumnModel(); int maxwidth = 0; for (int row = 0; row < jTable.getRowCount(); row++) { TableCellRenderer rend = jTable.getCellRenderer(row, col); Object value = jTable.getValueAt(row, col); Component comp = rend.getTableCellRendererComponent(jTable, value, false, false, row, col); maxwidth = Math.max(comp.getPreferredSize().width + margin, maxwidth); } // for row/*from www . j av a 2 s .c om*/ TableColumn column = columnModel.getColumn(col); TableCellRenderer headerRenderer = column.getHeaderRenderer(); if (headerRenderer == null) { headerRenderer = jTable.getTableHeader().getDefaultRenderer(); } Object headerValue = column.getHeaderValue(); Component headerComp = headerRenderer.getTableCellRendererComponent(jTable, headerValue, false, false, 0, col); maxwidth = Math.max(maxwidth, headerComp.getPreferredSize().width + margin); column.setPreferredWidth(maxwidth); if (locking) { // User will not able to adjust the width manually. column.setMinWidth(maxwidth); column.setMaxWidth(maxwidth); } }