有關 Java Swing Slider 的使用可以參考 "How to Use Sliders". 預設 Java Swing Slider 只提供一個方向的滑動. 一個 Swing Tutorial 的範例如下:
通常 Slider 上會有 Min/Max 的設定, 而上面的範例只能選擇由 Min~Max 間的某個值. 但如果我希望使用 Slider 來選擇的是一個 Range, 則可以參考這邊的範例代碼.
In order to create the range slider, we needed a new Swing component because the existing JSlider component only displays a single thumb to adjust the slider value. Interestingly, even though JSlider only allows you to update a single value, its data model actually provides support for two values; these define an internal range within the minimum and maximum bounds.
The internal range is defined by the slider value and an “extent,” which is the length of the inner range that begins at the slider value. In DefaultBoundedRangeModel, the default “extent” is zero and not normally used for anything. We can add it to the slider value to define an upper value of a selected range.
RangeSlider extends JSlider to install a UI delegate and provide value change methods. The methods set the lower and upper values in the selected range. We define the existing slider value as the lower value, and the upper value as the lower value plus the extent. To access these values, we override some methods, and add a couple of our own. This class do below three things: