0

JScrollPaneページ分割された要素のリストを表示するBoxLayout( Boxa も試しましたが) を使用GridLayoutしています。各ページには 5 つの要素があり、各要素には 5 つのビジュアル コンポーネントがあります。要素を満たすためのデータは、ページが変更されるたびにロード時に Web サービスから取得され、リスト内の現在の要素が削除され、新しい要素が追加されます (これは最適化できることはわかっていますが、今のところはこれだけです)。

ここで奇妙なことは、ロード時とページ変更時の両方で、垂直スクロールバーが上でも下でもなく、正確な中央でもない中央の位置に移動することです。コンポーネントを追加した後、プログラムで上に移動しようとしましたが、データ表示メソッドが終了した後、スクロールはその位置に再び置かれます。その後、次のページが変わるまで、通常どおりスクロール バーを使用できます。

実際、私は に を付けてAdjustmentListener、スクロールバーをその位置に移動するイベントがトリガーされるJScrollBar回数を 25 回 (5*5、当たり前!) 数えることができます。AdjustmentEvent.

スクロールバーを移動する前にウィンドウで呼び出しを試みrevalidate()ましrepaint()たが、何も機能していないようで、追加されたコンポーネントごとにその特定の位置に移動し続けます。

何か案は?一部のコードを掲載できなくて申し訳ありませんが、ごちゃごちゃしていて、問題の原因となっている可能性のある重要な部分を抽出するのが困難です。

どうもありがとう。

アップデート

@kleopatraに返信して、私がやっていることと同様のコードを作成しようとしますが、何らかの形で奇妙な動作を引き起こす何かをスキップしている可能性があります.

public class MyUI extends JPanel {

    List<Data> data;
    JPanel dataItemsPanel;
    JScrollPane scrollPane;

    // ...

    /**
     * Method to build the UI.
     */
    void createUI() {

        this.setLayout(new BorderLayout());

        // ...

        JPanel left = new JPanel();
        JPanel right = new JPanel(new BorderLayout());

        // ...

        dataItemsPanel = new JPanel(new GridLayout(0, 1));
        // this is the scroll pane that behaves weird
        scrollPane = new JScrollPane(dataItemsPanel);

        //...

        right.add(scrollPane, BorderLayout.CENTER);
        JSplitPane splitPane = new JSplitPane(JSPlitPane.HORIZONTAL_SPLIT,
            left, right);
        splitPane.setResizeWeight(0);
        splitPane.setOneTouchExpandable(true);
        splitPane.setContinuousLayout(true);
        this.add(splitPane, BorderLayout.CENTER);

        // ...

        loadPage(0);

    }

    /**
     * Method to load a data page. Called when the program starts (to load
     * the first page) and whenever the data page is changed.
     */
    void loadPage(int page) {

        data = retrievePageDataFromWebService(page);

        // remove previous data
        dataItemsPanel.removeAll();
        // add new data
        for (Data d : data) {
            // build complex data item representation
            JPanel description = new JPanel(new FlowLayout(FlowLayout.LEFT));
            description.add(new JEditorPane(/*...*/));

            JPanel options = new JPanel(new FlowLayout(FlowLayout.LEFT));
            options.add(new JButton(/*...*/));
            options.add(new JButton(/*...*/));

            JPanel leftDataPanel = new JPanel(new BorderLayout());
            leftDataPanel.add(new JEditorPane(/*...*/));

            JPanel rightDataPanel = new JPanel(new BorderLayout());
            rightDataPanel.add(new JEditorPane(/*...*/));
            rightDataPanel.add(options, BorderLayout.CENTER);

            JPanel data = new JPanel(new GridLayout(1, 2));
            data.add(leftDataPanel);
            data.add(rightDataPanel);

            JPanel dataItemContainer = new JPanel(new BorderLayout());
            dataItemContainer.add(description, BorderLayout.NORTH);
            dataItemContainer.add(data, BorderLayout.CENTER);

            // finally add it to the data panel
            dataItemsPanel.add(dataItemContainer);

        }

        /*
        After this method finishes, an AdjustmentEvent is called once per
        added component (25 times). Trying to set the scroll bar position
        at this point has no effect, as the events are triggered after the
        method. I have tried to call revalidate() and repaint() here and
        then move the scroll bar but the result is the same.
         */
    }
}
4

0 に答える 0