Using JSlider to create a date chooser? - java

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:
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?

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.

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)

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
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
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
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)

Related

OHLCDataset: Get item from/having the date

I have a JFreechart, showing stocks data, the type of dataset i use to plot it is an OHLCDataset.
I can get the actual X value from the point where the user click on the plot (i mean, i get the real date corresponding to that point, not just the coordinate on the window).
Next step i need to make is get the data from the OHLCDataset corresponding to that date, to be able to get the Open.High,Close and Low values in that date, but i just can find ways to get the OHLCDataset date corresponding to an item (an integer wich indicates the ordinal), not even one way to obtain that item having the date.
¿Any ideas of how to get the item having the date?
Thanks.
The approach suggested is tedious and error-prone. Instead, add a ChartMouseListener, as shown here. You can invoke getDataset() on any XYItemEntity you encounter.

Java SWT DateTime - Get Selection?

I've got a DateTime (SWT.TIME) and want to know if the hours or minutes or seconds are selected.
I would like to get this information because I want that the User can scroll the mouse wheel in order to change the selected values accordingly.
Unfortunately I couldn't find any way to get the selected element of the DateTime Control. Does anybody have an idea?
DateTime provides many methods for getting the selected date. Like getHours, getMinutes, getDay, and so on. It's unclear to me what's your exact question, but appropiate setters are there as well.

Swing time picker

Im looking for a pretty and decent time picker component. There are a lot of alternatives for date picking on Swing but no for time.
I've seen nice Date/Time components picking on JQuery ( for example: http://trentrichardson.com/examples/timepicker/ ). There is something similar on Swing?
Thanks in advance.
Use JSpinner with SpinnerNumberModel
I would suggest the TimePicker component of the LGoodDatePicker library. The time can be chosen with the (default) "drop down" menu, with (optional) "spinner" style buttons, or both.
Fair disclosure: I'm the primary developer.
The TimePicker can be customized with optional settings. A few of the settings are the language (locale), default times in the drop down menu, fonts and colors, display/menu/parsing formats, 12 or 24 hour clock, seconds or nanoseconds precision, and so forth.
The library also includes the DatePicker and DateTimePicker components. All three components are easy to use. (They can each be instantiated with a single line of code.)
I've pasted screenshots of the components and the demo application below.
Project Home page: https://github.com/LGoodDatePicker/LGoodDatePicker .
( Click to enlarge the demo screenshot. )
I think you will like the ease of JCalendar. It offers a JDateChooser, a JDayChooser or a JSpinnField, written by Kai Toedter', available here: JCalendar 1.4.
You can get your dates like this:
java.util.Date fromDate = jDateChooser1.getDate();
JSpinnField lets you set max and min values easily:
jSpinField1.setMaximum(59);
jSpinField1.setMinimum(0);

Netbeans GUI Spinner - only allow it to go so high

I want to restrict the spinner to go from 0 to 59.
How can I do this?
Use the same general process I described in my answer to your similar question about combo boxes. This will be a little more straight forward if your numbers are fixed:
Edit the model property for the spinner in the properties editor.
Select Number for the Mode type
Edit the fields as necessary
Enjoy!
Give your JSpinner a SpinnerNumberModel and construct the SpinnerNumberModel with parameters that fulfill your criteria. e.g.,
// if the initial value will be 30, then this will set the spinner to
// initialize at 30, have a range from 0 to 59, and a step size of 1
SpinnerNumberModel spinnerNumberModel = new SpinnerNumberModel(30, 0, 60, 1);
spinner.setModel(spinnerNumberModel);
Also, on a side note, I strongly urge you to learn to code Swing without the use of NetBeans-generated code as this will help you immensely in understanding what Swing is doing under the hood and how to best code in Swing with or without NetBeans generation.
A note. Net beans will not let you edit this code yourself. However if you go to the model and choose number you can specify the values that you want.

Java Swing - Problem in JSpinner

I am developing a Java Desktop Application and designing the GUI with the help of Netbeans Swing GUI builder.
I want to use a JSpinner in my app. I have dragged and dropped it to a JPanel. Now, I want to set its two properties:
First, It should display numbers in the range of 1 to 50. Neither less than 1 nor greater than 50. How can I set that range?
Second, when I try to get the value of it by spinner.getValue() it returns an Object. As my spinner's data type is Integer, would it be better to downcast the Object into Integer or Is there any other way to get that numeric value?
Create a SpinnerNumberModel, this should solve all your problems.
SpinnerNumberModel model =
new SpinnerNumberModel(int initialValue, int minValue, int maxValue, int step)
For further information I recommend reading How to Use Spinners
From here, the way to do this in NetBeans:
Create the JSpinner, as you have done.
Right click on it and select "Customize Code"
Set the initialization to be a spinner with a SpinnerNumberModel.
int myInt = (Integer)mySpinner.getValue();
Java has autoboxing for primitive data types, so the above code will get your spinner value as an integer, as long as you use the SpinnerNumberModel as suggested by Ham.
Ham is correct on your first question (how to limit the range of 1 to 50). For the second question, yes, you can simply cast it. Most (if not all) swing components return an Object for their value (the only notable exception being text fields).
Read the section from the Swing tutorial on "How to Use Spinners". And don't forget to check out the rest of the table of contents for Swing basics.

Categories