B4J Code Snippet Java 27

Found a fun thing in java27 - all the hours spent trying to make title bar of form look cool - they coded it now

1793980628416.png


Example code - (crappy code was just testing it)

Code: Copy
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI 
    Private Button1 As B4XView
    Dim button2 As TextField
    Dim HeaderBar As JavaObject
    Dim la As Label
    Dim p As Pane
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("EXTENDED")
    MainForm.RootPane.LoadLayout("Layout1")
    la.Initialize("la")
    la.Text = "Search here"
    button2.Initialize("b2")
    button2.Text = "?"
    button2.SetSize(120,25)
 
    p.Initialize("p") 
    
    p.AddNode(button2,70,0,-1,-1)
    p.AddNode(la,-1,-1,-1,-1)
    HeaderBar.InitializeNewInstance("javafx.scene.layout.HeaderBar",Null)
    HeaderBar.RunMethod("setCenter",Array(p))
    HeaderBar.Runmethod("setStyle",Array("-fx-background-color: linear-gradient(to right, pink, lightgreen);"))
    MainForm.RootPane.AddNode(HeaderBar,0,0,MainForm.Width,-1)
    MainForm.Show
End Sub
 
Minor addition - as pane covers headerBar you lose drag of window (a bit) quick fix - needs improving as window jumps on start of move

Code: Copy
Sub P_MouseDragged (EventData As MouseEvent)
    MainForm.WindowLeft = EventData.As(JavaObject).RunMethod("getScreenX",Null) - MainForm.Width/2
    MainForm.WindowTop = EventData.As(JavaObject).RunMethod("getScreenY",Null)
End Sub
 
In your example you could make the label and/or any other static nodes mouse transparent lbl.as(JavaObject).RunMethod("setMouseTransparent",Array(True)) then you should be able to drag on the label / node.
 
I made a wrapper lib for the preview version, when I get a bit of time I'll have a look and see if anything has changed. It worked with Java 25.
 
Tried it - dragging works , but if you have textfield in bar - you cant click on it to enter things.

this sorts the jump out (dx & dy global vars)

Code: Copy
Sub p_MousePressed (EventData As MouseEvent)
    dx = (EventData.As(JavaObject).RunMethod("getScreenX", Null) - MainForm.WindowLeft)
    dy = (EventData.As(JavaObject).RunMethod("getScreenY", Null) - MainForm.WindowTop)
End Sub
Sub p_MouseDragged (EventData As MouseEvent)
    MainForm.WindowLeft = EventData.As(JavaObject).RunMethod("getScreenX",Null) - dx
    MainForm.WindowTop = EventData.As(JavaObject).RunMethod("getScreenY",Null) - dy
End Sub
 
Last edited:
Try this wrapper out, the only changes I could see were renaming of the headerbar areas. I haven't tried everything, but let me know if it doesn't work for some reason. There are a few 'B4x' utilities thrown in for good measure. Custom View and some useful Designer Script Extensions to simplify the creation of the HeaderBar. Have a look at the layout file. If it does what you want, I will release it as a B4xLib. You will likely need to change the #JavaCompilerPath in Main.

This makes me want to use Javafx 27 now.

For those that haven't see this before, Javafx27 needs to be run in conjunction with Java25 as a minimum.
 

Attachments

Last edited:
That works a treat - nice job.

I have no use for headerbar but curiosity got me when I saw in api's.
 
I will certainly make use of it, I was waiting for it to get out of preview, although it's still a little cutting edge for comfort if you are getting paid for your apps. It can be tricky to set up, which is why I wrote the custom view and designer scripts. Just takes the edge off a little.
 
Got me thinking - good place for a progress bar - always visible and easy to spot
 
Yep see, you might have a use for it after all. And for windows at least, you get access to the snap layouts.
 
Back
Top