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

Discussion in 'B4J Questions' started by Nicola Ciaramellano, Sep 13, 2019.

  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:

    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
    "1"'Load the layout file.
    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
    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))
    End Sub

    Sub TableView1_SelectedCellChanged (RowIndex As Int, ColIndex As Int, Cell As Object)
    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?

    Attached Files:

  2. Daestrum

    Daestrum Well-Known Member Licensed User

    Quick fix using JavaObject library
    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.
  4. Nicola Ciaramellano

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

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

    Attached Files:

  5. Erel

    Erel Administrator Staff Member Licensed User

  6. Daestrum

    Daestrum Well-Known Member Licensed 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: Oct 1, 2019
  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