/** * Moves the selected thumb in the specified direction by a unit increment. This method is called * when the user presses one of the arrow keys. */ @Override public void scrollByUnit(int direction) { synchronized (slider) { int delta = 1 * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); if (upperThumbSelected) { int oldValue = ((RangeSlider) slider).getUpperValue(); ((RangeSlider) slider).setUpperValue(oldValue + delta); } else { int oldValue = slider.getValue(); slider.setValue(oldValue + delta); } } }
/** * Moves the selected thumb in the specified direction by a block increment. This method is called * when the user presses the Page Up or Down keys. */ @Override public void scrollByBlock(int direction) { synchronized (slider) { int blockIncrement = (slider.getMaximum() - slider.getMinimum()) / 10; if (blockIncrement <= 0 && slider.getMaximum() > slider.getMinimum()) { blockIncrement = 1; } int delta = blockIncrement * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); if (upperThumbSelected) { int oldValue = ((RangeSlider) slider).getUpperValue(); ((RangeSlider) slider).setUpperValue(oldValue + delta); } else { int oldValue = slider.getValue(); slider.setValue(oldValue + delta); } } }
public void display() { // Initialize values. rangeSlider.setValue(3); rangeSlider.setUpperValue(7); // Initialize value display. rangeSliderValue1.setText(String.valueOf(rangeSlider.getValue())); rangeSliderValue2.setText(String.valueOf(rangeSlider.getUpperValue())); // Create window frame. JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setTitle("Range Slider Demo"); // Set window content and validate. frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(this, BorderLayout.CENTER); frame.pack(); // Set window location and display. frame.setLocationRelativeTo(null); frame.setVisible(true); }