Sterling Duchess
Sterling Duchess

Reputation: 2080

Java - JPanel & its contents how to set bounds

I have 3 JPanels on my JFrame. Anyhow the JPanels expand vertically and horizontally as the JFrame is being re-sized they are bound to JFrame.

I am wondering how do i set JScrollPane that is inside one of the JPanels to be bound to that JPanel so if that JPanel is being re-sized so is the JScrollPane inside it.

Upvotes: 2

Views: 5818

Answers (2)

Robin
Robin

Reputation: 36611

If you have a JPanel containing only one JComponent which should scale with the panel, one of the easiest layout manager options is the BorderLayout and adding the contents in the center

JPanel panel = new JPanel( new BorderLayout() );
JComponent componentToAdd = ...;
panel.add( componentToAdd, BorderLayout.CENTER );

There is a whole Swing tutorial on the different layout managers which describes in which situations the differentmanagers are most suited

Upvotes: 3

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

This would require you to give the JPanel a correct layout such as a BorderLayout, GridLayout, BoxLayout, or other depending on the need (but not JPanel's default layout -- FlowLayout -- which does not change the preferredSize of the components held).. If a BorderLayout, then add the JPanel BorderLayout.CENTER (or in another position again depending on need). If you are not familiar with the basic layouts, you will want to read through and study the tutorials which can be found here:

Lesson: Laying Out Components Within a Container

Upvotes: 2

Related Questions