0

I'm using Netbeans graphical JFrame design. So I have a jList inside a jScrollPane (from the designer) and during the program I add some long strings (file paths) to the list.

Some strings are too long to fit in the list's width so the jList expands horizontally to fit the longest item. However, I want the width to be fixed and have a horizontal scrollbar at the bottom.

How can I do this?

nc404
  • 135
  • 2
  • 8
  • 6
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jul 29 '15 at 00:29
  • I think this is a duplicate of [this](http://stackoverflow.com/a/19820525/3102124) – Kami Jul 29 '15 at 00:31

1 Answers1

3

You can use:

JList list = new JList(...);
list.setPrototypeCellValue("XXXXXXXXXXXXXXXXXXXX");
JScrollPane scrollPane = new JScrollPane( list );
panel.add( scrollPane );

to control the preferred width. The scrollbar will appear as required.

camickr
  • 321,443
  • 19
  • 166
  • 288