Example usage for java.awt Component getPreferredSize

List of usage examples for java.awt Component getPreferredSize

Introduction

In this page you can find the example usage for java.awt Component getPreferredSize.

Prototype

public Dimension getPreferredSize() 

Source Link

Document

Gets the preferred size of this component.

Usage

From source file:Main.java

/**
 * Returns minimum dimension combined from specified components dimensions.
 *
 * @param component1 first component//from  w w w  .j  av a  2 s . co  m
 * @param component2 second component
 * @return minimum dimension
 */
public static Dimension min(final Component component1, final Component component2) {
    return min(component1.getPreferredSize(), component2.getPreferredSize());
}

From source file:Main.java

public static boolean canHScroll(JViewport viewport) {
    JScrollPane scrollPane = (JScrollPane) viewport.getParent();
    Rectangle availR = scrollPane.getBounds();

    Component view = viewport.getView();
    Dimension viewPrefSize = view != null ? view.getPreferredSize() : new Dimension(0, 0);
    Dimension extentSize = viewport.toViewCoordinates(availR.getSize());

    boolean canHScroll = true;
    if (view instanceof Scrollable)
        canHScroll = !((Scrollable) view).getScrollableTracksViewportWidth();
    if (canHScroll && (viewPrefSize.width <= extentSize.width))
        canHScroll = false;// w ww .  j a  va  2  s. c  om

    return canHScroll;
}

From source file:Main.java

public static void sizeWidthToFitData(JTable table, int vc, int buf) {
    TableColumn tc = table.getColumnModel().getColumn(vc);

    int max = table.getTableHeader().getDefaultRenderer()
            .getTableCellRendererComponent(table, tc.getHeaderValue(), false, false, 0, vc)
            .getPreferredSize().width;/*  w  ww . j  a  v  a 2 s  .c  o m*/

    int vrows = table.getRowCount();
    for (int i = 0; i < vrows; i++) {
        TableCellRenderer r = table.getCellRenderer(i, vc);
        Object value = table.getValueAt(i, vc);
        Component c = r.getTableCellRendererComponent(table, value, false, false, i, vc);
        int w = c.getPreferredSize().width;
        if (max < w) {
            max = w;
        }
    }

    tc.setPreferredWidth(max + buf);
}

From source file:Main.java

/**
 * Returns true if the given point is within the actual bounds of the
 * JList item at index (not just inside the cell).
 *///from   w w  w. j  a v  a 2  s .  c om
private static boolean pointIsInActualBounds(JList list, int index, Point point) {
    ListCellRenderer renderer = list.getCellRenderer();
    ListModel dataModel = list.getModel();
    Object value = dataModel.getElementAt(index);
    Component item = renderer.getListCellRendererComponent(list, value, index, false, false);
    Dimension itemSize = item.getPreferredSize();
    Rectangle cellBounds = list.getCellBounds(index, index);
    if (!item.getComponentOrientation().isLeftToRight()) {
        cellBounds.x += (cellBounds.width - itemSize.width);
    }
    cellBounds.width = itemSize.width;

    return cellBounds.contains(point);
}

From source file:Main.java

public static void setSizes(Component comp, Dimension dim) {
    comp.setMinimumSize(dim);/*  w  w  w  .  j av a  2s.c om*/
    comp.setPreferredSize(dim);
    comp.setMaximumSize(new Dimension(Short.MAX_VALUE, (int) comp.getPreferredSize().getHeight()));
}

From source file:Main.java

/**
 * Returns maximum dimension combined from specified components dimensions.
 *
 * @param component1 first component/* ww w . ja  va2s .c o  m*/
 * @param component2 second component
 * @return maximum dimension
 */
public static Dimension max(final Component component1, final Component component2) {
    return max(component1.getPreferredSize(), component2.getPreferredSize());
}

From source file:Main.java

/**
 *
 * @param list/* w  w  w  .  j a  v  a  2s  .co m*/
 * @param index
 * @param point
 * @return
 */
public static boolean pointIsInActualBounds(JList list, int index, Point point) {
    ListCellRenderer renderer = list.getCellRenderer();
    ListModel dataModel = list.getModel();
    Object value = dataModel.getElementAt(index);
    Component item = renderer.getListCellRendererComponent(list, value, index, false, false);
    Dimension itemSize = item.getPreferredSize();
    Rectangle cellBounds = list.getCellBounds(index, index);

    if (!item.getComponentOrientation().isLeftToRight()) {
        cellBounds.x += (cellBounds.width - itemSize.width);
    }

    cellBounds.width = itemSize.width;

    return cellBounds.contains(point);
}

From source file:Main.java

/**
 * Returns maximum component width.//from ww  w .j  a  v  a 2 s  . c  o m
 *
 * @param components
 *            components to process
 * @return maximum component width
 */
public static int maxWidth(final Component... components) {
    int max = 0;
    for (final Component component : components) {
        max = Math.max(max, component.getPreferredSize().width);
    }
    return max;
}

From source file:Main.java

/**
 * Returns maximum component height./*from ww  w  .jav  a 2 s .  co m*/
 *
 * @param components
 *            components to process
 * @return maximum component height
 */
public static int maxHeight(final Component... components) {
    int max = 0;
    for (final Component component : components) {
        max = Math.max(max, component.getPreferredSize().height);
    }
    return max;
}

From source file:Main.java

/**
 *
 * @param table/*from  www. j  a v  a 2  s  . co  m*/
 * @param row
 * @param column
 * @param p
 * @return
 */
public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
    if ((table.convertColumnIndexToModel(column) != 0) || (row == -1)) {
        return true;
    }

    TableCellRenderer tcr = table.getCellRenderer(row, column);
    Object value = table.getValueAt(row, column);
    Component cell = tcr.getTableCellRendererComponent(table, value, false, false, row, column);
    Dimension itemSize = cell.getPreferredSize();
    Rectangle cellBounds = table.getCellRect(row, column, false);
    cellBounds.width = itemSize.width;
    cellBounds.height = itemSize.height;

    // See if coords are inside
    // ASSUME: mouse x,y will never be < cell's x,y
    assert ((p.x >= cellBounds.x) && (p.y >= cellBounds.y));

    if ((p.x > (cellBounds.x + cellBounds.width)) || (p.y > (cellBounds.y + cellBounds.height))) {
        return true;
    }

    return false;
}