B4J Question How to get the value of the selected single cell in a tableview

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi All,

by default the cell selection is by row. Using below changes to selection by cell.
B4X:
Dim JO As JavaObject = tableview1
Dim JO2 As JavaObject = JO.RunMethod("getSelectionModel", Null)
JO2.RunMethod("setCellSelectionEnabled", Array As Object(True))

With the mouse or arrow keys the single cell is displayed :)

Question: How to get the value of the selected single cell by using mouseclick or using arrow keys?

I tried:
B4X:
tableviewMonth_SelectedRowChanged
but as expected the content of the whole row is returned.

then

B4X:
Dim JO As JavaObject = tableview1
Dim JO2 As JavaObject = JO.RunMethod("getSelectionModel", Null)
Dim r() As Object = JO2.RunMethod("getSelectedItem", Null)
For i = 0 To r.Length - 1
    Log(r(i))
Next
but again the whole row is returned although the single cell is shown as selected.

Looked up further for JavaAPI methods, but could not find a solution.

Appreciate any hints. Testproject attached.
 

Attachments

  • b4howtotesttv.zip
    1.6 KB · Views: 221

davelew1s

Active Member
Licensed User
Longtime User
That's great just what we need...Thanks Erel...Rob your part solution make the table look better when making a selection, so i'll be using it for now thanks.
Dave.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Thank You Erel and good to see of use by Dave.

Keep the good work on B4J.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
It can be done with
B4X:
tab1.getSelectionModel().getSelectedItem()[tab1.getSelectionModel().getSelectedCells().get(0).getColumn()]
but converting to JavaObject calls could be fun :)
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks for the suggestion = Indeed - I tried that direction also but receive an error message like:
B4X:
java.lang.IllegalAccessException: Class anywheresoftware.b4j.object.JavaObject can not access a member of class javafx.scene.control.TableView$TableViewArrayListSelectionModel with modifiers "public"

using your code --- but also by other methods to access the cell:
B4X:
Private Sub getSelectedItem As String
    Dim JO As JavaObject = tableview1
    ' get the selectionmodel
    Dim JOSelectionModel As JavaObject = JO.RunMethod("getSelectionModel", Null)
    ' get the column
    Dim JOGetSelectedCells As JavaObject = JOSelectionModel.RunMethod("getSelectedCells", Null)
    Dim JOGet0 As JavaObject = JOGetSelectedCells .RunMethod("get", Array As Object(0))
    Dim cellIndex As Int = JOGet0.RunMethod("getColumn", Null)
    Log("Cell Index:" & cellIndex)
    '
    Dim getSelectedItem As String = JOSelectionModel.RunMethod("getSelectedItem", Array As Object(cellIndex))
    Log(getSelectedItem )
End Sub
 
Upvote 0

JEG

Member
Licensed User
Longtime User
Problems. SelectedRowChanged returns the index but SelectedCellChanged returns nothing. Must something be set?
 
Upvote 0
Top