Make JSlider thumb appear upon clicking track

120 views Asked by At

I am trying to modify a JSlider so that the thumb (knob) appears upon clicking the track. I am adapting code found here to achieve this goal. Essentially, I hide the thumb by initially setting its size to 0,0. When the mouse clicks the track, the thumb changes its size so that it appears. The problem I have encountered is that the track does not appear either. However, it appears once the track is clicked. Is there away modify the thumb (or hide it somehow) without altering the track? Any suggestions would be appreciated.

public class SliderUI extends MetalSliderUI {
private boolean displayThumb = false;
private int x = 0;
private int y = 0;

public SliderUI(){
    super();
}


@Override
 protected Dimension getThumbSize() {
     return new Dimension(x, y);
 }

@Override
protected void scrollDueToClickInTrack(int direction) {

    //keep displaying thumb once true
    if(!displayThumb){
        x = 15;
        y = 20;
        displayThumb = true;
    }
    int value = slider.getValue(); 
    if (slider.getOrientation() == JSlider.HORIZONTAL) {
        value = this.valueForXPosition(slider.getMousePosition().x);
    } else if (slider.getOrientation() == JSlider.VERTICAL) {
        value = this.valueForYPosition(slider.getMousePosition().y);
    }
    slider.setValue(value);
}

}

Update: I also tried a similar approach with paintThumb(), but to no avail. I received a null pointer exception error. Here is what I tried:

public class SliderUI extends MetalSliderUI {
private boolean displayThumb = false;
protected static Icon horizThumbIcon;
protected static Icon vertThumbIcon;
private static Icon SAFE_HORIZ_THUMB_ICON;
private static Icon SAFE_VERT_THUMB_ICON;

public SliderUI(){
    super();
}

private static Icon getHorizThumbIcon() {
    if (System.getSecurityManager() != null) {
        return SAFE_HORIZ_THUMB_ICON;
    } else {
        return horizThumbIcon;
    }
}

private static Icon getVertThumbIcon() {
    if (System.getSecurityManager() != null) {
        return SAFE_VERT_THUMB_ICON;
    } else {
        return vertThumbIcon;
    }
}


@Override
public void paintThumb(Graphics g)  {
       //keep displaying thumb once true
        if(!displayThumb){
            displayThumb = true;
            return;
        }
    Rectangle knobBounds = thumbRect;

    g.translate( knobBounds.x, knobBounds.y );

    if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
        getHorizThumbIcon().paintIcon( slider, g, 0, 0 );
    }
    else {
        getVertThumbIcon().paintIcon( slider, g, 0, 0 );
    }

    g.translate( -knobBounds.x, -knobBounds.y );
}

@Override
protected void scrollDueToClickInTrack(int direction) {


    int value = slider.getValue(); 
    if (slider.getOrientation() == JSlider.HORIZONTAL) {
        value = this.valueForXPosition(slider.getMousePosition().x);
    } else if (slider.getOrientation() == JSlider.VERTICAL) {
        value = this.valueForYPosition(slider.getMousePosition().y);
    }
    slider.setValue(value);
}

}

1

There are 1 answers

0
Christopher Fisher On BEST ANSWER

For future reference, here is a simple solution to my question:

public class SliderUI extends MetalSliderUI {

    private boolean displayThumb = false;

    public SliderUI(){
        super();
    }


  @Override
    public void paintThumb(Graphics g) {
      if(displayThumb){
          super.paintThumb(g);
      }
    }

    @Override
    protected void scrollDueToClickInTrack(int direction) {
        //keep displaying thumb once true
        if(!displayThumb){
            displayThumb = true;
        }
        int value = slider.getValue(); 
        if (slider.getOrientation() == JSlider.HORIZONTAL) {
            value = this.valueForXPosition(slider.getMousePosition().x);
        } else if (slider.getOrientation() == JSlider.VERTICAL) {
            value = this.valueForYPosition(slider.getMousePosition().y);
        }
        slider.setValue(value);
    }
}