在我的应用程序中,我使用jlist和jscrollpane来显示操作日志。为此,我愿意:
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);
但如果我开始添加项目,列表中最多只显示9个项目。但是模型报告的大小是正确的(如示例11),我添加了一个序列
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
}
控制台显示最后11,但只有9个项目是在列表中,如果我向下滚动。你知道我做错了什么或错过了什么吗?
1条答案
按热度按时间nzkunb0c1#
不要硬编码首选尺寸(删除该语句)。
您希望jlist在从listmodel添加/删除项时动态计算其首选大小。
滚动条将根据需要显示。