Java Swing O Reilly Unlike the AWT Component
Java Swing O Reilly Unlike the AWT Component class, the getBounds() accessor in JComponent can take a pre- instantiated Rectangle object, as shown below: Rectangle myRect = new Rectangle(); myRect = component.getBounds(myRect); If a Rectangle is supplied, the getBounds() method alters each of the fields in the passed-in Rectangle to reflect the component’s current size and position, returning a copy of it. If the reference passed in is a null, the method instantiates a new Rectangle object, sets its values, and returns it. You can use the former approach to conserve memory if there are several calls to getBounds(). The setBounds() method resets the component’s size and position. This method also takes a Rectangle object. If the new settings are a change from the previous settings, the component is moved, typically resized, and invalidated. If the component has a parent, it is invalidated as well. Be warned that various layout managers may override any changes you attempt to make to the bounds property. Invalidating a component with a call to setBounds() may force the layout manager to recompute and reset the bounds of the component in relation to the other components resolving it to the same size as before. Here is a short example that shows how to retrieve the current position and size of any Swing component: JFrame frame = new JFrame(”Test Frame”); frame.setBounds(20,20,200,200); frame.setVisible(true); Rectangle r = new Rectangle(); r = frame.getBounds(r); System.out.println(”X = ” + r.x()); System.out.println(”Y = ” + r.y()); System.out.println(”Width = ” + r.width()); System.out.println(”Height = ” + r.height()); There is a shorthand approach for retrieving each of the bounds properties. JComponent contains four methods that directly access them: getX() , getY(), getWidth(), and getHeight(). You can use these accessors directly instead of instantiating a Rectangle object on the heap with a call to getBounds(). Consequently, you can replace the last six lines with the following four: - 60 -
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.