Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2005

Tip: Looking for answers? Try searching our database.

Method to do live resizing in a JFrame?

Thread view: 
Wes Harrison - 12 Jan 2005 11:38 GMT
I know there is a method that sets up live contents resizing in a JFrame so
that the components are resized as the window is resized but I can't find it
now.  Can someone help me find it?  I think it was new to JDK 1.4.0.

Thanks,

Wes
danman - 12 Jan 2005 17:06 GMT
What LayoutManager are you using?; I trust its not a "null"
Wes Harrison - 12 Jan 2005 23:10 GMT
I am using BorderLayout.  What I mean is that there is a way to turn on live
resizing so that the contents of the window are resized in real time as the
window's resize borders are being dragged but I can't remember what it is
and looking through the JFrame JavaDoc hasn't helped.

Wes

> What LayoutManager are you using?; I trust its not a "null"
Yanick - 13 Jan 2005 02:10 GMT
This is a layout manager I worked with in some project, it's a rework of
some layout manager I found at sun's tutorial. It uses the Java 1.5
implementations so there could be some work around (ie the Hashtable) to
make it 1.4 compatible.

-Yanick

// ***************************************************************
import java.awt.*;
import java.awt.geom.*;
import java.util.*;

public class PointLayout
   implements LayoutManager2 {

 private Dimension reference;                               //the reference
bounding box
 private Hashtable<Component,Rectangle2D.Double> compTable; //constraints
(Rectangles)
 private boolean scaling;

 /**
  * Default constructor: same as PointLayout( true );
  */
 public PointLayout() {
   this( true );
 }

 /**
  * Constructor: specify the scaling enabled/disabled
  * @param scale boolean true si les composants sont redimensionn?s
automatiquement
  */
 public PointLayout( boolean scale ) {
   compTable = new Hashtable<Component,Rectangle2D.Double>();
   reference = null;
   scaling = scale;
 }

 /**
  * Activate or deactivate the auto scaling of components
  * @param scale boolean true for enabled, false else
  */
 public void setAutoScale( boolean scale ) {
   scaling = scale;
 }

 /**
  * Return if the components are scaled when resizing
  * @return boolean true if components are scaled, false else
  */
 public boolean isAutoScaling() {
   return scaling;
 }

 /**
  * Adds the specified component to the layout, using the specified
  * constraint object.
  * @param comp the component to be added
  * @param constraints  where/how the component is added to the layout.
  */
 public void addLayoutComponent(Component comp, Object constraints) {
   if (constraints instanceof Rectangle) {
     Rectangle rect = (Rectangle) constraints;
     if (rect.width <= 0 || rect.height <= 0) {
       throw new IllegalArgumentException(
           "cannot add to layout: rectangle must have positive width and
height");
     }
     // ******* optional : prevent negative (not visible) components
**********
     //if (rect.x + rect.width < 0 || rect.y + rect.height < 0) {
     //  throw new IllegalArgumentException(
     //      "cannot add to layout: rectangle x and y must be >= 0");
     //}
     setConstraints(comp, rect);
   }
   else if (constraints != null) {
     throw new IllegalArgumentException(
         "cannot add to layout: constraint must be a Rectangle");
   }
   /**
   else {
     throw new IllegalArgumentException(
         "cannot add to layout: you must specify a constraint");

   }
   */
 }

 public void setConstraints(Component comp, Rectangle constraints) {
   compTable.put(comp, new Rectangle2D.Double(
     (double) constraints.x,
     (double) constraints.y,
     (double) constraints.width,
     (double) constraints.height));
 }

 /**
  * Adds the specified component with the specified name to
  * the layout.  This does nothing, since constraints are required.
  */
 public void addLayoutComponent(String name, Component comp) {
   throw new Exception( "Method not allowed; constraints required" );
 }

 /**
  * Removes the specified component from the layout.
  * @param comp the component to be removed
  */
 public void removeLayoutComponent(Component comp) {
   compTable.remove(comp);
 }

 /**
  * Lays out the container in the specified container.
  * @param parent the component which needs to be laid out
  */
 public void layoutContainer(Container parent) {
   synchronized (parent.getTreeLock()) {
     if (parent.getSize().getWidth() > 0 && parent.getSize().getHeight() >
0) {

       if ( scaling ) {     // if scaling is ON

         Insets insets = parent.getInsets();

         int ncomponents = parent.getComponentCount();

         if (ncomponents == 0) {
           return;
         }

         double xfactor = 1.0d;
         double yfactor = 1.0d;
         if (reference != null) {
           xfactor = parent.getWidth() / reference.getWidth();
           yfactor = parent.getHeight() / reference.getHeight();
         }

         Rectangle2D.Double rect;
         Component c;
         for (int i = 0; i < ncomponents; i++) {
           c = parent.getComponent(i);
           rect = compTable.get(c);
           if (rect != null) {
             rect.setRect( (rect.getX() * xfactor),
                          (rect.getY() * yfactor),
                          (rect.getWidth() * xfactor),
                          (rect.getHeight() * yfactor));
             c.setBounds(insets.left + (int) rect.getX(),
                         insets.top + (int) rect.getY(),
                         (int) rect.getWidth(), (int) rect.getHeight());
           }
         }
       }
       reference = parent.getSize(); // get the parent size

     }
   }
 }

 /**
  * Calculates the minimum size dimensions for the specified
  * panel given the components in the specified parent container.
  * @param parent the component to be laid out
  * @see #preferredLayoutSize
  */
 public Dimension minimumLayoutSize(Container parent) {
   return getLayoutSize(parent);
 }

 public Dimension preferredLayoutSize(Container parent) {
   return getLayoutSize(parent);
 }

 /**
  * Returns the maximum size of this component.
  * @see java.awt.Component#getMinimumSize()
  * @see java.awt.Component#getPreferredSize()
  * @see LayoutManager
  */
 public Dimension maximumLayoutSize(Container target) {
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   return new Dimension(screenSize.width, screenSize.height);
 }

 /**
  * Algorithm for calculating layout size (minimum or actual).
  *
  * @param parent the container in which to do the layout.
  * @return the dimensions to lay out the subcomponents of the specified
  *         container.
  */
 protected Dimension getLayoutSize(Container parent) {
   int ncomponents = parent.getComponentCount();
   Dimension minSize = parent.getSize();
   for (int i = 0; i < ncomponents; i++) {
     Component c = parent.getComponent(i);
     Rectangle2D.Double rect = (Rectangle2D.Double) compTable.get(c);
     if ((c != null) && (rect != null)) {
       minSize.width = Math.max(minSize.width, (int) (rect.getX() +
rect.getWidth()));
       minSize.height = Math.max(minSize.height, (int) (rect.y +
rect.getHeight()));
     }
   }
   return minSize;
 }

 /**
  * Returns the alignment along the x axis.  This specifies how
  * the component would like to be aligned relative to other
  * components.  The value should be a number between 0 and 1
  * where 0 represents alignment along the origin, 1 is aligned
  * the furthest away from the origin, 0.5 is centered, etc.
  */
 public float getLayoutAlignmentX(Container target) {
   return 0f;
 }

 /**
  * Returns the alignment along the y axis.  This specifies how
  * the component would like to be aligned relative to other
  * components.  The value should be a number between 0 and 1
  * where 0 represents alignment along the origin, 1 is aligned
  * the furthest away from the origin, 0.5 is centered, etc.
  */
 public float getLayoutAlignmentY(Container target) {
   return 0f;
 }

 /**
  * Invalidates the layout, indicating that if the layout manager
  * has cached information it should be discarded.
  */
 public void invalidateLayout(Container target) {
   // Do nothing
 }

}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.