Leandro Lima
Leandro Lima

Reputation: 1170

Scrollable flow panel

I need to create a panel where I can put some rectangles and it automatically reorder just inserting a scrollbar and growing up vertically. Also this panel can be resizable and again the rectangles must to be reordered to correctly be displayed inside the panel.

Upvotes: 1

Views: 499

Answers (2)

camickr
camickr

Reputation: 324098

If I understand the question you want components to wrap to the next line so that the panel grows vertically while the width remains fixed.

If so then check out the WrapLayout

Note: the FlowLayout already supports the wrapping of components to a new row on the panel. This issue is that the preferred size calculation assumes all components are placed on a single row. The WrapLayout overrides the preferred size calculation to support the wrapping of components on a new row.

Upvotes: 3

Adamski
Adamski

Reputation: 54697

Use a JScrollPane. If you never want a horizontal scroll bar you can add the following:

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

(By default the scroll pane will add horizontal and vertical scroll bars when required.)

The scroll pane itself will only be resizeable if you add it to a Container with the appropriate layout manager; e.g.

JFrame frm = new JFrame();
frm.setLayout(new BorderLayout());
JScrollPane sp = new JScrollPane();
frm.add(sp, BorderLayout.CENTER); // Adding a component to the CENTER will cause the component to grow as the frame is resized.

Upvotes: 1

Related Questions