2

I want to implement a date chooser using a JSlider. The user should be able to use the slider to freely choose between two previously known dates. I've seen examples like this one:

enter image description here

But I want to do the same, using only one slider. The minimal distance between two points (tick) should be one day. Any hints how to implement that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • So the range of the slider should be adjusted once a selection is made, right? Or should there be some sort of scrolling when the knob is placed at the outer values? One way to implement that might be not to slide the knob but to slide the slider, i.e. adjust the min/max to selected day -/+ X and reposition the knob at the center. – Thomas Jan 10 '12 at 16:37
  • The slider should always display the same range (e.g. from 1.1.2012 until 10.1.2012), and in this range the user should be available to choose one day. –  Jan 10 '12 at 16:38

3 Answers3

3

If you want to have a slider with min = 1.1.2012 and max = 10.1.2012 just create a slider with min = 0 and max = number of days in between, then add the selected number to 1.1.2012.

I assume 10.1.2012 means January 10th, thus your slider would have min = 0 and max = 9. Then set the labels accordingly.

Thomas
  • 87,414
  • 12
  • 119
  • 157
3

I can't to image how to do that with one JSlider, because there you'd have bunch of days, there are some workarounds for Double/RangeSlider, but I think better and easiest would be implements JSpinner with SpinnerDateModel, or best options is look for Custom Java Calendar or DatePicker

EDIT (@Robin)

enter image description here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

First to answer your question: you can just use a JSlider, use the number of days between your start and end date to determine the range, and use custom labels (by using for example the setLabelTable method)

Now for user-friendliness, avoid this since

  1. Nobody is familiar with this concept. Every site/application nowadays uses a textfield, most of the time in combination with a calendar widget. That is what users expect, not a slider
  2. It will be hard to get all dates as labels on the slider due to the limited width. This means that a user have to interpolate / count to select his correct date
  3. If you stick to the slider approach, at least consider to add a textfield as well. Even a non-editable text field which shows the currently selected date would be a huge improvement over a slider (see point 2)
Robin
  • 36,233
  • 5
  • 47
  • 99
  • +1 maybe my insomnia, maybe wrong description by OP's, I'm not sure if s/he meaning JSlider with two buttons, please see attn picture – mKorbel Jan 10 '12 at 21:54
  • @mKorbel one, two, three buttons, it does not matter. Datepickers are so common that nobody expects a slider for date choosing. The only sliders I can imagine related to time are in movie/music players, but those are sliders for a whole other purpose. A search in google images for 'date picker' and 'date slider' proves my point. The first shows UI I can relate to and recognize, the second shows UI from before the Web 1.0 days – Robin Jan 10 '12 at 22:06
  • I implemented RangeSlider for filtering data (data_type/column/volatility selected in the JComboBox before) from JTable, so confortable and user_friendly way :-) endless users lazyzyness – mKorbel Jan 10 '12 at 22:24