I'm trying to place a JList
inside of a JScrollPane
and have it alphabetically list the entries in vertical columns like this:
A D G
B E H
C F
However when the JList
runs out of space to display more entries, I'd like the JScrollPane
to scroll only in the vertical direction.
This works when I use VERTICAL_WRAP
. However, it seems like when I use vertical wrap I get a horizontal scrollbar and when I use HORIZONTAL_WRAP
I get the scrollbar I want, but the items get placed in an order that I don't like. Can I have my cake and eat it too? Here's a simple example of what I'm trying to do.
This is the closest I could get, but I'd like to be able to scroll vertically while maintaining the vertical alphabetical ordering.
public class ScrollListExample {
static List<String> stringList = new ArrayList<String>();
static {
for (int i = 0; i < 500; i++) {
stringList.add("test" + i);
}
}
public static void main(final String[] args) {
final JFrame frame = new JFrame();
final Container contentPane = frame.getContentPane();
final JList list = new JList(stringList.toArray());
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(0);
final JScrollPane scrollPane = new JScrollPane(list);
contentPane.add(scrollPane);
frame.setPreferredSize(new Dimension(800, 400));
frame.pack();
frame.setVisible(true);
}
}
One solution I've though of is: If the cell size is known I can create a component listener, and listen for a resize event. When that event is triggered I can calculate the desired row count in order to prevent horizontal scrolling. This just seems like a hack, and I'm not sure how it could work with variable sized text components.
VERTICAL_WRAP
is ` VERICAL_WRAP => Indicates "newspaper style" layout with the cells flowing vertically then horizontally.` SO, if cell will flow vertically and due to your dimension the output will get wrapped from bottom so than it will start filling horizontally.Therefor a horizontal scrollbar. – Roemervertical scrollbar
usingHORIZONTAL_WRAP
as you have mentioned.What is that order you need. – Roemer