B4J Question MouseClicked on Table

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
How I can get the row data in TableView clicking on it with mouse?
I would like to have the same result that you get with SelectedRowChanged.
This event (SelectedRowChanged) need for other purposes.
Thanks
 

stevel05

Expert
Licensed User
Longtime User
Yes, to capture the mouse click on a row you would have to Create a RowFactory and populate the table in a completely different way. You could also catch the mouseclick on the Tableview (anywhere on it) and calculate the row from the mouse click position. Or the simplest would be to record the click using an EventFilter which fires before the SelectedRowChanged sub, then when the SelectedRowChanged Event fires you will know if the mouse was pressed or not.

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TableView1 As TableView
    Private MousePressed As Boolean
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
 
    TableView1.SetColumns(Array As String("Col1","Col2"))
 
    Dim L As List
    L.Initialize
    For i = 0 To 10
        L.Add(Array As String("C1-"&i,"C2-" & i))
    Next
 
    TableView1.Items.AddAll(L)
 
    Dim TVJO As JavaObject = TableView1
 
    Dim MouseEvent As JavaObject
    MouseEvent.InitializeStatic("javafx.scene.input.MouseEvent")
    Dim O As Object = TVJO.CreateEventFromUI("javafx.event.EventHandler","TVPressed",Null)
    TVJO.RunMethod("addEventFilter",Array(MouseEvent.GetField("MOUSE_PRESSED"),O))

End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Private Sub TVPressed_Event (MethodName As String, Args() As Object) As Object
    MousePressed = True
End Sub

Sub TableView1_SelectedRowChanged(Index As Int, Row() As Object)
    Log(Row)
    If MousePressed Then
        MousePressed = False
        Log("Selected Changed By Mouse Click")
    Else
        Log("Selected Changed Other")
    End If
End Sub

The layout just holds a TableView.
 
Upvote 0
Top