2

JScrollPane内に(URLからではなく)カスタムhtmlドキュメントを含むJEditorPaneと、ユーザーがエディターペインで強調表示されるテキストを入力できるようにJTextFieldがあります。テキストフィールドのkeyPressedイベントで、ドキュメントでテキストを検索し、次のように囲みます。

<a name='spot'><span style='background-color: silver'>my text</span></a> 

背景を強調表示してから、新しいテキストをJEditorPaneに設定します。これはすべて正常に機能しますが、ペインをスクロールして新しく強調表示されたテキストまで表示したいと思います。したがって、エディタペインのdocumentListenerのchangedUpdateメソッドに、次を追加します。

pane.scrollToReference("spot"); 

この呼び出しは、BoxView.modelToView内にArrayIndexOutOfBoundsExceptionをスローします。このメソッドはテキスト内の「スポット」参照を検出しますが、ビューがまだ新しいテキストで更新されていない可能性があるため、そこでスクロールしようとすると失敗します。

ビューへの参照を取得できず、JEditorPaneのビューが完全に更新されたことを意味するリッスンするイベントが見つからないようです。何か案は?

ありがとう、

ジャレド

4

1 に答える 1

3

JScrollPane#scrollToReference(java.lang.String reference) URLへの文字列参照について、

Scrolls the view to the given reference location (that is, the value 
returned by the UL.getRef method for the URL being displayed).

次に、次の回避策を示すすべての例

import java.io.IOException;
import java.net.URL;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class MyScrollToReference extends JDialog {
    private static final long serialVersionUID = 1L;

    public MyScrollToReference(JFrame frame, String title, boolean modal, String urlString) {
        super(frame, title, modal);

        try {
            final URL url = MyScrollToReference.class.getResource(urlString);
            final JEditorPane htmlPane = new JEditorPane(url);
            htmlPane.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(htmlPane);
            getContentPane().add(scrollPane);
            htmlPane.addHyperlinkListener(new HyperlinkListener() {

                public void hyperlinkUpdate(HyperlinkEvent e) {
                    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                        if (e.getURL().sameFile(url)) {
                            try {
                                htmlPane.scrollToReference(e.getURL().getRef());
                            } catch (Throwable t) {
                                t.printStackTrace();
                            }
                        }
                    }
                }
            });
        } catch (IOException e) {
        }
    }
}
于 2012-04-10T17:29:35.737 に答える