user1181445
user1181445

Reputation:

JScrollPane and sizing issues

I am having quite a problem in regards to a proper way to handle 'packing' a JPanel and allowing a scroll bar.

I left out a bit of the code, but I believe what is provided should suffice.

The issue, is that the JScrollBar either doesn't show up, or can't be interacted with (if I set the scroll bar to always have the vertical bar).

Here is an image depicting this:

![No Scroll Bar Present][1]

The ProjectPanel (extends JPanel) are of fixed size and, as you can see, extend farther than the visible view port. There is NO way of getting the calculated height of the JPanel (ProjectSelector), as the ProjectPanels can also be transitioned as so:

![They need to fill as a grid][2]

If anyone could help provide some insight on how to do this, that would be great. As of now, I would like at all costs not to use an external API, as that would cause more harm than good.

Question:

How can I set the height for the preferred size to be 'flexible', so as I add components it can expand? If that wouldn't be ideal / no possible, how could I properly allow the JScrollPane to show all components of the JPanel efficiently?

Upvotes: 1

Views: 284

Answers (2)

linski
linski

Reputation: 5094

If you are looking for a different layout, why don't you try MiGLayout? It is a very powerful and flexible layout manager.

See a detailed example here.

From that link, rewrite the initUI method to see the behavoir interesting to your problem:

public void initUI() {
    this.setPreferredSize(new Dimension(100, 100));
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setJMenuBar(initMenuBar());
    this.getContentPane().add(new JScrollPane(mainPanel = initMainPanel()));
    this.setLocationByPlatform(true);
    this.pack();        
}

Upvotes: 0

camickr
camickr

Reputation: 324118

if I do not, then the Flow Layout organizes them horizontally

Maybe you should be using a GridLayout.

If not then you can try the Wrap Layout which is a FlowLayout that wraps to a new line as required.

Upvotes: 2

Related Questions