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

Discussion in 'B4J Questions' started by Nicola Ciaramellano, Sep 13, 2019 at 8:57 PM.

Tags:
  1. Nicola Ciaramellano

    Nicola Ciaramellano Member Licensed 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:

    Code:
    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 StringAs 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!
     

    Attached Files:

  2. Daestrum

    Daestrum Well-Known Member Licensed User

    Quick fix using JavaObject library
    Code:
    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 JavaObjectAs JavaObject
     
    Return o
    End Sub
     
    Nicola Ciaramellano likes this.
  3. Nicola Ciaramellano

    Nicola Ciaramellano Member Licensed User

    Thank you very much!
    It works very well :)
     
    Daestrum likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice