jList disable auto resize

时间:2015-07-29 00:26:24

标签: java swing netbeans jscrollpane jlist

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?

1 个答案:

答案 0 :(得分:2)

您可以使用:

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

控制首选宽度。滚动条将根据需要显示。