简体   繁体   English

仅显示4条记录,并将滚动条添加到JList

[英]Display only 4 records and add a scroll bar to JList

I have added a JList, and made it to only display 4 records at a time. 我添加了一个JList,并使它一次仅显示4条记录。 If there are more than 4 records the user should be able to scroll and view other records. 如果有多于4条记录,则用户应该能够滚动和查看其他记录。 But in my case, i get to see all 8-10 records that i added. 但就我而言,我可以看到添加的所有8-10条记录。 The code is not showing the first 4 records and the scroll bar. 该代码未显示前4条记录和滚动条。 Can someone tell me what i'm missing ? 有人可以告诉我我想念的吗?

import java.awt.BorderLayout;

public class FrameTest {

    private JPanel panel;
    private JFrame frame;
    private FrameTest ft;
    private JList list;

    public FrameTest() {
        initComponents();
        ft = this;
    }

    private void initComponents() {
        frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

        panel = new JPanel();
        panel.setBorder(new EmptyBorder(5, 5, 5, 5));
        panel.setLayout(new BorderLayout(0, 0));

        frame.getContentPane().add(panel);

        list = new JList();
        list.setVisibleRowCount(4);
        list.setModel(new AbstractListModel() {
            String[] values = new String[] {"adf", "gr", "rg", "g", "tg", "gt", "tg", "tg", "t", "gt", "gt"};
            public int getSize() {
                return values.length;
            }
            public Object getElementAt(int index) {
                return values[index];
            }
        });
        list.setSelectedIndex(1);
        panel.add(list, BorderLayout.SOUTH);

        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);
    }


}

note: Above is a simplified version of my code. 注意:上面是我的代码的简化版本。

Try adding the JList to JScrollPane and adding the scroll pane to you frame. 尝试将JList添加到JScrollPane并将滚动窗格添加到框架。

    panel.add(new JScrollPane(list), BorderLayout.SOUTH);

The visibleRowCount is used by getPreferredScrollableViewportSize to determine the number of rows that should be displayed in a scroll pane getPreferredScrollableViewportSize使用visibleRowCount确定滚动窗格中应显示的行数

setVisibleRowCount() changes the size preferences of the JList, but the actual size is determined by which Layout Manager is laying out the JList. setVisibleRowCount()更改JList的大小首选项 ,但是实际大小由布局管理器对JList进行布局确定。 Some layouts take notice of the preferred sizes, others don't. 有些布局会注意首选尺寸,而有些则不会。

The documentation says: 该文件说:

For a VERTICAL layout orientation, this sets or gets the preferred number of rows to display without requiring scrolling. 对于“垂直”布局方向,这无需滚动即可设置或获取要显示的首选行数。 For the HORIZONTAL_WRAP or VERTICAL_WRAP layout orientations, it defines how the cells wrap. 对于HORIZONTAL_WRAP或VERTICAL_WRAP布局方向,它定义了单元格的包装方式。

More details in the JavaDoc . JavaDoc中有更多详细信息。

Use JScrollPane to achieve the scroll bar. 使用JScrollPane实现滚动条。

    JScrollPane scrollPane = new JScrollPane(list);
    panel.add(scrollPane, BorderLayout.SOUTH);

Details in the tutorial here: Using JScrollPane 教程中的详细信息在这里: 使用JScrollPane

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM