2

I'm trying to create a jslider that moves withing the following ranges.

[-x,-1)[1,x]

In short I don't want the values -1 and 0 to be valid values for the JSlider. But the values from x to -1 should be allowed, and 1 to x should be allowed.

I'm trying to not write hacky code, so I don't want to write a function in the UI code that just gets the value from a different (continuous) range, and then transforms it to the range I want with a bunch of it statements.

Ideally, I should just be able to call slider.getValue() and know that the return value will be in the range I described above.

Varun Madiath
  • 3,152
  • 4
  • 30
  • 46
  • 1
    Added swing tag. So more people who have experience with this will see it. – Boro May 28 '11 at 09:57
  • Should have definitely thought to add that tag. Thanks for adding it. – Varun Madiath May 28 '11 at 09:58
  • @Varun Madiath what did you try so far? Do you have some code? – Boro May 28 '11 at 10:10
  • @Varun Madiath I think the ranges should be `[-x,-1)[1,x)` instead of `[x,-1)[1,x)`, right? – Boro May 28 '11 at 10:32
  • Yeah, I'll edit that, also the final x should be inclusive – Varun Madiath May 28 '11 at 13:23
  • OK what about -1 it still should be excluded? For reference I updated the code to reflect you latest range edit. – Boro May 28 '11 at 14:04
  • Currently I've got the really ugly method of doing everything with the return value from the slider.getValue() method. I simply changed the boundaries tp [-(x-2),x] and then subtracted 2 from the value if x was less than or equal to 0. – Varun Madiath May 28 '11 at 17:36
  • I guess I could use the method above and override `DefaultBoundedRangeModel`'s `setValue` implementation, but I'm trying to find a better way to do this if possible. – Varun Madiath May 28 '11 at 17:37
  • Please let me know if you will figure out something nicer. – Boro May 28 '11 at 19:31

2 Answers2

2

I think you must do this value adjustment yourself perhaps within overridden method setValue()?

Try out this code:

    int x = 10;
    @Override
    public void setValue(int n)
    {
        if((n >= -x && n < -1)|| (n =< x && n >= 1))
        {   
            super.setValue(n);
            System.out.println("OI in setValue");
        }
    }
Boro
  • 7,913
  • 4
  • 43
  • 85
  • I was thinking that code like this really belongs in the model. Don't you? – Varun Madiath May 28 '11 at 13:26
  • @Varun Madiath you are right. It would be much better. Check out `BoundedRangeModel` then which you can then set to the slider. It has setValue method thus I believe you could copy the code over to your model. – Boro May 28 '11 at 14:02
  • Great, the problem I see with your code, is that if the user were to drag the slide to a value of -1 or 0, the slider would simply bounce back to it's prior position. Which will confuse the user. – Varun Madiath May 28 '11 at 17:35
  • @Varun Madiath well you are right. It was just a suggestion a sample code to start with :) now you should handle special cases yourself, i.e. see where it is released and set to the closes possible :) – Boro May 28 '11 at 19:30
2

A slider's value is just the ratio between the thumb's current position and the sliders width, in pixels. IIUC, you have three ranges, [-x,-1), [-1,1) and [1,x], so you'll need two thumbs. You might look at JXMultiThumbSlider, which supports a MultiThumbModel.

Addendum: For a related use-case, I started with How to Write a Custom Swing Component. It's laborious, but it may produce a cleaner result.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • are you "familiar" with java.net, why there everything d*i*e*d +1 – mKorbel May 28 '11 at 20:11
  • @mKorbel: Excellent point, but the API might serve as a guide to any extant source or examples. – trashgod May 28 '11 at 20:21
  • big and largiest project are unaccessible for example SwingX http://stackoverflow.com/questions/6154504/jxmultisplitpane-causes-repaint-during-slider-adjustment, that's horror suck swingx.jar ... – mKorbel May 28 '11 at 20:25
  • @mKorbel hi guys do you have a link to an image or a simple example with this component. Would be nice to see it. I think, that there was at least a year ago a jar demonstrating all SwingX components, am I right? – Boro May 29 '11 at 19:51
  • @Boro: I _thought_ I saw one on SO, but I can't find it. – trashgod May 29 '11 at 20:20