Example usage for java.awt Component setPreferredSize

List of usage examples for java.awt Component setPreferredSize

Introduction

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

Prototype

public void setPreferredSize(Dimension preferredSize) 

Source Link

Document

Sets the preferred size of this component to a constant value.

Usage

From source file:Main.java

public static JPanel createKV(final Component key, final Component value, final int keyWidth,
        final boolean fill) {
    initComponentHeight(key, value);//  w  w  w.  java2 s . co  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

public static void setSize(final Component component, final int width, final int height) {
    if (component != null && width >= 0 && height >= 0) {
        runInEDT(() -> {//from w  w w.j  av a  2s  . c o  m
            component.setMaximumSize(new Dimension(width, height));
            component.setMinimumSize(new Dimension(width, height));
            component.setPreferredSize(new Dimension(width, height));
            component.setSize(new Dimension(width, height));
        });
    }
}

From source file:Main.java

/**
 * Makes all specified component sizes equal.
 *
 * @param components components to modify
 *///from   w  w  w. j a v a 2 s.c  o  m
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:com.clank.launcher.swing.SwingHelper.java

/**
 * Equalize the width of the given components.
 *
 * @param component component//from   ww  w. j  a va  2 s. c o m
 */
public static void equalWidth(Component... component) {
    double widest = 0;
    for (Component comp : component) {
        Dimension dim = comp.getPreferredSize();
        if (dim.getWidth() > widest) {
            widest = dim.getWidth();
        }
    }

    for (Component comp : component) {
        Dimension dim = comp.getPreferredSize();
        comp.setPreferredSize(new Dimension((int) widest, (int) dim.getHeight()));
    }
}

From source file:Main.java

public void addToToolbar(Component component, int row, int column) {
    Dimension d = component.getPreferredSize();
    component.setMaximumSize(d);//from w w w  . j a  va 2  s  . c om
    component.setMinimumSize(d);
    component.setPreferredSize(d);
    toolbar.add(component);

}

From source file:com.floreantpos.config.ui.TerminalConfigurationView.java

public void restartPOS() {
    JOptionPane optionPane = new JOptionPane(Messages.getString("TerminalConfigurationView.26"), //$NON-NLS-1$
            JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, Application.getApplicationIcon(),
            new String[] { /*Messages.getString("TerminalConfigurationView.28"),*/Messages
                    .getString("TerminalConfigurationView.30") }); //$NON-NLS-1$ //$NON-NLS-2$

    Object[] optionValues = optionPane.getComponents();
    for (Object object : optionValues) {
        if (object instanceof JPanel) {
            JPanel panel = (JPanel) object;
            Component[] components = panel.getComponents();

            for (Component component : components) {
                if (component instanceof JButton) {
                    component.setPreferredSize(new Dimension(100, 80));
                    JButton button = (JButton) component;
                    button.setPreferredSize(PosUIManager.getSize(100, 50));
                }/*from   ww  w .  j a va2 s . co  m*/
            }
        }
    }
    JDialog dialog = optionPane.createDialog(Application.getPosWindow(),
            Messages.getString("TerminalConfigurationView.31")); //$NON-NLS-1$
    dialog.setIconImage(Application.getApplicationIcon().getImage());
    dialog.setLocationRelativeTo(Application.getPosWindow());
    dialog.setVisible(true);
    Object selectedValue = (String) optionPane.getValue();
    if (selectedValue != null) {

        if (selectedValue.equals(Messages.getString("TerminalConfigurationView.28"))) { //$NON-NLS-1$
            try {
                Main.restart();
            } catch (IOException | InterruptedException | URISyntaxException e) {
            }
        } else {
        }
    }

}

From source file:org.jdal.swing.form.SimpleBoxFormBuilder.java

/**
 * Add a component to Form at position pointer by cursor, 
 * Increments cursor by one.//from ww w .  j  av a  2 s  .c om
 * @param c Component to add
 */
public void add(Component c) {
    if (debug) {
        if (c instanceof JComponent)
            ((JComponent) c).setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
    }
    addBox(c);

    if (rowHeight < Short.MAX_VALUE) {
        Dimension d = c.getPreferredSize();
        d.height = rowHeight;
        c.setPreferredSize(d);

        c.setMinimumSize(d);

        if (!c.isMaximumSizeSet() || c.getMaximumSize().getHeight() > rowHeight) {
            c.setMaximumSize(new Dimension(Short.MAX_VALUE, rowHeight));
        }
    }

}

From source file:com.moneydance.modules.features.importlist.table.HeaderRenderer.java

@Override
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected,
        final boolean hasFocus, final int row, final int column) {
    Component component = this.defaultHeaderTableCellRenderer.getTableCellRendererComponent(table, value,
            hasFocus, hasFocus, row, column);
    if (component instanceof JComponent) {
        JComponent jComponent = (JComponent) component;
        jComponent.setBorder(new EmptyBorder(1, 1, 1, 1));
        jComponent.setOpaque(false);//from w  w  w. ja  va  2s .  co  m

        if (jComponent instanceof JLabel) {
            JLabel jLabel = (JLabel) jComponent;
            jLabel.setHorizontalAlignment(SwingConstants.LEFT);
        }
    }

    component.setFont(Preferences.getHeaderFont());
    component.setForeground(Preferences.getHeaderForeground());

    component.setSize(
            new Dimension(component.getWidth(), Helper.INSTANCE.getPreferences().getHeaderRowHeight()));
    component.setMinimumSize(
            new Dimension(component.getWidth(), Helper.INSTANCE.getPreferences().getHeaderRowHeight()));
    component.setPreferredSize(
            new Dimension(component.getWidth(), Helper.INSTANCE.getPreferences().getHeaderRowHeight()));
    component.setMaximumSize(
            new Dimension(component.getWidth(), Helper.INSTANCE.getPreferences().getHeaderRowHeight()));

    return component;
}

From source file:org.openmicroscopy.shoola.util.ui.UIUtilities.java

/**
 * Sets the preferred size, minimum and maximum size of the specified 
 * component./* w  w  w. j av a  2s. c om*/
 * 
 * @param component   The component to handle.
 * @param dim      The dimension to set.
 */
public static void setDefaultSize(Component component, Dimension dim) {
    if (component == null)
        return;
    if (dim == null)
        dim = new Dimension(5, 5);
    component.setPreferredSize(dim);
    component.setMaximumSize(dim);
    component.setMinimumSize(dim);
}

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.ja v a 2s .c  o  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);
        }
    }

}