問題: 解析された 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();
}
要約すると、スクロールバーなしで、解析された ArrayList のサイズと同じ数の行をJList に表示する必要があります。
問題の写真は次のとおりです。
画像の解像度が非常に大きいため、他の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 を表示します。