Java JComponent Size saveComponentSize(JComponent comp, Preferences prefs, String name)

Here you can find the source of saveComponentSize(JComponent comp, Preferences prefs, String name)

Description

Save the size of a JComponent to a package Preferences object.

License

Open Source License

Declaration

public static void saveComponentSize(JComponent comp, Preferences prefs, String name) 

Method Source Code

//package com.java2s;
/**/*from  w ww  .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;

import javax.swing.JViewport;

public class Main {
    /**
     * Save the size of a {@link JComponent} to a package Preferences object.
     * If the component is an instance of {@link JViewport} then the
     * visible size is stored.
     *
     * The Preference names are "name_width" and "name_height", where name is
     * some symbolic String (like MainComponent etc.). These names
     * are used when restoring by the symmetric method
     * {@link #setFrameLocation}.
     */
    public static void saveComponentSize(JComponent comp, Preferences prefs, String name) {
        Dimension dims = null;
        if (comp instanceof JViewport) {
            dims = ((JViewport) comp).getExtentSize();
        } else {
            dims = comp.getSize();
        }
        prefs.putInt(name + "_width", dims.width);
        prefs.putInt(name + "_height", dims.height);
    }
}

Related

  1. preferredSize(JComponent component)
  2. resize(JComponent component, Dimension size)
  3. resizeComponentWidth(JComponent currentComponent, double maxComponentWidth)
  4. restrictWindowMinimumSize(final Window wnd, final Dimension minSize)
  5. sameSize(JComponent[] components)
  6. setAllSizes(JComponent component, int width, int height)
  7. setComponentSize(int width, int height, JComponent l)
  8. setComponentSize(JComponent comp, int defWidth, int defHeight, Preferences prefs, String name)
  9. setComponentSize(JComponent comp, int width, int height)