### Customized Listview with Hyperlink

To add Listview items with Hyperlinks, make use of JavaObject Hyperlink.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Hyperlink.html
The Hyperlink is added to a ListView entry as a JavaObject plus and Eventhandler for MouseClicked.
B4J Code
`Private Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private lvwMain As ListView
End Sub`
`Private Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Main")
MainForm.SetFormStyle("UTILITY")
MainForm.Resizable = False
MainForm.Title = "B4j HowTo Hyperlink ListView"
MainForm.Show
'Add the items to the listview: line1 is a label, line2 is the hyperlink
ListViewAdd2Lines(lvwMain, "MySite", "http://mysite", "0")
ListViewAdd2Lines(lvwMain, "B4X Homepage", "http://www.b4x.com", "1")
End Sub`
'Add two lines to the listview:
'Line 1 is a Label
'Line 2 is a Hyperlink declared as JavaObject
`Sub ListViewAdd2Lines(lv As ListView, Line1 As String, Line2 As String, Value As Object)
'The anchorpane to hold the nodes
Dim ap As AnchorPane
ap.Initialize("")
'Node 1 = Label
Dim lbl1 As Label
lbl1.Initialize("lbl1")
lbl1.Text = Line1
lbl1.Font = fx.DefaultFont(16)
lbl1.Tag = Value
'Node 2 = Hyperlink
Dim joHL As JavaObject
'Create the instance with the URL visible
joHL.InitializeNewInstance("javafx.scene.control.Hyperlink", Array(Line2))
joHL.RunMethod("setFont", Array(fx.DefaultFont(12)))
'Set the value of the listview line as id - this could be used to set the line as selected when clicking on the hyperlink
joHL.RunMethod("setId", Array(Value))
'Add a mouseclicked eventhandler which opens the URL in the default browers
Dim oHL As Object = joHL.CreateEvent("javafx.event.EventHandler","HyperLinkMouseClicked",False)
joHL.RunMethod("setOnMouseClicked",Array(oHL))
'Add the 2 nodes
ap.AddNode(lbl1, 0, 0, lv.Width, 20dip)
ap.AddNode(joHL, 0, 25dip, lv.Width, 20dip)
lv.Items.Add(ap)
End Sub`
'Handle mouseclicked event on the hyperlink
'Get the text from the hyperlink node and open in the default browser
`Private Sub HyperlinkMouseClicked_Event(EventName As String,Args() As Object) As Object
Dim MEvent As Event = Args(0)
'Assign the argument, which is a JavaObject to a new JavaObject
Dim joHL As JavaObject = Args(0)
'To understand the makeup of the JavaObject = the mouseclicked event
'(MouseEvent) MouseEvent [source = Hyperlink[id=0, styleClass=hyperlink]'http://www.mysite', target = Text[text="http://www.mysite", x=0.0, y=0.0, alignment=LEFT, origin=BASELINE, boundsType=LOGICAL_VERTICAL_CENTER, font=Font[name=System Regular, family=System, style=Regular, size=10.0], fontSmoothingType=LCD, underline, fill=0x333333ff], eventType = MOUSE_CLICKED, consumed = false, x = 54.0, y = 10.0, z = 0.0, button = PRIMARY, pickResult = PickResult [node = Text[text="http://mysite... Log(joHL)
'Get the hyperlinktext which is the url - look at previous JavaObject makeup target = Text
Log(joHL.RunMethodJO("getTarget", Null).RunMethod("getText", Null))
fx.ShowExternalDocument(joHL.RunMethodJO("getTarget", Null).RunMethod("getText", Null))
'This will give you the Id
Log(joHL.RunMethodJO("getSource", Null).RunMethod("getId", Null))
'Consume the event
MEvent.Consume
Return False
End Sub`
Layout Nodes
ListView lvwMain
Style main.css
This external style sheet is used:
.list-cell:selected {
-fx-background-color: lightgrey;
}
Add to the form, i.e.
MainForm.Stylesheets.Add(File.GetUri(File.DirAssets, "main.css"))

Changed: 2018-06-07 09:16, Category: ListView, File: T6C216M1585.htm, ID: 1585