B4J Question TableView: Multiple Selection via program

Status
Not open for further replies.

GuyBooth

Active Member
Licensed User
Longtime User
I would like to be able to select all the rows in a table programmatically.
I can select them with a mouse by selecting the first row, scrolling to end and Shift-Clicking on the last row. So far my efforts to do this via a program have failed:
B4X:
For i = 0 To tblMediaTracks.Items.Size-1
     tblMediaTracks.selectedrow = i
Next
selects each track one at a time, and at the end I just have the last track selected. I want to add them all to the SelectedRows array (in TableViewExtension) but it is read-only. Is there an approach I am missing?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code cannot work. It replaces the selected row each call.

B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private TableView1 As TableView
   Private tvSelModel As JavaObject
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"))
   For i = 1 To 100
       TableView1.Items.Add(Array(i, i))
   Next
   Dim jotable As JavaObject=TableView1
   Dim joSelMode As JavaObject
   joSelMode.InitializeStatic("javafx.scene.control.SelectionMode")
   'get Selection Model:
   tvSelModel = jotable.RunMethodJO("getSelectionModel", Null)
   'set the selectionmode of the tableview by using the enum
   tvSelModel.RunMethod("setSelectionMode",Array(joSelMode.GetField("MULTIPLE")))
End Sub

Sub SelectAll
   tvSelModel.RunMethod("selectAll", Null)
End Sub

Sub MainForm_MouseClicked (EventData As MouseEvent)
   SelectAll
End Sub
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
This works.
I was able to apply just these lines to my existing code and tableview set up in Designer, jTableViewExtended library applied and using its MultipleSelection method:
B4X:
Sub Process_Globals
    ' other globals  
    Private tblSelModel As JavaObject
End sub

Sub TableView_Setup
    ' Other setup code
    ' …..
    Dim jotable As JavaObject = tblMediaTracks
    Dim joSelMode As JavaObject
    joSelMode.InitializeStatic("javafx.scene.control.SelectionMode")
    'get Selection Model:
    tblSelModel = jotable.RunMethodJO("getSelectionModel", Null)
End Sub

Sub chkAllTracks_CheckedChange(Checked As Boolean)
  If Checked Then
     tblSelModel.RunMethod("selectAll", Null)
  Else
     tblMediaTracks.ClearSelection
  End If
End Sub
When I click on the checkbox to select all the rows, the color of the selected row is a light gray. Calling a tblMediaTracks.RequestFocus takes care of this.

[Edit] There is also a CSS setting that can be changed to make the color of the selection bar when the TableView does not have the focus the same as when it does (or any color of your choosing). The two settings are
-fx-selection-bar: <color>;
-fx-selection-bar-non-focused: <color>;

Many thanks Erel!
 
Last edited:
Upvote 0

mediakon

New Member
Licensed User
Longtime User

Hi Erel, Could You give me a hand and tell me, how i can get information about selected rows i.e. after click on button?
 
Upvote 0
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…