Swing/TriangleTickSlider のバックアップの現在との差分(No.1)
TITLE:JSliderの目盛りをアイコンに変更する
Posted by aterai at 2010-05-24
JSliderの目盛りをアイコンに変更する
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:
概要
JSlider
の目盛りに使用するJLabel
を取得し、アイコンを追加したり文字色を変更するなどの変更を行います。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
サンプルコード
サンプルコード
#spandel
JSlider slider = new JSlider(0,100);
#spanend
#spanadd
JSlider slider = new JSlider(0, 100);
#spanend
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(5);
slider.setPaintLabels(true);
slider.setSnapToTicks(true);
#spanadd
slider.putClientProperty("Slider.paintThumbArrowShape", Boolean.TRUE);
#spanend
Dictionary dictionary = slider.getLabelTable();
#spandel
if(dictionary != null) {
#spanend
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);
}
#spanadd
if (dictionary != null) {
#spanend
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);
}
}
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JSliderのラベル(JLabel)をJSlider#getLabelTable()メソッドで取得し、このラベルに三角形のアイコンを追加して、目盛り(MajorTick)の代わりとして表示しています。- 注: JSlider#setOrientation(SwingConstants.VERTICAL)には未対応
解説
-
JSlider
のラベル(JLabel
)一覧をJSlider#getLabelTable()
メソッドで取得し、この各JLabel
に三角形のアイコンを追加して目盛り(MajorTick
)として使用 -
JSlider#setOrientation(SwingConstants.VERTICAL)
で作成した垂直JSlider
には未対応