Connecting Tech Pros Worldwide Forums | Help | Site Map

Swing Jlist LayoutOrientation Problem

Member
 
Join Date: Feb 2008
Posts: 89
#1: Oct 17 '09
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.
best answer - posted 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().

Member
 
Join Date: Nov 2007
Posts: 33
#2: Oct 18 '09

re: Swing Jlist LayoutOrientation Problem


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().
Member
 
Join Date: Feb 2008
Posts: 89
#3: Oct 18 '09

re: Swing Jlist LayoutOrientation Problem


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.
Reply