Other Color Picker Class B4j 2.2+

Based on the new methods in JavaObject for Version 2.2, here is the javafx colorpicker control.

Pass a Pane into which to load the colorpicker and an initial color to the class.

A callback to {EventName}_ColorPicked(Color As Object) will be made when a color is selected.

Based on code found here.

B4X:
#Event: ColorPicked(Color As Object)
'Class module
Sub Class_Globals
    Private fx As JFX
    Private CP As JavaObject
    Private mModule As Object
    Private mEventName As String
End Sub

'Initializes the object.
'Pass a Pane to load the Color Picker into, Color is the initial color when the Color Picker is displayed
Public Sub Initialize(Module As Object,EventName As String,Pane1 As Pane,Color As Paint)

    'Store the calling object
    mModule = Module
    mEventName = EventName

    'Create the ColorPicker Object
    CP.InitializeNewInstance("javafx.scene.control.ColorPicker",Null)
    'Set it's initial Value
    CP.RunMethod("setValue",Array(Color))

    'Create the Event Handler
    Dim E As Object = CP.CreateEventFromUI("javafx.event.EventHandler","CP",Null)
    CP.RunMethod("setOnAction",Array(E))

    'Add the colorPicker
    Pane1.AddNode(CP,0,0,-1,-1)

End Sub
Sub CP_Event(MethodName As String,Args() As Object)
    'Pass the value back to the calling module
    If SubExists(mModule,mEventName & "_ColorPicked") Then
        CallSub2(mModule,mEventName & "_ColorPicked",CP.RunMethod("getValue",Null))
    End If
End Sub
Sub setColor(Color As Paint)
    CP.RunMethod("setValue",Array(Color))
    'Workaround for bug in ColorPicker that doesn't update the selected color
    Dim AJO As JavaObject
    AJO.InitializeNewInstance("javafx.event.ActionEvent",Null)
    CP.RunMethod("fireEvent",Array(AJO))
End Sub
 

Attachments

  • CP.zip
    1.9 KB · Views: 450
Last edited:

stevel05

Expert
Licensed User
Longtime User
I've added a setColor sub to the initial post, as there is a bug in the ColorPicker (JavaFX2) that doesn't display the color if it's not selected via the dialog. Using the sub this way should fix it so that you don't need to initialize a new instance to display a new color.
 

jmon

Well-Known Member
Licensed User
Longtime User
Thanks, It works very well, and it is much needed in one of my projects.

Is there any reason why it should be run under B4J 2.2? I found that the code can be easily modified to run with B4J 2.00. Just Change all the "Array" with "Array As Object":
B4X:
CP.RunMethod("setValue",Array As Object (Color))

Also, I had to change the sub "setColor(Color As Paint)" to "setColor(Color As Object)" to be able to input colors from jFX, such as "fx.Colors.Red".

So this is my modified code for B4J 2.00:
B4X:
#Event: ColorPicked(Color As Object)
'Class module
Sub Class_Globals
    Private fx As JFX
    Private CP As JavaObject
    Private mModule As Object
    Private mEventName As String
End Sub

'Initializes the object.
'Pass a Pane to load the Color Picker into, Color is the initial color when the Color Picker is displayed
Public Sub Initialize(Module As Object,EventName As String,Pane1 As Pane,Color As Paint)

    'Store the calling object
    mModule = Module
    mEventName = EventName

    'Create the ColorPicker Object
    CP.InitializeNewInstance("javafx.scene.control.ColorPicker",Null)
    'Set it's initial Value
    CP.RunMethod("setValue",Array As Object (Color))

    'Create the Event Handler
    Dim E As Object = CP.CreateEventFromUI("javafx.event.EventHandler","CP",Null)
    CP.RunMethod("setOnAction",Array As Object(E))

    'Add the colorPicker
    Pane1.AddNode(CP,0,0,-1,-1)

End Sub
Sub CP_Event(MethodName As String,Args() As Object)
    'Pass the value back to the calling module
    If SubExists(mModule,mEventName & "_ColorPicked") Then
        CallSub2(mModule,mEventName & "_ColorPicked",CP.RunMethod("getValue",Null))
    End If
End Sub
Sub setColor(Color As Object)
    CP.RunMethod("setValue",Array As Object(Color))
    'Workaround for bug in ColorPicker that doesn't update the selected color
    Dim AJO As JavaObject
    AJO.InitializeNewInstance("javafx.event.ActionEvent",Null)
    CP.RunMethod("fireEvent",Array As Object(AJO))
End Sub

Thanks again.
 

stevel05

Expert
Licensed User
Longtime User
No, no reason, I was getting muddled with B4a, which didn't have CreateEvent/CreateEventFromUI until the latest Beta.
 

jmon

Well-Known Member
Licensed User
Longtime User
...which didn't have CreateEvent/CreateEventFromUI until the latest Beta.
I didn't even notice that it was added. I just learnt something. Thanks for your code.
 

IanMc

Well-Known Member
Licensed User
Longtime User
Thanks guys.

I've stuck it on my muckabout app: IansB4J_app.jar

Is it possible to do something similar with the DatePicker in scenebuilder?
 

IanMc

Well-Known Member
Licensed User
Longtime User
I've managed to add the DatePicker by basically copying parrot fashion what you have done here but I don't know how to initialize it with a date.
Any ideas?
B4X:
#Event: DatePicked(Date As Object)
'Class module
Sub Class_Globals
    Private fx As JFX
    Private DP As JavaObject
    Private mModule As Object
    Private mEventName As String
End Sub

'Initializes the object.
'Pass a Pane to load the Date Picker into, Date is the initial Date when the Date Picker is displayed
Public Sub Initialize(Module As Object,EventName As String,Pane1 As Pane)

    'Store the calling object
    mModule = Module
    mEventName = EventName

    'Create the DatePicker Object
    DP.InitializeNewInstance("javafx.scene.control.DatePicker",Null)
    'Set it's initial Value
   ' DP.RunMethod("setValue",Array As Object(Date))

    'Create the Event Handler
    Dim E As Object = DP.CreateEventFromUI("javafx.event.EventHandler","DP",Null)
    DP.RunMethod("setOnAction",Array As Object(E))

    'Add the DatePicker
    Pane1.AddNode(DP,0,0,-1,-1)

End Sub
Sub DP_Event(MethodName As String,Args() As Object)
    'Pass the value back to the calling module
    If SubExists(mModule,mEventName & "_DatePicked") Then
        CallSub2(mModule,mEventName & "_DatePicked",DP.RunMethod("getValue",Null))
    End If
End Sub
Sub setDate(Date As Object)
    DP.RunMethod("setValue",Array As Object(Date))
    'Workaround for bug in ColorPicker that doesn't update the selected color
    Dim AJO As JavaObject
    AJO.InitializeNewInstance("javafx.event.ActionEvent",Null)
    DP.RunMethod("fireEvent",Array As Object(AJO))
End Sub
 
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi Ian,

an option could be to use a javaobject to set the new java 8 dateformat. Have tested below code and works fine.

In AppStart define:
B4X:
DateTime.DateFormat = "yyyy-MM-dd"
DP.Initialize(Me,"DP",pnlDP,DateTime.Date(DateTime.Now))

In Initialize add after AddNode
B4X:
Public Sub Initialize(Module As Object,EventName As String,Pane1 As Pane, Date As String)
...
    ' Set the initial date as provided by parameter date
    Dim joLD As JavaObject
    joLD.InitializeStatic("java.time.LocalDate")
    Try
        DP.RunMethod("setValue",Array As Object(joLD.RunMethod("parse", Array As Object(Date))))
    Catch
        Log(LastException.Message)
    End Try
End Sub

To set the current date without using the parameter date, use
B4X:
' set current date
DP.RunMethod("setValue",Array As Object(joLD.RunMethod("now", Null)))
 

IanMc

Well-Known Member
Licensed User
Longtime User
Yes it works! and it also fires the DP_DatePicked event though, is there a way to get it just to accept the date without firing that? and what about the setDate method? how will that work?

Cool :) thanks rwblinn

Here's all of my code so far...

added: oh, sorry :) should have read your post properly
B4X:
' set current date
DP.RunMethod("setValue",ArrayAsObject(joLD.RunMethod("now", Null)))
 

Attachments

  • IansB4J_app.zip
    32.4 KB · Views: 328
Last edited:

hookshy

Well-Known Member
Licensed User
Longtime User
I have test this lib ...if the panel is last on the left border of the screen ..the color palete is opening allwasy to the right so you can not chose a color anymore ...
same and more obvious with the modal color choser!

Can you help ?
 

ThRuST

Well-Known Member
Licensed User
Longtime User
How to put the selected color value from color picker example in post #2 into HTMLEditor forecolor property, anyone?

B4X:
Sub ColorPicked(Color As Object)
   
' How to set HTMLEditor forecolor from color value in color picker?
   
End Sub
 
Top