• 追加された行はこの色です。
  • 削除された行はこの色です。
---
title: JSliderの目盛にアイコンや文字列を追加する
tags: [JSlider, JLabel, Icon, JButton]
author: aterai
pubdate: 2009-02-23T12:08:35+09:00
description: JSliderの目盛にJComponentを表示することで、アイコンを追加したり、文字列の色などを変更します。
---
* 概要 [#l66e3fb4]
`JSlider`の目盛に`JComponent`を表示することで、アイコンを追加したり、文字列の色などを変更します。

#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTTNRK4g8I/AAAAAAAAAkA/dn8dNaWNmxM/s800/SliderLabelTable.png)

* サンプルコード [#z0aa3bc1]
#code(link){{
Hashtable<Integer, Component> labelTable = new Hashtable<>();
int c = 0;
for(String s:Arrays.asList(
for (String s: Arrays.asList(
    "wi0009-16.png", "wi0054-16.png", "wi0062-16.png",
    "wi0063-16.png", "wi0064-16.png", "wi0096-16.png",
    "wi0111-16.png", "wi0122-16.png", "wi0124-16.png",
    "wi0126-16.png")) {
  labelTable.put(c++,
    new JLabel(s, new ImageIcon(getClass().getResource(s)),
               SwingConstants.RIGHT));
}
labelTable.put(c, new JButton("aaa"));
JSlider slider1 = new JSlider(JSlider.VERTICAL,0,10,0);
JSlider slider1 = new JSlider(JSlider.VERTICAL, 0, 10, 0);
slider1.setLabelTable(labelTable);
slider1.setSnapToTicks(true);
slider1.setPaintTicks(true);
slider1.setPaintLabels(true);
}}

* 解説 [#a3a5576d]
上記のサンプルでは、`JSlider#setLabelTable(Dictionary)`メソッドを使用して、任意のキーと値のペアで作成したマップを追加し、スライダーのラベルを以下のように変更しています。

- 左
-- アイコンを設定した`JLabel`と、`JButton`を追加

- 右、下
-- `JLabel`を追加して、目盛の文字列と色を変更

* 参考リンク [#f7d54ab9]
- [[JSliderのUIや色を変更する>Swing/VolumeSlider]]
- [http://www.icongalore.com/ XP Style Icons - Windows Application Icon, Software XP Icons]

* コメント [#d7c41d7b]
#comment
#comment