Home Java Example Java Awt Common Component Methods
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Common Component Methods

Advertisement
The common methods of AWT components are as follow: 1 getLocation() - This method is used to get position of the component, as a Point.

Common Component Methods

     

The common methods of AWT components are as follow:

1 getLocation() - This method is used to get position of the component, as a Point. The usage of the method is shown below.

Point p = someComponent.getLocation(); 
int x = p.x; 
int y = p.y; 

One point to note here is that if you are working on Java 2 Platform then you need not to create a new point object. The x and y parts of the location can be easily accessed by using getX() and getY(). It is always efficient to use getX() and getY() methods. For example,

int x = someComponent.getX(); 
int y = someComponent.getY(); 

2. getLocationOnScreen() - This method is used to get the position of  the upper-left corner of the screen of the component, as a Point. The usage of the method is shown below.

Point p = someComponent.getLocationOnScreen(); 
int x = p.x; 
int y = p.y; 

It is always advisable to use getLocation(); method if you are working on Java 2 platform.

3. getBounds() - This method is used to get the current bounding Rectangle of component. The usage of the method is shown below.

Rectangle r = someComponent.getBounds(); 
int height = r.height; 
int width = r.width; 
int x = r.x; 
int y = r.y; 

One point to note here is that if you need a Rectangle object then the efficient way is to use getX(), getY(), getWidth(), and getHeight() methods while working on Java 2 platform.

4. getSize() - This method is used to get the current size of component, as a Dimension. The usage of the method is shown below.

Dimension d = someComponent.getSize(); 
int height = d.height; 
int width = d.width; 

Again, it is always advisable to use getWidth() and getHeight() methods to directly access the width and height while working on Java 2 platform. You can also use getSize() if you require a Dimension object. For Example,

int height = someComponent.getHeight(); 
int width = someComponent.getWidth(); 

5. setBackground(Color)/setForeground(Color) -
This method is used to change the background/foreground colors of the component.

6. setFont(Font) - This method is used to change the font of text within a component.

7. setVisible(boolean) - This method is used for the visibility state of the component. The component appears on the screen if setVisible() is set to true and if its set to false then the component will not appear on the screen. Furthermore, if we mark the component as not visible then the component will disappear while reserving its space in the GUI.

8. setEnabled(boolean) - This method is used to toggle the state of the component. The component will appear if set to true and it will also react to the user. ON the contrary, if set to false then the component will not appear hence no user interaction will be there.

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 13, 2008

Ask Questions?    Discuss: Common Component Methods  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com