---
category: swing
folder: VerticalIconAlignMultilineText
title: JCheckBoxのチェックアイコンを一行目中央に配置する
tags: [JCheckBox, Html, Icon]
author: aterai
pubdate: 2014-03-03T00:16:40+09:00
description: JCheckBoxのテキストが複数行の場合、チェックアイコンが一行目中央に配置されるよう設定します。
image: https://lh4.googleusercontent.com/-xEdb1NQpk3A/UxNGwOHM8dI/AAAAAAAACBE/GDPtPjFUuJs/s800/VerticalIconAlignMultilineText.png
---
* Summary [#summary]
`JCheckBox`のテキストが複数行の場合、チェックアイコンが一行目中央に配置されるよう設定します。
#download(https://lh4.googleusercontent.com/-xEdb1NQpk3A/UxNGwOHM8dI/AAAAAAAACBE/GDPtPjFUuJs/s800/VerticalIconAlignMultilineText.png)
* Source Code Examples [#sourcecode]
#code(link){{
class WindowsVerticalAlignmentCheckBoxUI extends WindowsCheckBoxUI {
@Override public synchronized void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
// ...
// Paint the radio button
int y = HtmlViewUtil.getFirstLineCenterY(text, b, iconRect);
getDefaultIcon().paintIcon(c, g, iconRect.x, iconRect.y + y);
// ...
}
public static int getFirstLineCenterY(
String text, AbstractButton c, Rectangle iconRect) {
int y = 0;
if (text != null && c.getVerticalTextPosition() == SwingConstants.TOP) {
Object o = c.getClientProperty(BasicHTML.propertyKey);
if (o instanceof View) {
View v = (View) o;
try {
Element e = v.getElement().getElement(0);
Shape s = new Rectangle();
Position.Bias b = Position.Bias.Forward;
s = v.modelToView(
e.getStartOffset(), b, e.getEndOffset(), b, s);
y = (int) (.5 + Math.abs(
s.getBounds().height - iconRect.height) * .5);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
}
return y;
}
}
}}
* Description [#explanation]
* Description [#description]
- 左: `SwingConstants.TOP`
-- `JCheckBox#setVerticalTextPosition(SwingConstants.TOP)`を設定してチェックアイコンとテキストの上辺が揃うように設定
-- `JCheckBox`のフォントサイズが大きくなると、チェックアイコンが上にずれてしまう
- 右: `First line center`
-- `WindowsCheckBoxUI#paint(...)`などをオーバーライドし一行目の中央にチェックアイコンの中心が揃うように設定
-- `<html>aa<font size="+5">bb</font>cc...</html>`のような一部の文字サイズを大きくしたテキストを設定しても行の中央に揃えることが可能
* Reference [#reference]
- [https://stackoverflow.com/questions/22121439/jcheckbox-vertical-alignment-for-multi-line-text java - JCheckBox: Vertical alignment for multi-line text - Stack Overflow]
* Comment [#comment]
#comment
#comment