B4J Library jControlsFX Notifications Class

This is a class that uses ControlsFX Notifications - it's able to show desktop notifications. It depends on the JavaObject and jControlsFX library so be sure to include them.

Notifications.png
.

a little example of use:
B4X:
    Dim n As Notifications
    n.Initialize(Me, "Notification")
    n.Title("Test Title").Text("Test Text Test Text").Action3("Yes", "Yes", "No", "No", "Cancel", "Cancel").DarkStyle.Show

Private Sub Notification_Click()
    Log("Notification has been clicked")
End Sub
Private Sub Yes_Action()
    Log("Yes has been clicked")
End Sub
Private Sub No_Action()
    Log("No has been clicked")
End Sub
Private Sub Cancel_Action()
    Log("Cancel has been clicked")
End Sub

You can put this class into a new project which you can compile as a library if you do not like this as a class.

class Notifications:
B4X:
#Event: Click()
#Event: Action()
Sub Class_Globals
    Private fx As JFX
    Private joMe As JavaObject
    Private cCallbackModule As Object
    Private cClickEventName As String
    Private cAction1EventName As String
    Private cAction2EventName As String
    Private cAction3EventName As String
    Private joNotifications As JavaObject
    Private joDuration As JavaObject
    Private joPos As JavaObject
       
    Public Position_BASELINE_CENTER As String = "BASELINE_CENTER"
    Public Position_BASELINE_LEFT As String = "BASELINE_LEFT"
    Public Position_BASELINE_RIGHT As String = "BASELINE_RIGHT"
    Public Position_BOTTOM_CENTER As String = "BOTTOM_CENTER"
    Public Position_BOTTOM_LEFT As String = "BOTTOM_LEFT"
    Public Position_BOTTOM_RIGHT As String = "BOTTOM_RIGHT"
    Public Position_CENTER As String = "CENTER"
    Public Position_CENTER_LEFT As String = "CENTER_LEFT"
    Public Position_CENTER_RIGHT As String = "CENTER_RIGHT"
    Public Position_TOP_CENTER As String = "TOP_CENTER"
    Public Position_TOP_LEFT As String = "TOP_LEFT"
    Public Position_TOP_RIGHT As String = "TOP_RIGHT"
End Sub
'If you do not want to raise the click event when the user clicks on the notification 
'(in addition to the notification hiding, which happens whenever the notification is clicked on)
'provide empty string ("") as the ClickEventName and/or Null as CallbackModule
Public Sub Initialize(CallbackModule As Object, ClickEventName As String)
    joMe = Me
   
    cCallbackModule = CallbackModule
    cClickEventName = ClickEventName
   
    joNotifications.InitializeStatic("org.controlsfx.control.Notifications")
    joDuration.InitializeStatic("javafx.util.Duration")
    joPos.InitializeStatic("javafx.geometry.Pos")
   
    joNotifications = joNotifications.RunMethodJO("create", Null)
End Sub
'Specify one action that should be shown in the notification as button.
'You should not call any other Action sub after this as the next call will override this call
Public Sub Action(Action1Name As String, Action1EventName As String) As Notifications
    Return Action3(Action1Name, Action1EventName, "", "", "", "")
End Sub
'Specify two actions that should be shown in the notification as buttons.
'You should not call any other Action sub after this as the next call will override this call
Public Sub Action2(Action1Name As String, Action1EventName As String, Action2Name As String, Action2EventName As String) As Notifications
    Return Action3(Action1Name, Action1EventName, Action2Name, Action2EventName, "", "")
End Sub
'Specify three actions that should be shown in the notification as buttons.
'You should not call any other Action sub after this as the next call will override this call
Public Sub Action3(Action1Name As String, Action1EventName As String, Action2Name As String, Action2EventName As String, Action3Name As String, Action3EventName As String) As Notifications
    Dim a As List
    a.Initialize
    If Action1Name.Length > 0 And Action1EventName.Length > 0 Then
        cAction1EventName = Action1EventName
        Dim a1 As JavaObject
        a1.InitializeNewInstance("org.controlsfx.control.action.Action", Array(Action1Name, joNotifications.CreateEvent("java.util.function.Consumer", "Action1", Null)))
        a.Add(a1)
    End If
    If Action2Name.Length > 0 And Action2EventName.Length > 0 Then
        cAction2EventName = Action2EventName
        Dim a2 As JavaObject
        a2.InitializeNewInstance("org.controlsfx.control.action.Action", Array(Action2Name, joNotifications.CreateEvent("java.util.function.Consumer", "Action2", Null)))
        a.Add(a2)
    End If
    If Action3Name.Length > 0 And Action3EventName.Length > 0 Then
        cAction3EventName = Action3EventName
        Dim a3 As JavaObject
        a3.InitializeNewInstance("org.controlsfx.control.action.Action", Array(Action3Name, joNotifications.CreateEvent("java.util.function.Consumer", "Action3", Null)))
        a.Add(a3)
    End If
   
    If a.Size > 0 Then
        joMe.RunMethod("addAction", Array(a))
    End If
    Return Me
End Sub
#If JAVA
public void addAction(java.util.ArrayList actionList) {
    /*ArrayList to Array Conversion */
    org.controlsfx.control.action.Action actionArray[] = new org.controlsfx.control.action.Action[actionList.size()];
    for(int j=0;j<actionList.size();j++){
        actionArray[j] = (org.controlsfx.control.action.Action)actionList.get(j);
    }
    _jonotifications.setObject( ((org.controlsfx.control.Notifications)_jonotifications.getObject()).action(actionArray) );
}
#End If
'Specify that the notification should use the built-in dark styling, rather than the default 'modena' notification style (which is a light-gray).
Public Sub DarkStyle() As Notifications
    joNotifications = joNotifications.RunMethodJO("darkStyle", Null)
    Return Me
End Sub
'Specify the graphic to show in the notification.
Public Sub Graphic(GraphicNode As Node) As Notifications
    joNotifications = joNotifications.RunMethodJO("graphic", Array(GraphicNode))
    Return Me
End Sub
'Specify the duration that the notification should show, after which it will be hidden.
Public Sub HideAfter(Milliseconds As Int) As Notifications
    Dim sDuration As Double = Milliseconds
    joNotifications = joNotifications.RunMethodJO("hideAfter", Array(joDuration.RunMethod("millis", Array(sDuration))))
    Return Me
End Sub
'Specify that the close button in the top-right corner of the notification should not be shown.
Public Sub HideCloseButton() As Notifications
    joNotifications = joNotifications.RunMethodJO("hideCloseButton", Null)
    Return Me
End Sub
'The dialog window owner - which can be Screen, Window or Node.
Public Sub Owner(OwnerObject As Object) As Notifications
    joNotifications = joNotifications.RunMethodJO("owner", Array(OwnerObject))
    Return Me
End Sub
'Specify the position of the notification on screen, by default it is bottom-right.
Public Sub Position(PositionField As String) As Notifications
    joNotifications = joNotifications.RunMethodJO("position", Array(joPos.RunMethod("valueOf",Array(PositionField))))
    Return Me
End Sub
'Specify the text to show in the notification.
Public Sub Text(MainText As String) As Notifications
    joNotifications = joNotifications.RunMethodJO("text", Array(MainText))
    Return Me
End Sub
'Specify the title to show in the notification.
Public Sub Title(TitleText As String) As Notifications
    joNotifications = joNotifications.RunMethodJO("title", Array(TitleText))
    Return Me
End Sub
'Instructs the notification to be shown.
Public Sub Show()
    SetOnActionAndShow("show")
End Sub
'Instructs the notification to be shown, and that it should use the built-in 'confirm' graphic.
Public Sub ShowConfirm()
    SetOnActionAndShow("showConfirm")
End Sub
'Instructs the notification to be shown, and that it should use the built-in 'error' graphic.
Public Sub ShowError()
    SetOnActionAndShow("showError")
End Sub
'Instructs the notification to be shown, and that it should use the built-in 'information' graphic.
Public Sub ShowInformation()
    SetOnActionAndShow("showInformation")
End Sub
'Instructs the notification to be shown, and that it should use the built-in 'warning' graphic.
Public Sub ShowWarning()
    SetOnActionAndShow("showWarning")
End Sub
Private Sub SetOnActionAndShow(ShowMethod As String)
    If cCallbackModule <> Null And cClickEventName.Length > 0 Then
        joNotifications = joNotifications.RunMethodJO("onAction", Array(joNotifications.CreateEvent("javafx.event.EventHandler", "OnAction", Null)))
    End If
    joNotifications.RunMethod(ShowMethod, Null)
End Sub
Private Sub OnAction_Event (MethodName As String, Args() As Object) As Object
    CallSubDelayed(cCallbackModule, cClickEventName & "_Click")
    Return Null
End Sub
Private Sub Action1_Event (MethodName As String, Args() As Object) As Object
    CallSubDelayed(cCallbackModule, cAction1EventName & "_Action")
    Return Null
End Sub
Private Sub Action2_Event (MethodName As String, Args() As Object) As Object
    CallSubDelayed(cCallbackModule, cAction2EventName & "_Action")
    Return Null
End Sub
Private Sub Action3_Event (MethodName As String, Args() As Object) As Object
    CallSubDelayed(cCallbackModule, cAction3EventName & "_Action")
    Return Null
End Sub
 

Cableguy

Expert
Licensed User
Longtime User
Can an image be added to the notification?
 

Cableguy

Expert
Licensed User
Longtime User
Yes by using the Graphic method and providing an Object that extends a Node ... so basically any control (I think).
I am a complete null with javaobject, can you provide a simple example?
 

mindful

Active Member
Licensed User
I am a complete null with javaobject, can you provide a simple example?
This has nothing to do with JavaObject.

You create a ImageView with your image and provide that ImageView to the Notifications Graphic method, before you call any of the Show methods
B4X:
'Specify the graphic to show in the notification.
Public Sub Graphic(GraphicNode As Node)

The ImageView as you can see here: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView.html extends a Node:
public class ImageView
extends Node
The ImageView is a Node used for painting images loaded with Image class.

So in conclusion you can provide any kind of control that extends a Node (ImageView, Label, etc.) to the Graphic method and it will be shown.


I will provide an example when I get to the pc and reopen the project. Until then you should try it ;)
 
Top