By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,276 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,276 IT Pros & Developers. It's quick & easy.

Swing Jlist LayoutOrientation Problem

P: 98
Hi all,

I've bumped into an odd problem creating a GUI in swing. As you will see in the screenshots, I have a tabbed layout. Each tab is a Jpanel. On this panel I have put a JList, containing a series of objects. My intention is to have a single column of objects in the list, and have it to scroll when the objects exceed the vertical space.

My code is this:

Expand|Select|Wrap|Line Numbers
  1. private JComponent createListBox() {
  2.         Object[] test = {"Thingy 1","Thingy 2",...};
  3.         JList list = new JList(test);
  4.         list.setLayoutOrientation(JList.VERTICAL_WRAP);
  5.         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  6.         list.setSize(385, 480);
  7.         list.setPreferredSize(new Dimension(385, 480));
  8.  
  9.         list.setVisibleRowCount(-1);
  10.         list.setPreferredSize(new Dimension(385,480));
  11.         JScrollPane listScroller = new JScrollPane(list);
  12.         return listScroller;
  13.     }
I know it should not be VERTICAL_WRAP, do read on.

When I use VERTICAL_WRAP I get this:



It's more than one column, I agree, but atleast it works.
When I use VERTICAL (which should be the option for what I intend to do) I get this:



My JList is squashed. It appears it doesn't take the set sizes into account.

I have little experience in creating GUI's, and I haven't found a solution. I hope you can help me.

Thanks.

The layoutManger on the pannel is a FlowLayout btw.
Oct 17 '09 #1

✓ answered by pbrockway2

It might be useful to read the section in Sun's Tutorial on Sizing a Scroll Pane.

It describes the general mechanism for "scrolling savvy" clients like JList where size is determined by getPreferredScrollableViewportSize() and the default value this has for a JList. It also describes how getPreferredScrollableViewportSize() may be affected by other methods: in particular for JList it discusses the effect of setVisibleRowCount().

Share this Question
Share on Google+
2 Replies


Expert 100+
P: 151
It might be useful to read the section in Sun's Tutorial on Sizing a Scroll Pane.

It describes the general mechanism for "scrolling savvy" clients like JList where size is determined by getPreferredScrollableViewportSize() and the default value this has for a JList. It also describes how getPreferredScrollableViewportSize() may be affected by other methods: in particular for JList it discusses the effect of setVisibleRowCount().
Oct 17 '09 #2

P: 98
Thank you, I read the javadoc for getPreferredScrollableViewportSize(), and it helped. I don't have scrollbars but at least my window is visible. Displaying the scrollbars shouldn't be difficult.
Oct 18 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.