1

Is there a way to have a textbox that validates with the format dd/mm/yyyy and doesn't allow any other characters? I've managed to validate it so it's only numbers but having numbers and slashes is proving to be an issue.

I am using JAVAFX.

david
  • 852
  • 1
  • 7
  • 22
Dylan Benton
  • 109
  • 1
  • 11

1 Answers1

4

I have created a date control which is based on a textbox: https://github.com/nablex/jfx-control-date/ It allows you to set a format (supported by SimpleDateFormat) and supports a popup for mouse selection.

You can also enter values by typing (only valid values are allowed) and browse the fields with the arrow buttons (left & right will browse, up & down will increase/decrease).

Some example code (can also be found in the test class on github):

DatePicker picker = new DatePicker();

// you may not want the controls to manipulate time, they are on by default however
picker.setHideTimeControls(true);

// optional: the format you want the date to be in for the user
picker.formatProperty().setValue("yyyy/MM/dd HH:mm:ss.SSS");

// optional: set timezone
picker.timezoneProperty().setValue(TimeZone.getTimeZone("CET"));

// optional: set locale
picker.localeProperty().setValue(new Locale("nl"));

// react to changes
picker.timestampProperty().addListener(new ChangeListener<Long>() {
    @Override
    public void changed(ObservableValue<? extends Long> arg0, Long oldValue, Long newValue) {
        // do something
    }
});

UPDATE

Filter logic was added. If you set a filter, you can limit the dates that are acceptable for the user to enter. Unacceptable dates will be greyed out in the GUI and the user will also be prevented from entering them manually in the text field.

For example this filter will block any dates before a random point in time:

    picker.filterProperty().setValue(new DateFilter() {
        @Override
        public boolean accept(Date date) {
            SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
            try {
                return date.after(parser.parse("2010-07-13"));
            }
            catch (ParseException e) {
                return false;
            }
        }
    });
nablex
  • 4,635
  • 4
  • 36
  • 51
  • how to use this with the javaFx? how to popup? which listener and which component is worked? – Java Man Mar 04 '14 at 06:10
  • 1
    The DatePicker class extends TextField so you can use it wherever and however you would use a TextField. The popup is activated when you click it with the mouse but you could also have it popup when it is focused (this was actually initially the case but I myself rarely used the popup so I made it less intrusive). In the DatePicker class you can see on line 185 how the popup is activated on click. – nablex Mar 04 '14 at 07:00
  • @user1109519: Thanks and how to get selected date in console? – Java Man Mar 04 '14 at 07:14
  • `picker.getDate()` gave me value of today but i want value which is I choose? then is it possible? – Java Man Mar 04 '14 at 07:23
  • 1
    You can use "timestampProperty()" which is a long (due to immutability) or you can use the convenience method "DatePicker.setDate()" to set a custom date. There is also a setCalendar() convenience method. (note that the latter also updates the timezone though) – nablex Mar 04 '14 at 07:29
  • @user1109519: no man I want to select 5th of march then how to get selected date in console? from datepicker? Is there any method available like pikcer.getselectedDate() or something which give me selected date in any format? long or anything? – Java Man Mar 04 '14 at 07:31
  • I'm not entirely sure what you mean. picker.getDate() will return whatever value is currently set by the user. If you want to initialize it to something other than the current date (which is the default) you need to use setDate() or update the timestamp property. Could you clarify what exactly you want to do that can not be done with these methods? – nablex Mar 04 '14 at 07:33
  • @user1109519: I get it man using your `timestampproperty listener` :) can I disable some dates to select from the user? – Java Man Mar 04 '14 at 08:40
  • if use two datepicker in one form then how to restrict second datepicker? for selecting date only after the dates which is select by first datepicker? For Ex. I choose 10th jan 2014 then second date picker starting from 11th jan 2014 OR from starting to 10th jan 2014 all dates are disable in second datepicker? Is it possible in this picker? as We can do it in Simple Core JAVA swing JDateChooser. – Java Man Mar 04 '14 at 08:42
  • Hmm, there is currently no way to filter out specific dates though I can see why this would be a nice feature addition. It should be rather trivial to implement. Perhaps you can set a listener on timestampProperty and roll back any change that results in an invalid date? E.g.: if (isBlacklisted(newValue)) rollback(oldValue); – nablex Mar 04 '14 at 08:43
  • I haven't worked much with swing so I didn't know it had that feature. It should be implementable using the same logic I outlined in my previous comment. – nablex Mar 04 '14 at 08:45
  • Thanks man and hope you enhance this feature soon. and there is no method found for isblacklisted and rollback? – Java Man Mar 04 '14 at 08:49
  • @user1109519: nope man. this is the feature of jdatechooser component not the swing.. so we can't use that logic overhere.. I think setrange() is something like that? – Java Man Mar 04 '14 at 08:50
  • I meant to say it is trivial to implement your own blacklisting. If you subscribe to changes from the timestampProperty() you can run **your own** isBlacklisted(newValue) method and if you determine that it is not valid, you can roll back the change. If you want to make the "validness" dependent on another date picker, this logic is trivial to implement in said custom isBlacklisted() method. – nablex Mar 04 '14 at 08:54
  • @user1109519: so we can disable button in this datepicker? using some logic code? – Java Man Mar 04 '14 at 08:58
  • 1
    Ah ok you want to visually disable the dates. No this is not possible at this time without updating the control itself. – nablex Mar 04 '14 at 09:07
  • @user1109519: yeah I want to disable visually. Anyway thanks for your support and for effort. Really appreciated. – Java Man Mar 04 '14 at 09:29
  • Because it is a nice to have feature, I added some code to enable a DateFilter. You can set it using the new filterProperty() of the DatePicker. It will enforce this filter when entering manually, using arrow keys or using the GUI. – nablex Mar 04 '14 at 11:04
  • @user1109519: have you added filtering code for disabling dates? – Java Man Mar 05 '14 at 04:58
  • 1
    Yes, you can now use DatePicker.filterProperty() to set a DateFilter object. The DateFilter is an interface with an accept(Date) interface. An example is also included in the test file. It will visually disable unacceptable dates and prevent the user from entering it manually in the text field. – nablex Mar 05 '14 at 05:58
  • @user1109519: so I have to update my `Datepicker.java` file right? so where to get copy of your new file? – Java Man Mar 05 '14 at 06:04
  • I would redownload the whole project. The test file was updated, DatePicker, the PopupCalendar and a new interface was added. – nablex Mar 05 '14 at 06:05
  • @user1109519:I redownload the whole project but it accept only string format? and I want to pass specific date or long format? then how it is possible? – Java Man Mar 05 '14 at 06:18
  • Are you talking about the new filterProperty()? I shall add an update to the answer above. – nablex Mar 05 '14 at 06:37
  • @user1109519: hey its working with me. it disable all the date but it gives me stackoverflow error. first time it does not disable at the second time click on any date at that time listener will called and disable all dates.. – Java Man Mar 05 '14 at 06:40
  • Can you give me an example that is triggering the stack overflow? There are protections against stack overflow for invalid initial dates but perhaps I overlooked a specific case. – nablex Mar 05 '14 at 06:48
  • @user1109519: in the Datepicker `intialize()` when `textProperty().addListener` called `if (filter.getValue().accept(getDate())) setText(oldValue);` here error occurs..:( – Java Man Mar 05 '14 at 06:52
  • hey is there any solution for this? `@Override protected String getUserAgentStylesheet() { return DatePicker.class.getClassLoader().getResource("jfx-date-picker.css").toExternalForm(); }` because everytime I manually pur css in classes. and when i close the editor and open it again I must put css again in that folder.. otherwise give me nullpointer exception :( – Java Man Mar 05 '14 at 07:08
  • @user1109519: this is my listener. please check where I did mistake? http://pastie.org/8865203 – Java Man Mar 05 '14 at 07:27
  • I don't see any particular problem with it, can you give me more information regarding the stack overflow? – nablex Mar 05 '14 at 07:30
  • I give you my code where i get error.. so check it at your side? can you disable the second datepicker dates? as per selection of first picker? when I am doing this it gives me that error.. and after two time click on the picker after that dates disable.. – Java Man Mar 05 '14 at 07:33
  • Instead of recreating a new date filter every time, would this not be better? http://pastie.org/8865225 – nablex Mar 05 '14 at 07:42
  • @user1109519: Still having problem; it disable after clicking on any date.. so it set the selected date though it is not correct date.. For eg: I selected 15 jun in first picker and in second picker i can select 12 Jun and after the select 12 Jun it disable starting to 14th June.. and in the text box incorrect value that is 12Jun :( – Java Man Mar 05 '14 at 08:36
  • @user1109519: I solved this issue man. Using setDate(). Thanks.. but what about my css problem? can you give me solution for that? – Java Man Mar 05 '14 at 09:06
  • 1
    I'm glad you solved the problem. I actually had completely missed your css comment but looking at it now I'm not sure what you mean. The css file mentioned here is available in the project so it should be in the jfx-date-picker.jar file and available. Why is it being removed? – nablex Mar 05 '14 at 09:10
  • what? where is the jar file of your project? I used your java file to serve my purpose? – Java Man Mar 05 '14 at 09:14
  • No problem. I meant to say that in your final project you should not include the source code from github in your own jar but rather package the github code into its own jar and put it on the classpath. The resulting jar will contain the css file. (PS: you have to build the jar yourself, you can run "mvn package" to do this) – nablex Mar 05 '14 at 09:22
  • @user1109519: I can not get it what I am suppose to do? where to put your css file? – Java Man Mar 05 '14 at 09:26
  • 1
    I use maven for lifecycle management. If you know and have maven you can easily run "mvn package" in the root folder which will result in a packaged jar that you can put on your classpath. If you don't have maven you will have to use your own environment to package it (e.g. export in eclipse). I would highly recommend reading up on maven, it doesn't take long to understand and it's immensely powerful. – nablex Mar 05 '14 at 09:35
  • @user1109519: okay man thanks.. I will go through the docs of using maven. at that putting css in the class folder manually.. – Java Man Mar 05 '14 at 09:46