Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

java - JList only show 9 items

in my app I use a JList with JScrollpane to show an action log. For this I do:

 logModel = new DefaultListModel();
        logModel.ensureCapacity(300);
        logList = new JList(logModel);
        logList.setPreferredSize(new Dimension(300, 150));
        logList.setVisibleRowCount(6);
        JScrollPane sp = new JScrollPane(logList);
        sp.setHorizontalScrollBar(null);
        GridBagConstraints gbc_logList = new GridBagConstraints();
        gbc_logList.anchor = GridBagConstraints.WEST;
        gbc_logList.gridx = 0;
        gbc_logList.gridy = 6;
        gbc_logList.weightx = 1.0;
        gbc_logList.weighty = 0.5;
        gbc_logList.gridwidth = 3;
        pane.add(sp, gbc_logList);

But If I start to add the items the list only show max. 9 items in the list. But the model reported the size in the right size (as sample 11) I add a sequence like

    private void createFolderSequence() {
        logModel.add(logModel.getSize(), "Create Folder: JNIFolder in root");
        int res = createFolder("JNIFolder", "/");
        logModel.add(logModel.getSize(), "Created: " + Integer.toString(res));
        
        logModel.add(logModel.getSize(), "Create Folder: JNIFolder2 in /JNIFolder/");
        res = createFolder("JNIFolder2", "/JNIFolder/");
        logModel.add(logModel.getSize(), "Created: " + Integer.toString(res));
        
        logModel.add(logModel.getSize(), "Rename Folder: JNIFolder2 to NIFolder3");
        res = renameFolder("/JNIFolder/JNIFolder2","JNIFolder3");
        logModel.add(logModel.getSize(), "Renamed: " + Integer.toString(res));
        
        logModel.add(logModel.getSize(), "Remove Folder: JNIFolder2");
        res = removeFolder("/JNIFolder/JNIFolder2");
        logModel.add(logModel.getSize(), "Removed: " + Integer.toString(res));
        System.out.println(logModel.getSize());
        
        logModel.add(logModel.getSize(), "Remove Folder: JNIFolder3");
        logModel.add(logModel.getSize(), "Remove Folder: JNIFolder3");
        res = removeFolder("/JNIFolder/JNIFolder3");
        logModel.add(logModel.getSize(), "Removed: " + Integer.toString(res));
        System.out.println(logModel.getSize()); //Print 11

    }

The console shows at last 11 but only 9 items are inside the list, also if I scroll down. Any Idea what I do wrong or what i missed?

question from:https://stackoverflow.com/questions/65559799/jlist-only-show-9-items

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
logList.setPreferredSize(new Dimension(300, 150));

Don't hardcode the preferred size. (Delete that statement).

You want the JList to dynamically calculate its preferred size as items are added/removed from the ListModel.

Scrollbars will then appear as required.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...