Example usage for javax.swing JComponent getPreferredSize

List of usage examples for javax.swing JComponent getPreferredSize

Introduction

In this page you can find the example usage for javax.swing JComponent getPreferredSize.

Prototype

@Transient
public Dimension getPreferredSize() 

Source Link

Document

If the preferredSize has been set to a non-null value just returns it.

Usage

From source file:Main.java

/**
 * If icon is an instance of {@link ImageIcon}, 
 * returns an icon scaled to the height of the component.
 * Else return the icon given./*from  w ww .  jav  a 2  s .  co m*/
 */
public static Icon scaleToHeight(JComponent c, Icon icon) {

    Icon sizedIcon = icon;
    if (icon instanceof ImageIcon) {
        int h = c.getPreferredSize().height - c.getInsets().top - c.getInsets().bottom;
        sizedIcon = new ImageIcon(((ImageIcon) icon).getImage().getScaledInstance(-1, h, Image.SCALE_SMOOTH));
    }
    return sizedIcon;
}

From source file:Main.java

public static void setComponentSize(JComponent comp, int width, int height) {
    comp.setPreferredSize(new Dimension(width, height));
    comp.setMinimumSize(comp.getPreferredSize());
    comp.setMaximumSize(comp.getPreferredSize());
}

From source file:Main.java

/**
 * Freezes the width of the given component to the given size, preventing
 * it from expanding to fill any additional space.
 * @param component the component to freeze
 * @param width the width to freeze the component to
 * @return the component passed in, for chaining purposes
 *///from   w w w . j a  va  2 s .  c om
public static JComponent freezeWidth(JComponent component, int width) {
    component.setMinimumSize(new Dimension(width, component.getMinimumSize().height));
    component.setPreferredSize(new Dimension(width, component.getPreferredSize().height));
    component.setMaximumSize(new Dimension(width, component.getMaximumSize().height));
    return component;
}

From source file:BoxSample.java

private static void changeWidth(JComponent comp) {
    comp.setAlignmentX(Component.CENTER_ALIGNMENT);
    comp.setAlignmentY(Component.CENTER_ALIGNMENT);
    Dimension dim = comp.getPreferredSize();
    dim.width = Integer.MAX_VALUE;
    comp.setMaximumSize(dim);/*from  w w w  .  ja v  a2 s. c  o  m*/
}

From source file:BoxSample.java

private static void changeHeight(JComponent comp) {
    comp.setAlignmentX(Component.CENTER_ALIGNMENT);
    comp.setAlignmentY(Component.CENTER_ALIGNMENT);
    Dimension dim = comp.getPreferredSize();
    dim.height = Integer.MAX_VALUE;
    comp.setMaximumSize(dim);/* ww w. j a va  2  s . co  m*/
}

From source file:Main.java

/**
 * Fixes the width of the component but maintaining it old height.
 *
 * @param comp/*from  ww  w.j  ava  2s  .c  om*/
 * @param width
 */
public static void fixWidth(JComponent comp, int width) {
    comp.setPreferredSize(new Dimension(width, comp.getPreferredSize().height));
    comp.setMaximumSize(comp.getPreferredSize());
}

From source file:Main.java

public static void sizeUniformly(JComponent... components) {

    for (JComponent c : components) {
        c.setPreferredSize(null);//from   w w  w.j  a  va 2 s .  co m
        c.setMinimumSize(null);
        c.setMaximumSize(null);
    }

    int width = 0;
    int height = 0;

    for (JComponent c : components) {
        Dimension d = c.getPreferredSize();
        if (d.width > width) {
            width = d.width;
        }
        if (d.height > height) {
            height = d.height;
        }
    }

    for (JComponent c : components) {
        Dimension d = c.getPreferredSize();
        d.width = width;
        d.height = height;
        c.setPreferredSize(d);
        c.setMinimumSize(d);
        c.setMaximumSize(d);
    }
}

From source file:de.ailis.xadrian.utils.SwingUtils.java

/**
 * Sets the preferred height of the specified component.
 * //from w  w  w .  j  a  v a  2 s.c o  m
 * @param component
 *            The component
 * @param height
 *            The preferred height
 */
public static void setPreferredHeight(final JComponent component, final int height) {
    component.setPreferredSize(new Dimension(component.getPreferredSize().width, height));
}

From source file:de.ailis.xadrian.utils.SwingUtils.java

/**
 * Sets the preferred width of the specified component.
 * //from w w w  .  j ava  2 s .  c o  m
 * @param component
 *            The component
 * @param width
 *            The preferred width
 */
public static void setPreferredWidth(final JComponent component, final int width) {
    component.setPreferredSize(new Dimension(width, component.getPreferredSize().height));
}

From source file:events.ListDataEventDemo.java

/** 
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.//from  w w  w.  j a  v a2s.  c  o  m
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("ListDataEventDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JComponent newContentPane = new ListDataEventDemo();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Don't let the content pane get too small.
    //(Works if the Java look and feel provides
    //the window decorations.)
    newContentPane.setMinimumSize(new Dimension(newContentPane.getPreferredSize().width, 100));

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}