Question

I have a JFrame that uses the BorderLayout. How do I get all the components that are in the left or the right or in any other location of this JFrame?

Was it helpful?

Solution

As @brano suggested, you can access those components via the LayoutManager of your JFrame's contentPane.

For exemple, assuming that you want to get the component that is located on BorderLayout.WEST (and that your JFrame is called frame), you could do:

final BorderLayout layout = (BorderLayout) frame.getContentPane().getLayout();
final Component west = layout.getLayoutComponent(BorderLayout.WEST);

One remark, though: the getLayoutComponent method doesn't belong to LayoutManager: thus, you have to know and be sure that your layout is an actual BorderLayout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top