10

I have a panel with flow layout, and it can contain a variable number of items - from 1 to 2000. I want to put it inside a scroll pane, scrollable in vertical direction, and with fixed width. The problem is, when I set preferred size of panel to something like (800,600), some items are missing, and there is no scroll. If I set up preferred size of scroll pane, then all elements in flow pane are put on one very long line.

Setting maximum size on any element seems to do nothing at all - layout managers ignore it.

How can I fix this?

Rogach
  • 26,050
  • 21
  • 93
  • 172
  • 1
    `from 1 to really big numbers` is there some logics – mKorbel Oct 03 '11 at 09:34
  • Added some better numbers :) Forgive my bad english. – Rogach Oct 03 '11 at 09:35
  • 3
    do. not. use. setXXSize. Instead, use an appropriate LayoutManager – kleopatra Oct 03 '11 at 09:36
  • 2
    hmmm, lot of LayoutManagers ignore setXxxSize, your question is addept for http://sscce.org/, there are endless variations starts with nested layout to the GridBagLayout – mKorbel Oct 03 '11 at 09:39
  • 1
    @kleopatra - but when I really need some element be some specific size? – Rogach Oct 03 '11 at 09:41
  • 2
    @Rogach: I see that I'm not the only one struggling with preferredSizeMethod :) . Here's a previous question you may consider interesting: http://stackoverflow.com/questions/7229226/avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swing – Heisenbug Oct 03 '11 at 09:53
  • @kleopatra: I'm waiting your downvote on my answer :) . I'm joking but please correct me if I'm wrong. – Heisenbug Oct 03 '11 at 10:54
  • 1
    use a decent LayoutManager and tell the _manager_ which size/resize rules you want. BTW, why do you need a fixed size? That'll break down sooner or later (latest when you switch to a device with a different resolution) – kleopatra Oct 03 '11 at 11:13
  • You should also take a look at `Scrollable` interface and have your panel implement it: that will allow specific scrolling behavior. – jfpoilpret Oct 03 '11 at 12:34
  • *"tell the manager which size/resize rules you want"*. I wish I could give @kleopatra bounty on that comment. – Matthieu Feb 04 '19 at 10:23

2 Answers2

5

I want to put it inside a scroll pane, scrollable in vertical direction, and with fixed width

You can use the Wrap Layout for this.

Don't set the preferred size of the panel. But you can set the preferred size of the scroll pane so the frame.pack() method will work.

camickr
  • 321,443
  • 19
  • 166
  • 288
4

You could use BoxLayout to do this:

JPanel verticalPane = new JPanel();
verticalPane.setLayout(new BoxLayout(verticalPane, BoxLayout.Y_AXIS));

JScrollPane pane = new JScrollPane(verticalPane);

//add what you want to verticalPane
verticalPane.add(new JButton("foo"));
verticalPane.add(new JButton("bar"));

This of course will use the preferred size of each component added. If you want to modify the preferred size for example of a JPanel, extend it and override getPreferredSize:

class MyPanel extends JPanel(){
    public Dimension getPreferredSize(){
         return new Dimension(100,100);
    }
} 

A note: BoxLayout will take in consideration getPreferredSize, other LayoutManager may not.

Please criticize my answer, I'm not sure it's completely correct and I'm curious to hear objections in order to know if I understood the problem.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • 4
    'This of course will use the preferred size of each component added' of course is wrong (or not completely correct, depends a bit on what you mean by "use" ;-) LayoutManagers are free to do whatever they like with the hints given by the components. Overriding the getXXSize is the right direction to go, though hard-coded values are nearly as bad as setting them. Simply use a decent LayoutManager and configure it with whatever sizing rules you need. – kleopatra Oct 03 '11 at 11:10