From 92eaefef220a2b2128f56a1458a361f744d32f73 Mon Sep 17 00:00:00 2001 From: mdb Date: Thu, 25 Jul 2002 23:21:33 +0000 Subject: [PATCH] A simple "label : slider : value label" combination. git-svn-id: https://samskivert.googlecode.com/svn/trunk@802 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/swing/SimpleSlider.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 projects/samskivert/src/java/com/samskivert/swing/SimpleSlider.java diff --git a/projects/samskivert/src/java/com/samskivert/swing/SimpleSlider.java b/projects/samskivert/src/java/com/samskivert/swing/SimpleSlider.java new file mode 100644 index 00000000..1d5009ae --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/swing/SimpleSlider.java @@ -0,0 +1,90 @@ +// +// $Id: SimpleSlider.java,v 1.1 2002/07/25 23:21:33 mdb Exp $ + +package com.samskivert.swing; + +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JSlider; + +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +/** + * Displays a slider with a label on the left explaining what the slider + * does and a label on the right displaying the slider's current value. + */ +public class SimpleSlider extends JPanel + implements ChangeListener +{ + /** + * Creates a simple slider with the specified configuration. + * + * @param label the text to display to the left of the slider. + * @param min the slider's minimium value. + * @param max the slider's maximum value. + * @param value the slider's starting value. + */ + public SimpleSlider (String label, int min, int max, int value) + { + setLayout(new HGroupLayout(HGroupLayout.STRETCH)); + add(_label = new JLabel(label), HGroupLayout.FIXED); + add(_slider = new JSlider(min, max, value)); + _slider.addChangeListener(this); + add(_value = new JLabel(Integer.toString(min)), HGroupLayout.FIXED); + } + + // documentation inherited + public void stateChanged (ChangeEvent e) + { + JSlider source = (JSlider)e.getSource(); + if (!source.getValueIsAdjusting()) { + _value.setText(Integer.toString(source.getValue())); + } + } + + /** + * Updates the label displayed to the left of the slider. + */ + public void setLabel (String label) + { + _label.setText(label); + } + + /** + * Returns the current value of the slider. + */ + public int getValue () + { + return _slider.getValue(); + } + + /** + * Sets the slider's current value. + */ + public void setValue (int value) + { + _slider.setValue(value); + _value.setText(Integer.toString(value)); + } + + /** + * Sets the slider's minimum value. + */ + public void setMinimum (int minimum) + { + _slider.setMinimum(minimum); + } + + /** + * Sets the slider's maximum value. + */ + public void setMaximum (int maximum) + { + _slider.setMaximum(maximum); + } + + protected JLabel _label; + protected JSlider _slider; + protected JLabel _value; +}