Swing/InsertComponentBaseline のバックアップ差分(No.10)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/InsertComponentBaseline へ行く。
- 1 (2012-09-03 (月) 06:06:39)
- 2 (2012-12-07 (金) 16:50:09)
- 3 (2012-12-25 (火) 18:20:51)
- 4 (2013-01-28 (月) 02:08:18)
- 5 (2015-02-25 (水) 17:38:04)
- 6 (2016-05-27 (金) 15:36:14)
- 7 (2017-04-04 (火) 14:13:45)
- 8 (2017-04-07 (金) 13:51:51)
- 9 (2017-08-12 (土) 21:48:41)
- 10 (2018-08-10 (金) 16:18:21)
- 11 (2019-06-25 (火) 15:57:13)
- 12 (2021-03-02 (火) 14:37:13)
- 13 (2022-07-21 (木) 08:40:08)
- 追加された行はこの色です。
- 削除された行はこの色です。
---
category: swing
folder: InsertComponentBaseline
title: JTextPaneに追加するコンポーネントのベースラインを揃える
tags: [JTextPane, JComponent, Baseline]
author: aterai
pubdate: 2012-09-03T06:06:39+09:00
description: JTextPaneに追加するコンポーネントのベースラインが他の文字列などとを揃うように設定します。
image: https://lh3.googleusercontent.com/-JveOiooEbAg/UEPEjv1VW2I/AAAAAAAABR4/qts-97h_JuA/s800/InsertComponentBaseline.png
---
* 概要 [#summary]
`JTextPane`に追加するコンポーネントのベースラインが他の文字列などとを揃うように設定します。
#download(https://lh3.googleusercontent.com/-JveOiooEbAg/UEPEjv1VW2I/AAAAAAAABR4/qts-97h_JuA/s800/InsertComponentBaseline.png)
* サンプルコード [#sourcecode]
#code(link){{
JCheckBox check1 = new JCheckBox("JComponent.setAlignmentY(...)");
Dimension d = check1.getPreferredSize();
int baseline = check1.getBaseline(d.width, d.height);
check1.setAlignmentY(baseline / (float) d.height);
textPane.replaceSelection("\n\n Baseline: ");
textPane.insertComponent(check1);
}}
* 解説 [#explanation]
- 上: `Default`
-- `JTextPane#insertComponent(...)`で、`JCheckBox`を追加
-- `JCheckBox`のデフォルトの`AlignmentY`は`0.5`なのでテキストのベースラインと揃わない
- 中: `JComponent#setAlignmentY(...)`
-- `JComponent#getBaseline()`でベースラインを取得し、`JComponent#setAlignmentY(baseline/(float)d.height)`でテキストベースラインの相対位置に配置
- 下: `setAlignmentY+setCursor+...`
-- 「中: `JComponent#setAlignmentY(...)`」+`Cursor+Opaque+Focusable`を設定
#code{{
check2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
check2.setOpaque(false);
check2.setFocusable(false);
}}
----
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTextPane.html#insertComponent-java.awt.Component- JTextPane#insertComponent(Component) (Java Platform SE 8)]
> コンポーネントは、`Component.getAlignmentY`によって返された値に従って、テキストベースラインに相対的に配置されます。`Swing`コンポーネントの場合、`JComponent.setAlignmentY`メソッドを使うと、この値を簡単に設定できます。たとえば、値を`0.75`に設定すると、コンポーネントの`75%`がベースラインの上に、`25%`がベースラインの下になります。
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTextPane.html#insertComponent-java.awt.Component- JTextPane#insertComponent(Component) (Java Platform SE 8)]
- [https://stackoverflow.com/questions/12151158/how-to-appropriately-adding-jlabel-to-jeditorpane java - How to appropriately adding JLabel to JEditorPane? - Stack Overflow]
* コメント [#comment]
#comment
#comment