• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

A scrollable JList?

 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I am a little confused about how I can make a JList scrollable. I understand that I should create a JScrollPane and add the JList to it, but there must be more because I cannot make this work correctly. When I use the scrollbar to scroll down, I do not see all of the data in the JList. There must be something more that I should do. Can someone please take a look at this sample code and tell me what I need to do to see all of the data without making the JList bigger? Many thanks to you!!!
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that what you may be doing wrong is the way you are adding items to your list. You need to add the list items to a DefaultListModel. For example:

also, vector is a bit of legacy code. You would be well to use an java.awt.List or an Array.
Hope that helps.

------------------
Happy Coding,
Gregg Bolinger
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry,
Your problem is in the following line:
list.setPreferredSize(dim);
What this tells the scrollPane is that its child is the same size as it is supposed to be. If you remove the line shown above the list will be sized according to its contents and all will be well.
Regards,
Manfred.
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I see! So, is there a problem with using a Vector for my data? Gregg seems to think so. What do others think? Gregg, what is the reason for this? Thanks!!!

Barry
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing "wrong" with using a vector. I was just told at one time that it was a bit of legacy code and ArrayList, and Lists have kind of taken the place of Vector.

------------------
Happy Coding,
Gregg Bolinger
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. Thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic