TITLE:JEditorPaneで長い行を折り返さない
#navi(../)
#tags(JEditorPane, JTextPane, StyledDocument)
RIGHT:Posted by &author(aterai); at 2007-09-24
*JEditorPaneで長い行を折り返さない [#v31d450a]
``JEditorPane``や、``JTextPane``で、行を``Viewport``の幅で折り返さないよう設定します。
-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTQbo-LQJI/AAAAAAAAAfk/YnnnPAQE-R4/s800/NoWrapTextPane.png)

**サンプルコード [#qd3a5388]
#code(link){{
class NoWrapParagraphView extends ParagraphView {
  public NoWrapParagraphView(Element elem) {
    super(elem);
  }
  @Override protected SizeRequirements calculateMinorAxisRequirements(
      int axis, SizeRequirements r) {
    SizeRequirements req = super.calculateMinorAxisRequirements(axis, r);
    req.minimum = req.preferred;
    return req;
  }
  @Override public int getFlowSpan(int index) {
    return Integer.MAX_VALUE;
  }
}
}}

**解説 [#k626f953]
上記のサンプルでは、スパンの必要サイズを計算する``calculateMinorAxisRequirements``メソッドなどをオーバーライドして、行折り返し段落のビュー(``ParagraphView``)で折り返しが発生しないようにしています。

``JEditorPane``や``JTextPane``といった``StyledDocument``をモデルにしているテキストコンポーネントに非常に長い行をペーストした場合、表示が更新されなくなりますが、折り返しできなくしてしまうと多少ましになるようです。

``JTextArea``でも行を非常に長くしてしまうと、カーソルキーの移動などで異常に時間がかかる場合があります。

- 例えば、このサンプルで、カーソルを一番最後に移動し、一行目(非常に長い行)にKBD{↑}キーで移動すると発生する
- [https://forums.oracle.com/message/5776978 Swing - Long last line in wrappable textarea hangs GUI (bug in java?)]

----
以下のような方法もあります。
- [https://forums.oracle.com/forums/thread.jspa?threadID=1351861 Swing - Disabling word wrap for JTextPane]
-- ``BoxView#layout(...)``をオーバーライド
- [http://www.java2s.com/Code/Java/Swing-JFC/NonWrappingWrapTextPane.htm Non Wrapping(Wrap) TextPane : TextField : Swing JFC : Java examples (example source code) Organized by topic]
-- ``JTextPane#getScrollableTracksViewportWidth()``をオーバーライド

**参考リンク [#ydbfb81a]
- [https://forums.oracle.com/forums/thread.jspa?threadID=1351861 Swing - Disabling word wrap for JTextPane]
- [http://www.java2s.com/Code/Java/Swing-JFC/NonWrappingWrapTextPane.htm Non Wrapping(Wrap) TextPane : TextField : Swing JFC : Java examples (example source code) Organized by topic]
-[http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6502558 Bug ID: 6502558 AbstractDocument fires event not on Event Dispatch Thread]
--[http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html Alexander Potochkin's Blog: Debugging Swing, the final summary]
- [[JTextPaneを一行に制限してスタイル可能なJTextFieldとして使用する>Swing/OneLineTextPane]]

**コメント [#rdd302ba]
#comment