### Set & Get DatePicker Properties

As from B4J v4.00 the B4J Visual Designer includes a DatePicker View.
Various subroutines (using in cases API methods) to set or get properties of the DatePicker (added using the B4J Visual Designer).
Reference: http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/DatePicker.html
`Sub Process_Globals
Private DatePicker1 As DatePicker
...
End Sub`
'Set a date year, month, day
'Example: SetDate(2018,1,1)
`Sub SetDate(dp As DatePicker, y As Int, m As Int, d As Int)
'Use the library DateUtils to set a date in the datepicker
dp.DateTicks = DateUtils.SetDate(y,m,d)
End Sub`
'Set the DatePicker Date to Now using SetDate
`Sub SetDateNow(dp As DatePicker)
SetDate(dp, DateTime.GetYear(DateTime.Now),DateTime.GetMonth(DateTime.Now),DateTime.GetDayOfMonth(DateTime.Now))
End Sub`
'Set the DatePicker Date to Now using DateUtils library
`Sub SetDateNow2(dp As DatePicker)
'Use the library DateUtils to set a date in the datepicker
dp.DateTicks = DateUtils.SetDate(DateTime.GetYear(DateTime.Now),DateTime.GetMonth(DateTime.Now),DateTime.GetDayOfMonth(DateTime.Now))
End Sub`
'Returns the DatePicker date in format as set by DateTime.DateFormat
`Sub GetDate(dp As DatePicker) As String
Return DateTime.Date(DatePicker1.DateTicks)
End Sub`
'Returns the DatePicker date in format as set by the parameter format
`Sub GetDate2(dp As DatePicker, format As String) As String
Dim result As String
Dim curformat As String = DateTime.DateFormat
DateTime.DateFormat = format
result = DateTime.Date(DatePicker1.DateTicks)
DateTime.DateFormat = curformat
Return result
End Sub`
'Returns the DatePicker date in format as set by DatePicker.DateFormat
`Sub GetDate3(dp As DatePicker) As String
'Declare the JavaObject as assign the DataPicker node
Dim jo As JavaObject = dp
'Get the value using the getValue()
Return jo.RunMethod("getValue", Null)
End Sub`
'Show the Weeknumbers
`Sub ShowWeekNumbers(dp As DatePicker)
Private joDatePicker As JavaObject
joDatePicker = dp
joDatePicker.RunMethod("setShowWeekNumbers", Array As Object(True))
End Sub`
(Not Recommended) API methods
This is just to show using JavaObjects, but can also be handled using the DateUtils Library (see previous).
`Sub Process_Globals
Private DatePicker1 As DatePicker
...
End Sub`
Set the actual date as default date
Private joDatePicker As JavaObject = DatePicker1
Dim joLD As JavaObject
joLD.InitializeStatic("java.time.LocalDate")
' Use the "now" API method
joDatePicker.RunMethod("setValue", Array As Object(joLD.RunMethod("now", Null)))
Set a date as default date
Private joDatePicker As JavaObject = DatePicker1
Dim joLD As JavaObject
joLD.InitializeStatic("java.time.LocalDate")
' Use the "of" API method: Parameter Year, Month, Day
joDatePicker.RunMethod("setValue", Array As Object(joLD.RunMethod("of", Array As Object(2013, 6,7))))
Set a date as value using a sub
' Set the datevalue of a javafx 8 datepicker.
' Parameter: dp as the datepicker node, d as the date in format yyyy-mm-dd, like 2014-07-12
' Reference: http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/DatePicker.html
`Sub setValueDP8(dp As Node, d As String)
If d.Length = 0 Then Return
' Split the date in year, month, day as int - required for the datelocale of the method
Dim ds() As String = Regex.Split("-", d)
Dim yy As Int = ds(0)
Dim mm As Int = ds(1)
Dim dd As Int = ds(2)
Dim joDatePicker As JavaObject = dp
Dim joLD As JavaObject
' Get the locale
joLD.InitializeStatic("java.time.LocalDate")
' Use the "of" API method: Parameter Year, Month, Day
joDatePicker.RunMethod("setValue", Array As Object(joLD.RunMethod("of", Array As Object(yy,mm,dd))))
End Sub`
Other examples using LocaleDate:
//Setting a particular date value by using the setValue()
checkInDatePicker.setValue(LocalDate.of(1958, 08, 9));
//Setting the minimum date available in the calendar
checkInDatePicker.setValue(LocalDate.MIN);
//Setting the maximum date available in the calendar
checkInDatePicker.setValue(LocalDate.MAX);
//Setting the current date
checkInDatePicker.setValue(LocalDate.now());

Changed: 2018-04-21 14:54, Category: DatePicker JavaFX 8, File: T6C182M1313.htm, ID: 1313