Swing/SeparatorOnTextPane のバックアップ差分(No.2)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/SeparatorOnTextPane へ行く。
- 追加された行はこの色です。
- 削除された行はこの色です。
--- title: JTextPaneにJSeparatorを追加する tags: [JTextPane, HTMLEditorKit, HTMLDocument, JSeparator, JLabel, MatteBorder] author: aterai pubdate: 2015-04-20T00:00:12+09:00 description: JTextPaneにセパレータとして、hr要素やJSeparator、MatteBorderを設定したJLabelなどを追加します。 --- * 概要 [#j05937c8] `JTextPane`にセパレータとして、`hr`要素や`JSeparator`、`MatteBorder`を設定した`JLabel`などを追加します。 #download(https://lh3.googleusercontent.com/-DttSBuXmfOs/VTOx9lKJR2I/AAAAAAAAN2s/3ZCvhgC8QDw/s800/SeparatorOnTextPane.png) * サンプルコード [#cfebdf7e] #code(link){{ HTMLEditorKit kit = new HTMLEditorKit(); HTMLDocument doc = new HTMLDocument(); textPane.setEditorKit(kit); textPane.setDocument(doc); textPane.setEditable(false); textPane.setText("<html><hr>:<hr />"); textPane.insertComponent(new JLabel("JSeparator: ")); textPane.insertComponent(new JSeparator(JSeparator.HORIZONTAL)); insertBR(kit, doc); textPane.insertComponent(new JLabel("MatteBorder1: ")); textPane.insertComponent(new JLabel() { @Override public void updateUI() { super.updateUI(); setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.RED)); } @Override public Dimension getMaximumSize() { return new Dimension(textPane.getSize().width, 1); } }); insertBR(kit, doc); textPane.insertComponent(new JLabel("MatteBorder2: ")); textPane.insertComponent(new JLabel() { @Override public void updateUI() { super.updateUI(); setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GREEN)); } @Override public Dimension getPreferredSize() { return new Dimension(textPane.getSize().width, 1); } @Override public Dimension getMaximumSize() { return this.getPreferredSize(); } }); insertBR(kit, doc); textPane.insertComponent(new JLabel("JSeparator.VERTICAL ")); textPane.insertComponent(new JSeparator(JSeparator.VERTICAL) { @Override public Dimension getPreferredSize() { return new Dimension(1, 16); } @Override public Dimension getMaximumSize() { return this.getPreferredSize(); } }); textPane.insertComponent(new JLabel(" TEST")); }} * 解説 [#bc760f8e] - `hr`要素 -- `htmlEditorKit.insertHTML(doc, doc.getLength(), "<hr />", 0, 0, null);`などで、`hr`要素を追加 - `JSeparator` -- `textPane.insertComponent(new JSeparator(JSeparator.HORIZONTAL));`で水平`JSeparator`を追加 - `MatteBorder1` -- `MatteBorder`を設定したテキスト無しの`JLabel`を追加 -- 最大サイズが(`JTextPane`の幅×`1`)になるよう、`JLabel#getMaximumSize()`をオーバーライド - `MatteBorder2` -- `MatteBorder`を設定したテキスト無しの`JLabel`を追加 -- 推奨サイズと最大サイズが(`JTextPane`の幅×`1`)になるよう、`JLabel#getPreferredSize()`と`JLabel#getMaximumSize()`をオーバーライド - `JSeparator.VERTICAL` -- `textPane.insertComponent(new JSeparator(JSeparator.VERTICAL));`で垂直`JSeparator`を追加 -- `JLabel#getPreferredSize()`、`JLabel#getMaximumSize()`をオーバーライドしないと表示されない? -- `JSeparator#getPreferredSize()`、`JSeparator#getMaximumSize()`をオーバーライドしないと表示されない --- [https://docs.oracle.com/javase/tutorial/uiswing/components/separator.html How to Use Separators (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)] > In most implementations, a vertical separator has a preferred height of 0, and a horizontal separator has a preferred width of 0. This means a separator is not visible unless you either set its preferred size or put it in under the control of a layout manager such as BorderLayout or BoxLayout that stretches it to fill its available display area. //* 参考リンク * コメント [#z081fac5] #comment #comment