JList - 使用垂直滚动条而不是水平滚动条并具有垂直换行方向?
我试图将 JList
放置在 JScrollPane
内部,并让它按字母顺序列出垂直列中的条目,如下所示:
A D G
B E H
C F
但是,当 JList
由于空间不足,无法显示更多条目,我希望 JScrollPane
仅在垂直方向滚动。
当我使用VERTICAL_WRAP
时,这有效。但是,似乎当我使用垂直换行时,我会得到一个水平滚动条,而当我使用 HORIZONTAL_WRAP 时,我会得到我想要的滚动条,但项目的放置顺序是我不喜欢的。我可以一边吃蛋糕一边吃吗?这是我正在尝试做的事情的一个简单示例。
这是我能得到的最接近的结果,但我希望能够垂直滚动,同时保持垂直字母顺序。
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);
}
}
我想到的一种解决方案是: 如果单元格大小已知,我可以创建一个组件侦听器,并侦听调整大小事件。当该事件被触发时,我可以计算所需的行数以防止水平滚动。这看起来像是一个黑客,我不确定它如何与可变大小的文本组件一起工作。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你的解决方案很好,根本不是黑客。无论如何,任何内置功能都必须做基本相同的事情。
这是对您的示例的修改,可以满足您的要求。
I think your solution is just fine, and not a hack at all. Any built-in feature would have to do basically the same thing anyways.
Here's a modification to your example that does what you want.
这就是你想要的吗? (可能需要更改首选尺寸......)
Is this what you're going for? (Might need to change the preferred size...)
出色的代码@Kevin K...我建议进行一些小修改以避免ArithmeticException(除以零)
Brilliant Code @Kevin K... I suggest a small modification to avoid ArithmeticException (Divide by zero)