Link to home
Start Free TrialLog in
Avatar of kinklaz
kinklaz

asked on

JScrollPane doesn't work on a JList?

It should, but I'm not getting it to..

I have subclassed the JList in order to make it fit into my BoxLayout.
therefore I have

CustomJList myJList = new CustomJList();

to enable JScrollPane..
JScrollPane myScroll = new JScrollPane(myJList);

I don't see scrollbars though! I have enough information to make the JList scroll but the scrollbar just simply wouldn't appear.

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

JScrollPane myScroll = new JScrollPane(myJList, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);

will show the scrollbars always, whether needed or not

JScrollPane myScroll = new JScrollPane(myJList, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);

should be

JScrollPane myScroll = new JScrollPane(myJList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

Avatar of kinklaz
kinklaz

ASKER

thanks CEHJ, but your code doesn't seem correct..
Avatar of kinklaz

ASKER

ok wait..
Avatar of kinklaz

ASKER

nope, doesn't work :(
ASKER CERTIFIED SOLUTION
Avatar of Ovi
Ovi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kinklaz

ASKER

aah let me try..
You should never use setPreferredSize()if you want to use JScrollPane(). The Ovi is correct!
> I have subclassed the JList in order to make it fit into my BoxLayout

why exactly??

> if you have overriden the method getPreferredSize() to
> achive your desired behaviour, you can kiss good bye your scrollPane

Not necessarily. This may be related to the problem, but just because getPreferredSize() is overriden doesn't mean you won't get scroll bars.

If overriding getPreferredSize() was the reason for subclassing, then this is the wrong approach as it is in fact the scroll pane that is being added to the box layout managed container and it's /min/max/preferred sizes that are used for layout.
Instead set the required sizes on your scroll pane:

JList myJList = new JList();
JScrollPane myScroll = new JScrollPane(myJList);
myScroll.setPreferredSize(..
myScroll.setMinimumSize(..
myScroll.setMaximumSize(..



1. "Not necessarily. This may be related to the problem, but just because getPreferredSize() is overriden doesn't mean you won't get scroll bars."

2. "If overriding getPreferredSize() was the reason for subclassing, then this is the wrong approach as it is in fact the scroll pane that is being added to the box layout managed container and it's /min/max/preferred sizes that are used for layout."

A little confusion here, don't you think 'objects'?  I see that finally you agree that getPreferredSize() is the wrong alternative, but the solution from my experience is not to set the size of the component to fit into something (only when it is absolutely necesary), generaly also a wrong alternative, but work with the LayoutManager constraints for achieve the desired effect. That's the scope of the layout, to lay down the managed components in a specific way, described by the layout itself.

kinklaz: So, if you cannot achieve the desired GUI design by using your BoxLayout you should switch to other layout manager, which is capable of representing the components in a way closer to yours. You should not change the default behaviour of your components. If ypu need assistance, you can post a little description of components used and how to lay them out.
> A little confusion here, don't you think 'objects'?  

Not really :)

> but the solution from my experience is not to set the size of the component to fit into something

There has been no mention of setting the size of anything.
That is what the layout managers job is.


Avatar of kinklaz

ASKER

sorry for being late..
I think the problem here is me using the BoxLayout.
Ovi's got a very good comment here..
*Did* you override getPreferredSize?
Avatar of kinklaz

ASKER

yes..