0

I have a JPanel with a layout CardLayout and in every card there is another JPanel with a layout BorderLayout which has a JScrollPane inside.

ie.

JScrollPane jscrollPane = new JScrollPane(filter.getFilterComponent());
JPanel jPanel = new JPanel(new CardLayout());
jPanel.add(jscrollPane);

JPanel container = new JPanel(new BorderLayout());
container.add(jPanel, BorderLayout.CENTER );

filter.getFilterComponent() can be textbox, list of checkboxes or etc.

The problem is when it is a JTextField it look like this JTextField

which is undesirable behavior. When I use FlowLayout, no matter how tall the component is the scroll bar doesn't appear. I think its because its parent container preferredSize is 0

How can I prevent the component inside the JScrollPane from re sizing and keep its dimension to its original? Or there is another way of doing this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
newbie
  • 1,884
  • 7
  • 34
  • 55
  • 1
    I'd try using a `GridBagLayout`, if that fails, consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer May 28 '15 at 03:54
  • Alright i'll try `GridBagLayout` first. – newbie May 28 '15 at 04:00

1 Answers1

0

I used FlowLayout instead of BorderLayout

JPanel container = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
container.add(jPanel);

and set the preferred size of the JScrollPane

JScrollPane jscrollPane = new JScrollPane(filter.getFilterComponent());
jscrollPane.setPreferredSize(new Dimension(XX,YY));

XX and YY is the width and height of the parent container so if the content inside the scroll pane exceeds the preferred size, it will scroll

newbie
  • 1,884
  • 7
  • 34
  • 55
  • See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson May 28 '15 at 11:07