B4J Question [B4J] How to take the right Tableview Selected Cell value

Nicola Ciaramellano

Member
Licensed User
Longtime User
Hi All,
I'm using a simple Tableview and I need to take the selected cell value. I used this code and it works perfect:

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private TableView1 As TableView
    Private Label1 As Label
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
    filltable
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

Sub filltable
    TableView1.SingleCellSelection=True
    TableView1.SetColumns(Array As String("Col1","Col2","Col3","Col4"))
    For i=1 To 100
        Dim row() As Object = Array As Object("Value C1: " & (i+10) , "Value C2: " & (i+20), "Value C3: " & (i+30),"Value C4: " & (i+40))
        TableView1.Items.Add(row)
    Next
End Sub

Sub TableView1_SelectedCellChanged (RowIndex As Int, ColIndex As Int, Cell As Object)
    Label1.Text=Cell
End Sub

But if I change the layout of the table dragging, for example, the last column in the first position at runtime, the label shows the wrong value if I click f.e. the first item.
Is there a way to avoid this problem?
Thanks!
 

Attachments

  • test.zip
    2.3 KB · Views: 264

Daestrum

Expert
Licensed User
Longtime User
Quick fix using JavaObject library
B4X:
Sub TableView1_SelectedCellChanged (RowIndex As Int, ColIndex As Int, Cell As Object)
' get real order of columns
 Dim cols As JavaObject = asJO(TableView1).RunMethod("getColumns",Null)
' read column ColIndex for cell at RowIndex 
Label1.Text = cols.RunMethodjo("get",Array(ColIndex)).RunMethod("getCellData",Array(RowIndex))
End Sub

Sub asJO(o As JavaObject) As JavaObject
 Return o
End Sub
 
Upvote 0

Nicola Ciaramellano

Member
Licensed User
Longtime User
I'm still working with this issue...

Daestrum sent me a quick fix, but I notice that this fix doesn't work always!
It's really strange because sometimes it works and sometimes it doesn't, apparently without a valid reason.
When it doesn't work, the runtime error is this:

main._tableview1_selectedcellchanged (java line: 141)
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4j.example.main._tableview1_selectedcellchanged(main.java:141)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.BA$1.run(BA.java:216)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ClassCastException: class java.lang.Integer cannot be cast to class [Ljava.lang.Object; (java.lang.Integer and [Ljava.lang.Object; are in module java.base of loader 'bootstrap')
at anywheresoftware.b4j.objects.TableViewWrapper$MyCellValueFactory.call(TableViewWrapper.java:211)
at anywheresoftware.b4j.objects.TableViewWrapper$MyCellValueFactory.call(TableViewWrapper.java:1)
at javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(TableColumn.java:593)
at javafx.controls/javafx.scene.control.TableColumnBase.getCellData(TableColumnBase.java:685)
... 19 more

I attached the sample source.
Hope someone can explain why... Thanks
 

Attachments

  • sample.zip
    2.4 KB · Views: 238
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The only way I could 'break' your example and get a similar error was to put an array of items into a cell in a column.
 
Last edited:
Upvote 0
Top