views:

25

answers:

1

For some reason, I cannot this working. It should be simple really.

I've having a JFrame with a BorderLayout, which contains a JPanel (SOUTH) and a (CENTER) JPanel (itemPanel).

itemPanel should be wrapped in a scrollpane. Its width = x, and all of its children shares its length, so it basically works like a table with only 1 column.

For some reason, I cannot get the scrollpane to show the scrollbars (and scroll). In the JFrame:

setPreferredSize(dimension);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().add("South",controlPanel);
JScrollPane scroll = new JScrollPane(itemPanel);
scroll.setBorder(null);
getContentPane().add("Center",scroll);
super.pack();
setVisible(true);

Initilizing and adding some dummy-panels to the itemPanel:

itemPanel = new ItemPanel(); // A JPanel with a flowlayout
itemPanel.setPreferredSize(new Dimension(dimension.width,0));
for(int i = 0; i < 20; i++){
  JPanel p = new JPanel();
  p.setPreferredSize(new Dimension(0,50));
  p.setBackground(i%2 == 0 ? Color.GREEN : Color.YELLOW);
  itemPanel.add(p);
}

if omitting itemPanel.setPreferredSize(new Dimension(dimension.width,0)); the scrollpane shows the horizontal scrollbars, but since the flowlayout does not have a width to follow, it just shows the components in one row.

A: 

WrapLayout may be what you are looking for.

camickr
Wow, that was easy :) Thanks!While I dont understand the problem yet, this solved it.
Peter