问题:我有一个从解析的 ArrayList 创建列表的方法。我设法在 GUI 中显示列表,没有滚动条。但是,我无法将其设置为仅显示 ArrayList 的大小。这意味着,如果大小为 6,则显示的列表中应该只有 6 行。下面是我正在使用的代码。我尝试visibleRowCount
如下设置,但它不起作用。我尝试打印出结果,它表明进行了更改。
private void createSuggestionList(ArrayList<String> str) {
int visibleRowCount = str.size();
System.out.println("visibleRowCount " + visibleRowCount);
listForSuggestion = new JList(str.toArray());
listForSuggestion.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listForSuggestion.setSelectedIndex(0);
listForSuggestion.setVisibleRowCount(visibleRowCount);
System.out.println(listForSuggestion.getVisibleRowCount());
listScrollPane = new JScrollPane(listForSuggestion);
MouseListener mouseListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent mouseEvent) {
JList theList = (JList) mouseEvent.getSource();
if (mouseEvent.getClickCount() == 2) {
int index = theList.locationToIndex(mouseEvent.getPoint());
if (index >= 0) {
Object o = theList.getModel().getElementAt(index);
System.out.println("Double-clicked on: " + o.toString());
}
}
}
};
listForSuggestion.addMouseListener(mouseListener);
textPane.add(listScrollPane);
repaint();
}
总结一下:我希望 JList 显示的行数与解析后的 ArrayList 的大小一样多,而没有滚动条。
这是问题的图片:
这是其他 2 个的链接,因为图片分辨率很大,我担心它会扭曲视图:
JList 1和2图片清楚地显示了它。JList 显示空行,我不希望它发生。
有任何想法吗?请帮忙。谢谢。如果我没有正确表达我的问题,请让我知道是否需要问题图片。
--
编辑:
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(200, 200));
//Create the text area for the status log and configure it.
changeLog = new JTextArea(5, 30);
changeLog.setEditable(false);
JScrollPane scrollPaneForLog = new JScrollPane(changeLog);
//Create a split pane for the change log and the text area.
JSplitPane splitPane = new JSplitPane(
JSplitPane.VERTICAL_SPLIT,
scrollPane, scrollPaneForLog);
splitPane.setOneTouchExpandable(true);
//Create the status area.
JPanel statusPane = new JPanel(new GridLayout(1, 1));
CaretListenerLabel caretListenerLabel =
new CaretListenerLabel("Caret Status");
statusPane.add(caretListenerLabel);
//Add the components.
getContentPane().add(splitPane, BorderLayout.CENTER);
getContentPane().add(statusPane, BorderLayout.PAGE_END);
如果有帮助,如何将 textPane 包含到容器中
另一个编辑:
public void showSuggestionList(JScrollPane pane, Rectangle caretCoords) {
pane.setVisible(false);
pane.setBounds(caretCoords.x - 5, caretCoords.y + 25, 400, 250);
pane.setVisible(true);
repaint();
}
showSuggestionList() 被称为我的 CaretListener,以在插入符号移动时显示 JScrollPane。