• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 607
  • Last Modified:

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.

0
kinklaz
Asked:
kinklaz
  • 6
  • 3
  • 2
  • +2
1 Solution
 
CEHJCommented:
JScrollPane myScroll = new JScrollPane(myJList, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);

will show the scrollbars always, whether needed or not

0
 
CEHJCommented:
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);

0
 
kinklazAuthor Commented:
thanks CEHJ, but your code doesn't seem correct..
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
kinklazAuthor Commented:
ok wait..
0
 
kinklazAuthor Commented:
nope, doesn't work :(
0
 
OviCommented:
"I have subclassed the JList in order to make it fit into my BoxLayout" - if you have overriden the method getPreferredSize() to achive your desired behaviour, you can kiss good bye your scrollPane :).
0
 
kinklazAuthor Commented:
aah let me try..
0
 
msterjevCommented:
You should never use setPreferredSize()if you want to use JScrollPane(). The Ovi is correct!
0
 
objectsCommented:
> 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(..



0
 
OviCommented:
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.
0
 
objectsCommented:
> 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.


0
 
kinklazAuthor Commented:
sorry for being late..
I think the problem here is me using the BoxLayout.
Ovi's got a very good comment here..
0
 
CEHJCommented:
*Did* you override getPreferredSize?
0
 
kinklazAuthor Commented:
yes..
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now