Re: [Layout] get position of component with getBounds()
A. Bolmarcich wrote:
On 2010-06-24, dmoyne <daniel.moyne@sfr.fr> wrote:
With AbsoluteLayout components are positionned in container (here jPanel)
with :
setBounds(x, y, width, height);
When using a layout like GridLayout when doing :
JScrollPane jScrollPane = new NewJScrollPaneJ();
jScrollPane.setPreferredSize(new Dimension(100,100));
jPanel.add(jScrollPane);
jPanel.validate();
System.out.println("INFO :" + jScrollPane.getBounds());
information supplied by getBounds() is all the time (0,0,0,0).
The bounds of jScrollPane are set by the layout manager of the container
it is
in. There is nothing in the code that tells AbsoluteLayout what it should
set
the bounds to. Chances are AbsoluteLayout is using the default bounds of
jScrollPane of (0,0,0,0).
If not mistaken when using GridLayout le layout manager selected calls
itself setBounds() to position the component jScrollPane in the container
(here jPanel).
GridLayout sets the bounds of the components in a container so they do
not overlap and each is approximately the same sizes. Different layout
managers use different methods to determine what to set the bounds of the
components in the container. See the documentation for the layout
manager that you are using to determine what you have to do so the layout
manager has the information it uses to set the bounds of the components.
I agree that when using a layout other than Absolute layout this nlayout is
doing the job to position itself the components within the container
following the rules attached to the sais layout.
The question here is : canwe in this case recover relative position to the
container of a component laid out ?
when using all methods about relative positioning of a component like
getX(), getY(), getSize() or getBounds() here, they all return 0 !
In the example attached code getBounds() is simply needed to scroll the main
container to a particular component with scrollRectToVisible() ; if I can
scroll by using another method that do not use relative positioning normally
not needed when using such a layout I will do, but which ?
Here is my code :
/*
Test of getBounds() when using a layout : here GridLayout
To use scrollRectToVisible() we need to supply a rectangle
to the main container ; getBounds() cannot be used here
as the informtion returned is all the time (0,0,0,0) for
unknown reasons
*/
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JTree;
import java.awt.Dimension;
public class ScrolledPane extends JFrame {
JScrollPane mainJScrollPane;
public ScrolledPane() {
super("JScrollPane Demonstration");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
setVisible(true);
}
public void init() {
// we set main container with GridLayout
JPanel mainJPanel = new JPanel();
GridLayout layout = new GridLayout(0, 1, 20, 10);
mainJPanel.setLayout(layout);
mainJPanel.setSize(600, 400);
// we add 10 jScrollPane containing tree
for (int i = 0; i < 10; i++) {
// we create a jScrollPane containing tree
JScrollPane jScrollPaneForJTree = new JScrollPane();
jScrollPaneForJTree.setName("jScrollPaneForJTree");
JTree jTree = new JTree();
jTree.setName("jTree");
jScrollPaneForJTree.setViewportView(jTree);
// we set height to avoid shrinking when reducing height of window
jScrollPaneForJTree.setPreferredSize(new Dimension(-1,100));
mainJPanel.add(jScrollPaneForJTree);
mainJPanel.validate();
// we display inofrmation of getBounds() for each jScrollPane
System.out.println("INFO :" + jScrollPaneForJTree.getBounds());
}
mainJScrollPane = new JScrollPane(mainJPanel);
getContentPane().add(mainJScrollPane, BorderLayout.CENTER);
// we try to scroll panel to 9th component
mainJPanel.scrollRectToVisible(mainJPanel.getComponent(8).getBounds());
}
public static void main(String args[]) {
new ScrolledPane();
}
}
Thanks.