TITLE:JEditorPaneで長い行を折り返さない

JEditorPaneで長い行を折り返さない

編集者:Terai Atsuhiro~

作成日:2007-09-24
更新日:2021-07-22 (木) 22:11:40
  • category: swing folder: NoWrapTextPane title: JEditorPaneで長い行を折り返さない tags: [JEditorPane, JTextPane, StyledDocument] author: aterai pubdate: 2007-09-24T18:04:06+09:00 description: JEditorPaneや、JTextPaneで、行をViewportの幅で折り返さないよう設定します。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTQbo-LQJI/AAAAAAAAAfk/YnnnPAQE-R4/s800/NoWrapTextPane.png

概要

JEditorPaneや、JTextPaneで、行をViewportの幅で折り返さないよう設定します。

概要

JEditorPaneや、JTextPaneで、行をViewPortの幅で折り返さないよう設定します。 Swing - Disabling word wrap for JTextPaneの投稿をそのまま引用しています。

#screenshot

サンプルコード

#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
class NoWrapParagraphView extends ParagraphView {
  public NoWrapParagraphView(Element elem) {
    super(elem);
  }
  protected SizeRequirements calculateMinorAxisRequirements(
#spanadd

#spanend
  @Override protected SizeRequirements calculateMinorAxisRequirements(
      int axis, SizeRequirements r) {
    SizeRequirements req = super.calculateMinorAxisRequirements(axis, r);
    req.minimum = req.preferred;
    return req;
  }
  public int getFlowSpan(int index) {
#spanadd

#spanend
  @Override public int getFlowSpan(int index) {
    return Integer.MAX_VALUE;
  }
}
  • &jnlp;
  • &jar;
  • &zip;

解説

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

解説

上記のサンプルでは、スパンの必要サイズを計算するcalculateMinorAxisRequirementsメソッドなどをオーバーライドして、行折り返し段落のビュー(ParagraphView)で折り返しが発生しないように設定しています。 JEditorPaneやJTextPaneといったStyledDocumentをモデルにしているテキストコンポーネントに非常に長い行をペーストした場合、表示が更新されなくなりますが、折り返しできなくしてしまうと多少ましになるようです。
  • JEditorPaneJTextPaneといったStyledDocumentをモデルにしているテキストコンポーネントに極めて長い行をペーストすると表示が更新されなくなる場合がある
    • 折り返し不可に設定するとこの動作が緩和される?
  • JTextAreaでも行を極めて長くしてしまうと、カーソルキーの移動などで異常に時間がかかる場合がある
JTextAreaでも行を非常に長くしてしまうと、カーソルキーの移動などで異常に時間がかかる場合があります。

参考リンク

参考リンク

コメント

コメント