Java JComponent Size setComponentSize(JComponent comp, int defWidth, int defHeight, Preferences prefs, String name)

Here you can find the source of setComponentSize(JComponent comp, int defWidth, int defHeight, Preferences prefs, String name)

Description

Set the preferred size of a JComponent by restoring from package Preferences object.

License

Open Source License

Declaration

public static void setComponentSize(JComponent comp, int defWidth, int defHeight, Preferences prefs,
        String name) 

Method Source Code

//package com.java2s;
/**//from  ww w .j  a v  a  2  s.  c  om
 * Get a short description of the licensing terms.
 */

import java.awt.Dimension;

import java.util.prefs.Preferences;
import javax.swing.JComponent;

public class Main {
    /**
     * Set the preferred size of a {@link JComponent} by restoring from
     * package Preferences object.
     *
     * The Preference names are "name_width" and "name_height", where name is
     * some symbolic String (like PlotComponent, etc.). These names
     * are generated by the symmetric method {@link #saveComponentSize}.
     *
     * If the default width and height values are 0 and no values exist
     * already then the size is not set.
     */
    public static void setComponentSize(JComponent comp, int defWidth, int defHeight, Preferences prefs,
            String name) {
        if (defWidth == 0 || defHeight == 0) {

            // Just check one value for presence, but we really need both.
            String width = prefs.get(name + "_width", null);
            if (width == null) {
                return;
            }
        }
        int width = prefs.getInt(name + "_width", defWidth);
        int height = prefs.getInt(name + "_height", defHeight);
        comp.setPreferredSize(new Dimension(width, height));
    }
}

Related

  1. restrictWindowMinimumSize(final Window wnd, final Dimension minSize)
  2. sameSize(JComponent[] components)
  3. saveComponentSize(JComponent comp, Preferences prefs, String name)
  4. setAllSizes(JComponent component, int width, int height)
  5. setComponentSize(int width, int height, JComponent l)
  6. setComponentSize(JComponent comp, int width, int height)
  7. setFixedSize(JComponent component, Dimension size)
  8. setFixedSize(JComponent component, Dimension size)
  9. setFixedSize(JComponent component, int width, int height)