有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JList是否使用垂直滚动条,而不是垂直环绕方向的水平滚动条?

我试着把一个JList放在一个JScrollPane里面,让它按字母顺序在垂直列中列出条目,如下所示:

A D G
B E H
C F 

但是,当JList的空间不够显示更多条目时,我希望JScrollPane只在垂直方向上滚动

当我使用VERTICAL_WRAP时,这就起作用了。然而,当我使用垂直包装时,我似乎得到了一个水平滚动条,当我使用HORIZONTAL_WRAP时,我得到了我想要的滚动条,但项目的排列顺序是我不喜欢的。我能把蛋糕也吃了吗?下面是一个简单的例子,说明我正在尝试做什么

enter image description here

这是我能得到的最接近的,但我希望能够在保持垂直字母顺序的同时垂直滚动

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);
    }
}

我想到的一个解决方案是: 如果单元格大小已知,我可以创建一个组件侦听器,并侦听调整大小事件。触发该事件时,我可以计算所需的行数,以防止水平滚动。这看起来像是一个黑客攻击,我不确定它如何与可变大小的文本组件一起工作


共 (2) 个答案

  1. # 1 楼答案

    精彩代码@Kevin K。。。我建议进行一个小的修改,以避免算术异常(除以零)

    private int computeVisibleColumnCount(JList list) 
        {
            int cellWidth = list.getCellBounds(0, 0).width;
            int width = list.getVisibleRect().width;
            return width == 0 ? 1 : width / cellWidth;
        }
    
  2. # 2 楼答案

    我认为你的解决方案很好,根本不是黑客攻击。不管怎样,任何内置功能都必须做基本相同的事情

    下面是对示例的一个修改,可以满足您的要求

    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);
            final JScrollPane scrollPane = new JScrollPane(list);
            contentPane.add(scrollPane);
            frame.setPreferredSize(new Dimension(800, 400));
            frame.pack();
    
            list.addComponentListener(new ComponentAdapter() {
                @Override
                public void componentResized(ComponentEvent e) {
                    fixRowCountForVisibleColumns(list);
                }
            });
    
            fixRowCountForVisibleColumns(list);
            frame.setVisible(true);
        }
    
        private static void fixRowCountForVisibleColumns(JList list) {
            int nCols = computeVisibleColumnCount(list);
            int nItems = list.getModel().getSize();
    
            // Compute the number of rows that will result in the desired number of
            // columns
            int nRows = nItems / nCols;
            if (nItems % nCols > 0) nRows++;
            list.setVisibleRowCount(nRows);
        }
    
        private static int computeVisibleColumnCount(JList list) {
            // It's assumed here that all cells have the same width. This method
            // could be modified if this assumption is false. If there was cell
            // padding, it would have to be accounted for here as well.
            int cellWidth = list.getCellBounds(0, 0).width;
            int width = list.getVisibleRect().width;
            return width / cellWidth;
        }
    }