Re: [JPanel child of JPanel]how to get a rectangle object with the dimensions of the JPanel object
Knute Johnson wrote:
Daniel Moyne wrote:
I have built a childJPanel child of another mainJPanel and I cannot get
its dimensions ; I tried :
Rectangle ee=childJPanel.getBounds();
childJPanel.getX();
childJPanel.getHeight();
but all the time I get 0 !
For the time being the layout of my mainJPanel is Absolute where I have
positioned my childJPanel with xpos, ypos, height and width but I do not
think that here it has no direct impact as the methods listed above shoud
work for any layouts ?
Thanks.
Can you see this childJPanel? My best guess is that it's size is zero.
import java.awt.*;
import java.awt.event.*;
public class test8 extends Frame {
public test8() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
});
Panel parent = new Panel(null);
parent.setBackground(Color.BLUE);
add(parent);
Panel child = new Panel();
child.setBackground(Color.YELLOW);
child.setBounds(50,50,50,50);
parent.add(child);
setSize(320,240);
setVisible(true);
System.out.println(child.getBounds());
}
public static void main(String[] args) {
new test8();
}
}
C:\Documents and Settings\Knute Johnson>java test8
java.awt.Rectangle[x=50,y=50,width=50,height=50]
Thanks Knute for this that works fine ; of course as you setBounds(...) you
get your parameters with getBounds() ; in my case I do not use setBounds()
and this was be the problem ; this is what I did ; I defined my childPanel
with a netneans class with a JPanelform like this :
Form ChildPanel
|_Other Components
|_JPanel
|_AbsoluteLayout
|_jScrollPane
|_jTree
at the end of my code I added this :
mainjPanel.add(jScrollPane, new
org.netbeans.lib.awtextra.AbsoluteConstraints(myXPosition, myYPosition,
JTREE_WIDTH, JTREE_HEIGHT));
with position and size parameters passed though my constructor.
I was wrongly expecting getBounds() applied to childPane to supply the
rectangle with (myXPosition, myYPosition, JTREE_WIDTH, JTREE_HEIGHT) but as
childPanel instance of ChildPanel had nothing to do with mainjPanel it did
not work.
Now if I add this as your example shows :
jScrollPaneMarriage.setBounds(myXPosition, myYPosition, JTREE_WIDTH,
JTREE_HEIGHT);
it works if I claim myRectangle like this :
childPanel.jScrollPane.getBounds();
Thanks as I was lost here.
Knute I seize this opportunity to ask you to put in this thread what you
posted on wrote on scrollRectToVisible(java.awt.Rectangle) that I want to
use in my app.
Thanks again.