Swing Jlist LayoutOrientation Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gangreen
    New Member
    • Feb 2008
    • 98

    Swing Jlist LayoutOrientation Problem

    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:

    [CODE=java]private JComponent createListBox() {
    Object[] test = {"Thingy 1","Thingy 2",...};
    JList list = new JList(test);
    list.setLayoutO rientation(JLis t.VERTICAL_WRAP );
    list.setSelecti onMode(ListSele ctionModel.SING LE_SELECTION);
    list.setSize(38 5, 480);
    list.setPreferr edSize(new Dimension(385, 480));

    list.setVisible RowCount(-1);
    list.setPreferr edSize(new Dimension(385,4 80));
    JScrollPane listScroller = new JScrollPane(lis t);
    return listScroller;
    }[/CODE]

    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.
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    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 getPreferredScr ollableViewport Size() and the default value this has for a JList. It also describes how getPreferredScr ollableViewport Size() may be affected by other methods: in particular for JList it discusses the effect of setVisibleRowCo unt().

    Comment

    • Gangreen
      New Member
      • Feb 2008
      • 98

      #3
      Thank you, I read the javadoc for getPreferredScr ollableViewport Size(), and it helped. I don't have scrollbars but at least my window is visible. Displaying the scrollbars shouldn't be difficult.

      Comment

      Working...