简体   繁体   中英

Using two JFrame slider's as a min and max value setter, how too only allow min to reach the max value?

I have two JFrame sliders which I want to use, the first is too set the minimum value and the second is too set the maximum value. Obviously if someone move the minimum value past the maximum value that wont look right (and maybe wont work? not got too that part yet)

My problem is I am unaware of a method to lock the minimum value at whatever the maximum value is (or 1 below) Example: Max is at 15, min cannot surpass 15 and the slider locks at the 15 mark.

        minSlider.setFont(minSlider.getFont());
        minSlider.setMaximum(20);
        minSlider.setMinimum(1);
        minSlider.setToolTipText("");
        minSlider.setValue(7);
        minSlider.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        getContentPane().add(minSlider, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 170, 280, -1));

        maxSlider.setMaximum(20);
        maxSlider.setMinimum(1);
        maxSlider.setToolTipText("");
        maxSlider.setValue(14);
        getContentPane().add(maxSlider, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 240, 280, -1));

        jLabel1.setText("Min:");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 180, -1, -1));

        jLabel2.setText("Max:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 250, -1, -1));

Add a addChangeListener() on your maxSlider .

That will continuously tell what is max value to the min slider.

Use this code:

maxSlider.addChangeListener(new ChangeListener() {
  public void stateChanged(ChangeEvent e) {
         minSlider.setMaximum(maxSlider.getValue());
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM