Results 1 to 10 of 10
  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default Two components in a JScrollPane

    Say I have two components in a panel in a scroll pane. The top one is a button and the one below is a JList. Now they can both scroll horizontally, but the JList won't scroll down vertically because the button is there too...

    Open iTunes or some music manager. You can scroll sideways to see more columns, but when you scroll vertically, the column title button things stay where they are.

    Please can someone help me to figure this out, otherwise I have a bit of a problem.

    Can I maybe add a vertical JScrollBar to only the JList and then a horizontal one to the panel containing the JList and the button? I've never used JScrollBars by themselves, but I'll have a look into that too.

  2. #2
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    I've just realised that I can probably put the JList into a vertical only JScrollPane and then the panel containing both into a horizontal only JScrollPane. Seems like the obvious thing to do. Don't know why I didn't think of that.

  3. #3
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Okay, it's difficult to explain this since my now access to internet at the moment is on a phone. There are actually a few JLists, like you would expect in a music player. Each one has a button at its top. I tried the idea that I posted in my previous post but it still didn't work because the five JLists were together in a JPanel. Which also meant that they weren't guaranteed to line up with their buttons above.

    So: Is it possible to make multiple JLists scroll together?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes it is. Each JScrollBar has a model. If all the lists are the same size, you can give each vertical scroll bar the same model.

  5. #5
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Can you please clarify a bit. JScrollBar has a setModel method that takes a BoundedRangeModel as a parameter... I don't know where to get one of those though... No method in JList to get one.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The scrolling is done by the jscrollpane and its jscrollbars, not the by the jlist, and in fact, the jlist has only limited knowledge about its scrolling. What I've done in the past is get the scroll bar of interest, here the vertical jscrollbar, extract its model via getModel, and then use this model to set the JScrollBar model of the sister jscrollPane's vertical jscrollbar. The lists must be the same size for this to work.

  7. #7
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Ok wow, thanks. That works perfectly.

    Last thing: now the vertical scrollbar disappears out of the side if I make the window smaller horizontally. I'm using GroupLayout to set out everything. The JLists (in their scrollpanes) are in a big JPanel, which is put with some titles and put in a horizontal scrollpane. So now when this horizontal scrollpane scrolls, the vertical scrollpane obviously disappears with everything else.

    Thanks for your help.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I avoid GroupLayout like the plague because it's hard to adjust in code. I'd advise you use different layouts, even to use nested JPanels with their own layouts.

  9. #9
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Hard to adjust in code? Why do you say that? I was initially using nested JPanels, each with different layout managers, but I always ran into problems with user resizing... I just tried GroupLayout the other day and it makes it so much easier to specify, for instance, which components must stay in line during resizing and things like that. Also makes it much easier to specify min, preferred and max sizes. It just makes so much sense.

    Anyways, regardless of the layout manager, do you have an idea how I might achieve what I'm trying to do now?
    Last edited by carderne; 09-09-2009 at 12:24 PM.

  10. #10
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Bump. Could I maybe set a JViewport for each JList and then only add the JScrollPane/JScrollBar afterwards, so that it is separate from the JLists in horizontal scrolling?

Similar Threads

  1. JScrollPane problem
    By KArelVH in forum AWT / Swing
    Replies: 6
    Last Post: 04-27-2009, 09:40 PM
  2. jscrollpane
    By kaemonsaionji in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 08:39 AM
  3. [SOLVED] JScrollPane - HELP!
    By terox13 in forum AWT / Swing
    Replies: 8
    Last Post: 05-10-2008, 03:58 AM
  4. jscrollpane problem
    By monkey04 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2008, 05:23 AM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 07:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •