8

問題: 解析された 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 に表示する必要があります。

問題の写真は次のとおりです。

jリスト1

画像の解像度が非常に大きいため、他の2つへのリンクを次に示します。ビューが歪むのではないかと心配しています。

JList 1 & 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 を表示します。

4

2 に答える 2

2

問題はレイアウト管理だとtextPane思います。私が見るlistForSuggestions限り、優先サイズが尊重されていれば、これらのアイテムを表示するのに必要な以上のスペースを占めるべきではありません。

したがって、JTextPaneは です。つまりContainer、サブコンポーネントを追加できます。しかし、これらのサブコンポーネントはどのように配置されているのでしょうか? それは、現在使用されているレイアウト マネージャー次第です。レイアウト マネージャーが の推奨サイズを尊重する場合は、問題listForSuggestiosないと思います。よくわかりません。

私が見るJTextPane限り、をインスタンス化するだけで「null-layout」を取得できます。つまり、別のレイアウト マネージャーを明示的に設定しない限り、サブコンポーネントの配置/サイズ変更を自分で処理する必要があります。

あなたは次のようなことを試みることができます

Dimension dim = listForSuggestions.getPreferredSize();
listForSuggestions.setBounds(xPos, yPos, dim.getWidth(), dim.getHeight());

ここに完全な例があります

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;


public class FrameTest {

    public static void main(String[] args) {
        JFrame f = new JFrame("Frame Test");


        ArrayList<String> str = new ArrayList<String>();
        for (int i = 0; i < 20; i++)
            str.add("number " + i);

        JTextPane tp = new JTextPane();


        int visibleRowCount = str.size();
        System.out.println("visibleRowCount " + visibleRowCount);
        JList listForSuggestion = new JList(str.toArray());
        listForSuggestion.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listForSuggestion.setSelectedIndex(0);
        listForSuggestion.setVisibleRowCount(5);
        System.out.println(listForSuggestion.getVisibleRowCount());
        JScrollPane 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.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
        listForSuggestion.addMouseListener(mouseListener);
        Dimension dim = listForSuggestion.getPreferredSize();
        listForSuggestion.setBounds(20, 20, (int) dim.getWidth(), (int) dim.getHeight());

        tp.add(listForSuggestion);

        f.add(tp);
        f.setSize(400, 400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

    }
}

これを行う最もエレガントな方法は、独自のレイアウトマネージャーをロールバックすることだと思います。(実際には非常に単純です。) そして、 を実行する代わりにtextPane.add(list)、 を実行しますtextPane.add(list, YourLayoutManager.POPUP_LIST)。次に、レイアウト マネージャーは、list優先サイズに従ってレイアウトされるはずだったという事実を記憶し、それに応じてlayoutContainer-method でレイアウトします。YourLayoutManager(アタッチされている への参照を与えると、JTextPaneおそらくlist現在のキャレット位置のすぐ横にレイアウトすることもできます。)

于 2010-04-30T07:34:58.140 に答える
1

リストを (コードを介して) 動的に入力している場合、スクロールバーを使用しないのは得策ではありません。20 個のリスト アイテムに対して fe が必要な場合はうまくいくかもしれませんが、2000 のようにさらにデータを使用する必要がある場合はどうなるか想像してみてください。GUI が台無しになります。

于 2010-04-30T07:32:00.660 に答える