I'm trying to create a horizontal JList with seven items inside a JScrollPane and only three of those items need to be visible and the others accessible via a scrollbar. Here's an example of what I'm trying to do:
This is what I tried to do:
JFrame frame = new JFrame("JList Example");
String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10"};
JList<String> list = new JList<>(items);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(1);
JScrollPane scrollPane = new JScrollPane(list);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scrollPane);
And this is what I got:
So I can't find a way to limit the number of visible items other than one way to setPreferredSize of the JScrollPane. Which is not a very good way as described here, and it doesn't really work for me. Is there another way to do what I want?
P.S. I'm not good at English, so sorry if you misunderstood what I want. Trying my best


I just want to point out the obvious that "only three of those items need to be visible" is problematic, as the first three values might be larger or smaller then the following items or with different data you'll get different, and sometimes, weird results.
Do we make use of a "prototype" value, the model's first element or try and get the first
nitems from the model to calculate the "desirable viewable width"? What happens when the model contains less thennelements or no elements at all?The point is, there's a "lot" to think about. If you just want to reduce the horizontal size of the component, you could just override the
getPreferredScrollableViewportSizeand return a "desirable" width based on you needs, but just remember that this not likely to bencolumns.The following example basically demonstrates this concept (as best as I can without trying to figure out each and every possible edge case).
It extends
JListand overrides thegetPreferredScrollableViewportSizemethod.visibleColumnCountis 0 or less, it will operate as normalfixedCellWidthis defined, this is multiplied byvisibleColumnCountprototypeCellValueis set, the rendered values width is multiplied byvisibleColumnCount256(just pick a number)visibleColumnCount, it sums the preferred width of each element, averages the amount and fills in the remaining columns with the average ... if that sounds confusing, you should have tried coming up with itncolumns and calculate their preferred widthsFor example...
I should not that the JavaDocs talk about "horizontal insets" but I can't find anywhere that it's accessible/available, so I made a guess. I suspect that this is probably a UI delegate option, so if you really want to make use of this example, I'd encourage you to experiment with it.
Now, having said all this, just override the
getPreferredScrollableViewportSizemethod and return awidthwhich otherwise meets your desirable result, messing about with "visible columns" is ambiguous and problematic