Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have a problem with setVisible(true) !!!!!!

Status
Not open for further replies.

reza123

Programmer
Jan 1, 2003
2
DE
Hi!

Could you tell me please, how can I use the method
getSize() before I call the method setVisible(true)??

If I call the method getSize() before setVisible(true)
then return getSize() only 0 !!!!!
I must call getSize() before setVisible(true).

I tried to call the methods addNotify() and validate()
before I call the getSize() but without success!!

Thank you for your help ;-))

Code:

import java.awt.*;

class Test extends Frame
{
Test()
{
setSize(200, 100);
setLayout(new FlowLayout());
Button b=new Button("test");
add(b);
System.out.println("Size: "+ b.getSize()); //It doesn't work!!!!
setVisible(true);
}
public static void main(String args[])
{
(new Test());
}
}
 
From the Java Documentation -
public Dimension getSize()
Returns the size of this component in the form of a Dimension object. The height field of the Dimension object
contains this component's height, and the width field of the Dimension object contains this component's width.
Returns:
a Dimension object that indicates the size of this component



So I would suggest storing b.getSize() into a Dimension object and then using the methods available to the dimension object to find out the height and width.


As pointed out MANY times before, the Java Documentation is there for a reason, use it.
----------------------------------------
There are no onions, only magic
----------------------------------------
 
Perhaps you also need a call " pack ();" or a similar method before you call getSize. You have assigned the LayoutManager with the setLayout call, but it has not yet done its work because it is waiting whether you add further components. Maybe the size is set up after that call.
 
Hi!

I have a solution ;-)

We need further methods.
1. addNotify();
2. validate();

import java.awt.*;

public class WidthTest
{
public static void main(String args[])
{
Frame f = new Frame();

f.addNotify(); // You must call this method

f.setSize(200, 100);
f.setLayout(new FlowLayout());
Button b = new Button("test");

f.add(b);

f.validate(); // And you must call this method

System.out.println("Groesse: "+ b.getWidth());

f.setVisible(true);

}
}

greet Reza :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top