List of usage examples for java.awt Component getPreferredSize
public Dimension getPreferredSize()
From source file:EdgeLayoutExample.java
public void layoutContainer(Container parent) { synchronized (parent.getTreeLock()) { Insets insets = parent.getInsets(); int top = insets.top; int left = insets.left; Dimension minimumSize = minimumLayoutSize(parent); int height = minimumSize.height; int width = minimumSize.width; int availableHeight = parent.getHeight() - insets.bottom - insets.top; int availableWidth = parent.getWidth() - insets.left - insets.right; if (height < availableHeight) { height = availableHeight;//from w ww. ja va2s.c o m } if (width < availableWidth) { width = availableWidth; } int bottom = availableHeight; int right = availableWidth; Dimension preferredSize = preferredLayoutSize(parent); int preferredWidthAvailable = width - preferredSize.width; int preferredHeightAvailable = height - preferredSize.height; Component centerComp = null; for (int i = 0; i < this.components.size(); i++) { Component c = (Component) this.components.get(i); String constraint = (String) this.constraints.get(c); if (constraint.equals(CENTER)) { centerComp = c; } else { int compHeight; int compWidth; int xOrigin; int yOrigin; if (constraint.equals(NORTH) || constraint.equals(SOUTH)) { compWidth = width; if (preferredHeightAvailable > 0) { int preferredHeightNeeded = c.getPreferredSize().height - c.getMinimumSize().height; if (preferredHeightAvailable > preferredHeightNeeded) { compHeight = c.getPreferredSize().height; preferredHeightAvailable -= preferredHeightNeeded; } else { compHeight = c.getMinimumSize().height + preferredHeightAvailable; preferredHeightAvailable = 0; } } else { compHeight = c.getMinimumSize().height; } height = height - compHeight; xOrigin = left; if (constraint.equals(NORTH)) { yOrigin = top; top += compHeight; } else { yOrigin = bottom - compHeight; bottom = yOrigin; } } else { compHeight = height; if (preferredWidthAvailable > 0) { int preferredWidthNeeded = c.getPreferredSize().width - c.getMinimumSize().width; if (preferredWidthAvailable > preferredWidthNeeded) { compWidth = c.getPreferredSize().width; preferredWidthAvailable -= preferredWidthNeeded; } else { compWidth = c.getMinimumSize().width + preferredWidthAvailable; preferredWidthAvailable = 0; } } else { compWidth = c.getMinimumSize().width; } width = width - compWidth; yOrigin = top; if (constraint.equals(WEST)) { xOrigin = left; left += compWidth; } else { xOrigin = right - compWidth; right = xOrigin; } } c.setSize(compWidth, compHeight); c.setBounds(xOrigin, yOrigin, compWidth, compHeight); } if (centerComp != null) { c.setSize(width, height); c.setBounds(left, top, width, height); } } } }
From source file:modnlp.capte.AlignmentInterfaceWS.java
public JTable autoResizeColWidth(JTable table, DefaultTableModel model) { table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setModel(model);//from www . j a v a2 s. c o m int margin = 5; for (int i = 0; i < table.getColumnCount(); i++) { int vColIndex = i; 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); } ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()) .setHorizontalAlignment(SwingConstants.LEFT); // table.setAutoCreateRowSorter(true); table.getTableHeader().setReorderingAllowed(false); return table; }
From source file:logdruid.ui.RecordingList.java
private void initColumnSizes(JTable theTable) { MyTableModel2 model = (MyTableModel2) theTable.getModel(); TableColumn column = null;/*from w w w. j a va 2 s .c o m*/ Component comp = null; int headerWidth = 0; int cellWidth = 0; // Object[] longValues = model.longValues; TableCellRenderer headerRenderer = theTable.getTableHeader().getDefaultRenderer(); for (int i = 0; i < 4; i++) { column = theTable.getColumnModel().getColumn(i); comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); headerWidth = comp.getPreferredSize().width; cellWidth = comp.getPreferredSize().width; column.setPreferredWidth(Math.max(headerWidth, cellWidth)); } }
From source file:TableLayout.java
private void measureComponents(Container parent) { // set basic metrics such as ncomponents & nrows, and load the components // into the components[][] array. loadComponents(parent);/* w w w . j a v a 2 s .co m*/ // allocate new arrays to store row and column preferred and min sizes, but // only if the old arrays aren't big enough if (minWidth == null || minWidth.length < ncols) { minWidth = new int[ncols]; prefWidth = new int[ncols]; columnWidth = new int[ncols]; weight = new int[ncols]; } if (minHeight == null || minHeight.length < nrows) { minHeight = new int[nrows]; prefHeight = new int[nrows]; } int i; for (i = 0; i < ncols; ++i) { minWidth[i] = 0; prefWidth[i] = 0; } for (i = 0; i < nrows; ++i) { minHeight[i] = 0; prefHeight[i] = 0; } // measure the minimum and preferred size of each row and column for (int row = 0; row < nrows; ++row) { for (int col = 0; col < ncols; ++col) { Component comp = components[col][row]; if (comp != null) { TableOption option = (TableOption) options.get(comp); if (option == null) option = defaultOption; Dimension minSize = new Dimension(comp.getMinimumSize()); Dimension prefSize = new Dimension(comp.getPreferredSize()); // enforce prefSize>=minSize if (prefSize.width < minSize.width) prefSize.width = minSize.width; if (prefSize.height < minSize.height) prefSize.height = minSize.height; // divide size across all the rows or columns being spanned minSize.width /= option.colSpan; minSize.height /= option.rowSpan; prefSize.width = (prefSize.width - hgap * (option.colSpan - 1)) / option.colSpan; prefSize.height = (prefSize.height - vgap * (option.rowSpan - 1)) / option.rowSpan; for (int c = 0; c < option.colSpan; ++c) { if (minSize.width > minWidth[col + c]) minWidth[col + c] = minSize.width; if (prefSize.width > prefWidth[col + c]) prefWidth[col + c] = prefSize.width; } for (int r = 0; r < option.rowSpan; ++r) { if (minSize.height > minHeight[row + r]) minHeight[row + r] = minSize.height; if (prefSize.height > prefHeight[row + r]) prefHeight[row + r] = prefSize.height; } } } } // add rows and columns to give total min and preferred size of whole grid MinWidth = 0; MinHeight = 0; PrefWidth = hgap; PrefHeight = vgap; for (i = 0; i < ncols; ++i) { MinWidth += minWidth[i]; PrefWidth += prefWidth[i] + hgap; } for (i = 0; i < nrows; ++i) { MinHeight += minHeight[i]; PrefHeight += prefHeight[i] + vgap; } }
From source file:logdruid.ui.DateEditor.java
private void initColumnSizes(JTable theTable) { MyTableModel2 model = (MyTableModel2) theTable.getModel(); TableColumn column = null;/*from w w w .ja v a2s . co m*/ Component comp = null; int headerWidth = 0; int cellWidth = 0; // Object[] longValues = model.longValues; TableCellRenderer headerRenderer = theTable.getTableHeader().getDefaultRenderer(); for (int i = 0; i < 3; i++) { column = theTable.getColumnModel().getColumn(i); comp = headerRenderer.getTableCellRendererComponent(null, column.getHeaderValue(), false, false, 0, 0); headerWidth = comp.getPreferredSize().width; /* * comp = table.getDefaultRenderer(model.getColumnClass(i)). * getTableCellRendererComponent( table, longValues[i], false, * false, 0, i); */ cellWidth = comp.getPreferredSize().width; if (DEBUG) { logger.info("Initializing width of column " + i + ". " + "headerWidth = " + headerWidth + "; cellWidth = " + cellWidth); } column.setPreferredWidth(Math.max(headerWidth, cellWidth)); } }
From source file:TableLayout.java
/** * Returns a matrix of Dimension objects specifying the preferred sizes of the * components we are going to layout.// w w w . ja va 2 s .c om */ private Dimension[][] getPreferredSizes(Container parent) { int rowCount = rows.size(); Dimension[][] prefSizes = new Dimension[rowCount][columnCount]; for (int i = 0; i < rowCount; i++) { Component[] row = (Component[]) rows.elementAt(i); for (int j = 0; j < columnCount; j++) { Component component = row[j]; // Can only happen on the last line when all the remaining components are null as well if (component == null) break; if (component.getParent() != parent) throw new IllegalStateException("Bad parent specified"); prefSizes[i][j] = component.getPreferredSize(); } } return prefSizes; }
From source file:io.heming.accountbook.ui.MainFrame.java
public MainFrame() throws Exception { categoryFacade = new CategoryFacade(); recordFacade = new RecordFacade(); builder = new Pages.Builder(); builder.order("id", false); pages = builder.make();/*from w ww . j av a2 s . c om*/ date_chooser_visible = false; disable = false; // Create widgets model = new TableModel(); table = new JTable(model) { /** * tip. * * @param evt * @return */ @Override public String getToolTipText(MouseEvent evt) { String tip = null; java.awt.Point point = evt.getPoint(); int row = rowAtPoint(point); int col = columnAtPoint(point); Rectangle bounds = getCellRect(row, col, false); Component comp = prepareRenderer(getCellRenderer(row, col), row, col); try { if (comp.getPreferredSize().width > bounds.width) { tip = getValueAt(row, col).toString(); } } catch (RuntimeException e) { //catch null pointer exception if mouse is over an empty line } return tip; } }; if (pages.hasNext()) { model.setRecords(pages.next()); } // ? table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); TableColumnModel columnModel = table.getColumnModel(); columnModel.getColumn(0).setPreferredWidth(80); columnModel.getColumn(1).setPreferredWidth(160); columnModel.getColumn(2).setPreferredWidth(80); columnModel.getColumn(3).setPreferredWidth(100); columnModel.getColumn(4).setPreferredWidth(100); columnModel.getColumn(5).setPreferredWidth(360); // Layout JScrollPane pane = new JScrollPane(table); add(pane, BorderLayout.CENTER); table.getTableHeader().setReorderingAllowed(false); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // table header table.getTableHeader().addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { int col = table.columnAtPoint(e.getPoint()); String name = table.getColumnName(col); // TODO } }); // ? addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { quit(); } }); categories = categoryFacade.listBySale(); initMenuBar(); initToolBar(); initStatusBar(); initTablePopupMenu(); searchRecords(); setIconImage(new ImageIcon(getClass().getResource("64.png")).getImage()); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); setTitle(""); setSize(860, 620); }
From source file:edu.ku.brc.specify.tasks.subpane.ESResultsTablePanel.java
/** * @param table/*from www .jav a 2 s . c o 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.uci.ics.jung.visualization.PluggableRenderer.java
/** * Labels the specified vertex with the specified label. * Uses the font specified by this instance's * <code>VertexFontFunction</code>. (If the font is unspecified, the existing * font for the graphics context is used.) If vertex label centering * is active, the label is centered on the position of the vertex; otherwise * the label is offset slightly.// w ww. j a v a 2s . c o m */ protected void labelVertex(Graphics g, Vertex v, String label, int x, int y) { Component component = prepareRenderer(graphLabelRenderer, label, isPicked(v), v); Dimension d = component.getPreferredSize(); int h_offset; int v_offset; if (centerVertexLabel) { h_offset = -d.width / 2; v_offset = -d.height / 2; } else { Rectangle2D bounds = vertexShapeFunction.getShape(v).getBounds2D(); h_offset = (int) (bounds.getWidth() / 2) + 5; v_offset = (int) (bounds.getHeight() / 2) + 5 - d.height; } rendererPane.paintComponent(g, component, screenDevice, x + h_offset, y + v_offset, d.width, d.height, true); }
From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java
/** * Labels the specified non-self-loop edge with the specified label. * Uses the font specified by this instance's * <code>EdgeFontFunction</code>. (If the font is unspecified, the existing * font for the graphics context is used.) Positions the * label between the endpoints according to the coefficient returned * by this instance's edge label closeness function. *//*from w w w.j av a 2 s .com*/ protected void labelEdge(Graphics2D g2d, Edge e, String label, int x1, int x2, int y1, int y2) { int distX = x2 - x1; int distY = y2 - y1; double totalLength = Math.sqrt(distX * distX + distY * distY); double closeness = edgeLabelClosenessFunction.getNumber(e).doubleValue(); int posX = (int) (x1 + (closeness) * distX); int posY = (int) (y1 + (closeness) * distY); int xDisplacement = (int) (LABEL_OFFSET * (distY / totalLength)); int yDisplacement = (int) (LABEL_OFFSET * (-distX / totalLength)); Component component = prepareRenderer(graphLabelRenderer, label, isPicked(e), e); Dimension d = component.getPreferredSize(); Shape edgeShape = edgeShapeFunction.getShape(e); double parallelOffset = 1; parallelOffset += parallelEdgeIndexFunction.getIndex(e); if (edgeShape instanceof Ellipse2D) { parallelOffset += edgeShape.getBounds().getHeight(); parallelOffset = -parallelOffset; } parallelOffset *= d.height; AffineTransform old = g2d.getTransform(); AffineTransform xform = new AffineTransform(old); xform.translate(posX + xDisplacement, posY + yDisplacement); double dx = x2 - x1; double dy = y2 - y1; if (graphLabelRenderer.isRotateEdgeLabels()) { double theta = Math.atan2(dy, dx); if (dx < 0) { theta += Math.PI; } xform.rotate(theta); } if (dx < 0) { parallelOffset = -parallelOffset; } xform.translate(-d.width / 2, -(d.height / 2 - parallelOffset)); g2d.setTransform(xform); rendererPane.paintComponent(g2d, component, screenDevice, 0, 0, d.width, d.height, true); g2d.setTransform(old); }