Swing/IconTitledBorder のバックアップ(No.9)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/IconTitledBorder へ行く。
- category: swing
folder: IconTitledBorder
title: TitledBorderのタイトルにアイコンを表示する
tags: [Border, TitledBorder, ImageIcon, Html]
author: aterai
pubdate: 2015-09-07T00:03:04+09:00
description: TitledBorderのタイトルに文字列だけでなく、アイコンを表示するように設定します。
image:
Summary
TitledBorder
のタイトルに文字列だけでなく、アイコンを表示するように設定します。
Screenshot

Advertisement
Source Code Examples
URL url = getClass().getResource("16x16.png");
String path = url.toString();
String title = String.format(
"<html><table cellpadding='0'><tr><td><img src='%s'></td><td>test</td></tr></table></html>", path);
panel.setBorder(BorderFactory.createTitledBorder(title));
View in GitHub: Java, KotlinExplanation
<img>
TitledBorder#setTitle(...)
を使用して<img>
タグでアイコンを指定したHtml
テキストを設定Class#getResource(...)
でアイコンのURL
を取得して<img>
タグのソースに指定- アイコンとテキストのベースラインが揃っていない
<table>
+<img>
<table><tr>
タグを使用して、アイコンとテキストのベースラインを揃える
TitledBorder#paintBorder(...)
TitledBorder#paintBorder(...)
をオーバーライドしてアイコンを描画- タイトル文字列に半角空白で余白を作成し、位置を決め打ちでアイコンを描画
ComponentTitledBorder
- BorderにJComponentを配置からマウスリスナーなどを削除し、アイコンを追加した
JLabel
を適用
- BorderにJComponentを配置からマウスリスナーなどを削除し、アイコンを追加した