Example usage for java.awt Dimension Dimension

List of usage examples for java.awt Dimension Dimension

Introduction

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

Prototype

public Dimension(int width, int height) 

Source Link

Document

Constructs a Dimension and initializes it to the specified width and specified height.

Usage

From source file:Main.java

/**
 * Makes all specified component sizes equal.
 *
 * @param components components to modify
 *///from w w w  .j a  v  a  2s. com
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

public static JToggleButton createToggleButton(ImageIcon icon, int dimension, String tooltipText) {
    JToggleButton btn = new JToggleButton();
    btn.setToolTipText(tooltipText);//from ww  w .j a v  a  2 s.  c  om
    btn.setIcon(icon);

    btn.setMaximumSize(new Dimension(dimension, dimension));
    btn.setMinimumSize(new Dimension(dimension, dimension));
    btn.setPreferredSize(new Dimension(dimension, dimension));
    btn.setMargin(new Insets(0, 0, 0, 0));

    return btn;
}

From source file:Main.java

public Main() {
    setTitle("like JDialog");
    setSize(new Dimension(500, 100));
    setUndecorated(true);/*from   w w  w.  j  ava 2 s. c  o m*/
    setResizable(false);
    getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

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;/*from   w  ww  .  j a  v  a2s .com*/

    return canHScroll;
}

From source file:Main.java

/**
 * Displays a message dialog with given information.
 *///from w  ww .j a v a2  s.  c o  m
public static void showInformationDialog(Component component, String message) {
    final JPanel panel = new JPanel(new BorderLayout(5, 5));

    final JLabel messageLabel = new JLabel(message);
    messageLabel.setFont(new Font("Dialog", Font.BOLD, messageLabel.getFont().getSize()));

    panel.add(messageLabel, BorderLayout.CENTER);

    // Adjust stack trace dimensions
    final Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
    screenDimension.setSize(screenDimension.getWidth() * 0.7, screenDimension.getHeight() * 0.7);
    final Dimension maxStackTraceDimension = new Dimension(500, 500);
    maxStackTraceDimension.setSize(Math.min(maxStackTraceDimension.getWidth(), screenDimension.getWidth()),
            Math.min(maxStackTraceDimension.getHeight(), screenDimension.getHeight()));

    JOptionPane.showMessageDialog(component, panel, "Information", JOptionPane.INFORMATION_MESSAGE);
}

From source file:TableDemoApplet.java

private static void createGUI(Container contentPane) {
    Object[][] rowData = new String[][] { { "98-43", "AraAra! SL" }, { "81-31", "Aragones Transports SA" },
            { "12-72", "Rocca SL" }, { "99-10", "Rodriguez e Hijos SA" }, { "00-65", "Rimbau Motors SL" } };
    JTable table = new JTable(rowData, new String[] { "Part No", "Provider" });

    JComboBox companyComboBox = new JComboBox(new Object[] { "AraAra! SL", "Aragones Transports SA", "Rocca SL",
            "Rodriguez e Hijos SA", "Rimbau Motors SL" });
    companyComboBox.setEditable(true);//from  w  w  w  .  j av  a  2  s. com
    new S15WorkingBackspace(companyComboBox);

    // setup the ComboBoxCellEditor, DefaultCellEditor won't work!
    table.getColumnModel().getColumn(1).setCellEditor(new ComboBoxCellEditor(companyComboBox));

    table.setPreferredScrollableViewportSize(new Dimension(400, 100));
    JScrollPane scrollPane = new JScrollPane(table);

    contentPane.setLayout(new java.awt.FlowLayout());
    contentPane.add(scrollPane);
    contentPane.add(new JTextField("HALLO!"));
}

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  ww.  j  av a  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

/**
 * Aligns the first <code>rows</code> <code>cols</code> components of <code>parent</code> in a grid. Each component in
 * a column is as wide as the maximum preferred width of the components in that column; height is similarly determined
 * for each row. The parent is made just big enough to fit them all.
 * // ww w .  ja v  a2s . co m
 * @param rows
 *          number of rows
 * @param cols
 *          number of columns
 * @param initialX
 *          x location to start the grid at
 * @param initialY
 *          y location to start the grid at
 * @param xPad
 *          x padding between cells
 * @param yPad
 *          y padding between cells
 */
public static void makeCompactGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad,
        int yPad) {

    parent.setMaximumSize(new Dimension(Integer.MAX_VALUE, parent.getPreferredSize().height));
    SpringLayout layout;
    try {
        layout = (SpringLayout) parent.getLayout();
    } catch (ClassCastException exc) {
        System.err.println("The first argument to makeCompactGrid must use SpringLayout.");
        return;
    }

    // Align all cells in each column and make them the same width.
    Spring x = Spring.constant(initialX);
    for (int c = 0; c < cols; c++) {
        Spring width = Spring.constant(0);
        for (int r = 0; r < rows; r++) {
            width = Spring.max(width, getConstraintsForCell(r, c, parent, cols).getWidth());
        }
        for (int r = 0; r < rows; r++) {
            SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
            constraints.setX(x);
            constraints.setWidth(width);
        }
        x = Spring.sum(x, Spring.sum(width, Spring.constant(xPad)));
    }

    // Align all cells in each row and make them the same height.
    Spring y = Spring.constant(initialY);
    for (int r = 0; r < rows; r++) {
        Spring height = Spring.constant(0);
        for (int c = 0; c < cols; c++) {
            height = Spring.max(height, getConstraintsForCell(r, c, parent, cols).getHeight());
        }
        for (int c = 0; c < cols; c++) {
            SpringLayout.Constraints constraints = getConstraintsForCell(r, c, parent, cols);
            constraints.setY(y);
            constraints.setHeight(height);
        }
        y = Spring.sum(y, Spring.sum(height, Spring.constant(yPad)));
    }

    // Set the parent's size.
    SpringLayout.Constraints pCons = layout.getConstraints(parent);
    pCons.setConstraint(SpringLayout.SOUTH, y);
    pCons.setConstraint(SpringLayout.EAST, x);

}

From source file:Main.java

public Main() {
    setPreferredSize(new Dimension(320, 240));
    setBackground(hue.getColor());/*from   ww  w.  j  a  v a2  s .c  o m*/
    JComboBox colorBox = new JComboBox();
    for (Hue h : Hue.values()) {
        colorBox.addItem(h);
    }
    colorBox.addActionListener(e -> {
        Hue h = (Hue) colorBox.getSelectedItem();
        Main.this.setBackground(h.getColor());
    });
    this.add(colorBox);
}

From source file:Main.java

public static void setMaxSizePercent(final Component c, float widthPercent, float heightPercent) {
    final Dimension screenDimensions = Toolkit.getDefaultToolkit().getScreenSize();
    float fWidth = ((float) screenDimensions.width) * widthPercent;
    float fHeight = ((float) screenDimensions.height) * heightPercent;
    c.setMaximumSize(new Dimension((int) fWidth, (int) fHeight));
}