• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JSliderの目盛りをアイコンに変更する
#navi(../)
RIGHT:Posted by &author(aterai); at 2010-05-24
*JSliderの目盛りをアイコンに変更する [#xbb1ae7a]
JSliderの目盛りをアイコンで描画します。
---
category: swing
folder: TriangleTickSlider
title: JSliderの目盛りをアイコンに変更する
tags: [JSlider, Icon, JLabel]
author: aterai
pubdate: 2010-05-24T15:00:41+09:00
description: JSliderの目盛りに使用するJLabelを取得し、アイコンを追加したり文字色を変更するなどの変更を行います。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTWS_t-t1I/AAAAAAAAApA/78UrJyqx8og/s800/TriangleTickSlider.png
---
* 概要 [#summary]
`JSlider`の目盛りに使用する`JLabel`を取得し、アイコンを追加したり文字色を変更するなどの変更を行います。

-&jnlp;
-&jar;
-&zip;
#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTWS_t-t1I/AAAAAAAAApA/78UrJyqx8og/s800/TriangleTickSlider.png)

//#screenshot
#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTWS_t-t1I/AAAAAAAAApA/78UrJyqx8og/s800/TriangleTickSlider.png)

**サンプルコード [#rf8f8227]
* サンプルコード [#sourcecode]
#code(link){{
JSlider slider = new JSlider(0,100);
JSlider slider = new JSlider(0, 100);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(5);
slider.setPaintLabels(true);
slider.setSnapToTicks(true);
slider.putClientProperty("Slider.paintThumbArrowShape", Boolean.TRUE);
Dictionary dictionary = slider.getLabelTable();
if(dictionary != null) {
    Enumeration elements = dictionary.elements();
    Icon tick = new TickIcon();
    while(elements.hasMoreElements()) {
        JLabel label = (JLabel) elements.nextElement();
        label.setBorder(BorderFactory.createEmptyBorder(1,0,0,0));
        label.setIcon(tick);
        label.setIconTextGap(0);
        label.setVerticalAlignment(SwingConstants.TOP);
        label.setVerticalTextPosition(SwingConstants.BOTTOM);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setHorizontalTextPosition(SwingConstants.CENTER);
        label.setForeground(Color.RED);
    }
if (dictionary != null) {
  Enumeration elements = dictionary.elements();
  Icon tick = new TickIcon();
  while (elements.hasMoreElements()) {
    JLabel label = (JLabel) elements.nextElement();
    label.setBorder(BorderFactory.createEmptyBorder(1, 0, 0, 0));
    label.setIcon(tick);
    label.setIconTextGap(0);
    label.setVerticalAlignment(SwingConstants.TOP);
    label.setVerticalTextPosition(SwingConstants.BOTTOM);
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setHorizontalTextPosition(SwingConstants.CENTER);
    label.setForeground(Color.RED);
  }
}
}}

**解説 [#xf147d2c]
上記のサンプルでは、JSliderのラベル(JLabel)をJSlider#getLabelTable()メソッドで取得し、このラベルに三角形のアイコンを追加して、目盛り(MajorTick)の代わりとして表示しています。
-注: JSlider#setOrientation(SwingConstants.VERTICAL)には未対応
* 解説 [#explanation]
上記のサンプルでは、`JSlider`のラベル(`JLabel`)一覧を`JSlider#getLabelTable()`メソッドで取得し、この各`JLabel`に三角形のアイコンを追加して目盛り(`MajorTick`)の代替としています。

**参考リンク [#l55155ea]
-[[JSliderのUIや色を変更する>Swing/VolumeSlider]]
-[[JSliderの目盛にアイコンや文字列を追加する>Swing/SliderLabelTable]]
- `JSlider#setOrientation(SwingConstants.VERTICAL)`で作成した垂直`JSlider`には未対応

**コメント [#a4d8203c]
* 参考リンク [#reference]
- [[JSliderのUIや色を変更する>Swing/VolumeSlider]]
- [[JSliderの目盛にアイコンや文字列を追加する>Swing/SliderLabelTable]]

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