---
category: swing
folder: VolumeSlider
title: JSliderのUIや色を変更する
tags: [JSlider]
author: aterai
pubdate: 2006-12-04T02:52:00+09:00
description: JSliderのトラックやつまみ、色などを変更して、音量調節風のスライダーを作成します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTWc-B0OXI/AAAAAAAAApQ/t1b78yBXWUQ/s800/VolumeSlider.png
---
* 概要 [#summary]
`JSlider`のトラックやつまみ、色などを変更して、音量調節風のスライダーを作成します。

#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTWc-B0OXI/AAAAAAAAApQ/t1b78yBXWUQ/s800/VolumeSlider.png)

* サンプルコード [#sourcecode]
#code(link){{
class TriangleSliderUI extends MetalSliderUI {
  @Override public void paintThumb(Graphics g) {
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.fillOval(thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height);
    g2.dispose();
  }

  @Override public void paintTrack(Graphics g) {
    int cx, cy, cw, ch;
    int pad;
    Rectangle trackBounds = trackRect;
    if (slider.getOrientation() == SwingConstants.HORIZONTAL ) {
      Graphics2D g2 = (Graphics2D) g.create();

// ...

slider2.setUI(new MetalSliderUI() {
  @Override protected void paintHorizontalLabel(Graphics g, int v, Component l) {
    JLabel lbl = (JLabel) l;
    lbl.setForeground(Color.GREEN);
    super.paintHorizontalLabel(g, v, lbl);
  }
});
slider2.setForeground(Color.BLUE);
}}

* 解説 [#explanation]
- 上: `TriangleSliderUI`
-- `MetalSliderUI`を継承する`SliderUI`を設定
--- `paintThumb(Graphics)`メソッドをオーバーライドしてつまみの形を変更
--- `paintTrack(Graphics)`メソッドをオーバーライドしてトラックの形と色を変更
-- 注: 垂直方向には未対応
- 下: `HorizontalLabelColor`
-- `JSlider#setForeground(Color)`メソッドで目盛の色を青に変更
-- `MetalSliderUI`を継承する`SliderUI`を作成
--- `paintHorizontalLabel(Graphics, int, Component)`メソッドをオーバーライドし目盛を緑色に変更
-- 目盛の色は[[JSliderの目盛にアイコンや文字列を追加する>Swing/SliderLabelTable]]でも変更可能

* 参考リンク [#reference]
- [https://community.oracle.com/thread/1375990 Swing - Problem in changing forground color of JSlider!!]
- [[JSliderの目盛にアイコンや文字列を追加する>Swing/SliderLabelTable]]
- [[JPopupMenuに垂直JSliderを配置してJToggleButtonの上部に表示する>Swing/PopupVolumeSlider]]

* コメント [#comment]
#comment
- `Java SE 6 Runtime (JRE) Update N build 12 Kernel Installer`だと、`NullPointerException`が発生する? -- &user(aterai); &new{2008-03-03 (月) 19:34:50};
- %%別方法のメモ:%% [[JSliderの目盛りをアイコンに変更する>Swing/TriangleTickSlider]]に移動。 -- &user(aterai); &new{2010-05-19 (水) 16:52:13};
- メモ: [https://bugs.openjdk.org/browse/JDK-5099681 Bug ID: 5099681 Windows/Motif L&F: JSlider should use foreground color for ticks.] -- &user(aterai); &new{2010-05-20 (木) 17:24:52};

#comment