B4J Question [SOLVED] Selectively Color rows in Tableview

Peter Lewis

Active Member
Licensed User
Longtime User
Hi All,

I am trying to color a row in a tableview dependent on the value of one of the cells

I have tried to color the cells but there is something I am missing.

Thank you

B4X:
For i = 0 To TableView1.items.size -1
        Dim row() As Object = TableView1.items.get(i)
                Dim abc As String = row(2)

                If abc = "0" Then
                        row(2) = "N"
                Else
                        row(2) = "Y"
                                    Dim tv1  As B4XView = TableView1
                                    tv1.TextColor=xui.Color_Green
                                
        
                End If
    Next
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User

Thank you Erel,

I was hoping there is something I could do with tableview , not b4xtable as the layout of B4xTable does not suit my other layouts in the program and would be a big convert to do all.

I am sure I did it before, I just cannot find the code.

B4xTable and CustomListView were options.

Thank you
 
Upvote 0

walt61

Active Member
Licensed User
Longtime User
Instead of a string, use a label for the cells in column 2. You can then access it like:
B4X:
Dim tv1 As B4XView = row(2)
tv1.TextColor = xui.Color_Green

One caveat: if you want to sort by clicking the column header, you'll have to implement that yourself: the tableview only handles strings (not labels or other objects) for sorting.
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Instead of a string, use a label for the cells in column 2. You can then access it like:
B4X:
Dim tv1 As B4XView = row(2)
tv1.TextColor = xui.Color_Green

One caveat: if you want to sort by clicking the column header, you'll have to implement that yourself: the tableview only handles strings (not labels or other objects) for sorting.
I tried that but it did not like it

1708880882134.png
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
This might do what you want (uses JavaObject)

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Dim tv As TableView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    tv.Initialize("tv")
    tv.SetColumns(Array("one","two","three"))
    tv.setColumnWidth(0,50)
    tv.setColumnWidth(1,50)
    tv.setColumnWidth(2,200)
    For a = 0 To 20
        Dim item(3) As Object
        item(0) = a
        item(1) = a * 2
        item(2) = "item" & a
    tv.Items.Add(item)
    Next
    MainForm.RootPane.AddNode(tv,20,50,300,300)
    ' highlight rows containing "item2"
    (Me).As(JavaObject).RunMethod("setRowColour",Array(tv,"item2"))
End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello World!", "B4X")
End Sub
#if java
import javafx.scene.control.*;

public static void setRowColour(TableView tabv, String keyValue){
    tabv.setRowFactory(tv -> new TableRow<Object[]>() {
        @Override
        protected void updateItem(Object[] item, boolean empty) {
            super.updateItem(item, empty);
            if (item == null || empty) {
                setStyle("");
            } else if (((String)item[2]).contains(keyValue)) {  // put your test here remember cols start at 0
                // Change the color of the row based on the value in the column
                setStyle("-fx-background-color: lightgreen;");
            } else {
                setStyle("");
            }
        }
    });
}
#End If
1708896737440.png
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
This might do what you want (uses JavaObject)

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Dim tv As TableView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    tv.Initialize("tv")
    tv.SetColumns(Array("one","two","three"))
    tv.setColumnWidth(0,50)
    tv.setColumnWidth(1,50)
    tv.setColumnWidth(2,200)
    For a = 0 To 20
        Dim item(3) As Object
        item(0) = a
        item(1) = a * 2
        item(2) = "item" & a
    tv.Items.Add(item)
    Next
    MainForm.RootPane.AddNode(tv,20,50,300,300)
    ' highlight rows containing "item2"
    (Me).As(JavaObject).RunMethod("setRowColour",Array(tv,"item2"))
End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello World!", "B4X")
End Sub
#if java
import javafx.scene.control.*;

public static void setRowColour(TableView tabv, String keyValue){
    tabv.setRowFactory(tv -> new TableRow<Object[]>() {
        @Override
        protected void updateItem(Object[] item, boolean empty) {
            super.updateItem(item, empty);
            if (item == null || empty) {
                setStyle("");
            } else if (((String)item[2]).contains(keyValue)) {  // put your test here remember cols start at 0
                // Change the color of the row based on the value in the column
                setStyle("-fx-background-color: lightgreen;");
            } else {
                setStyle("");
            }
        }
    });
}
#End If
View attachment 151188
Thank you, will try it now
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
This might do what you want (uses JavaObject)

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Dim tv As TableView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    tv.Initialize("tv")
    tv.SetColumns(Array("one","two","three"))
    tv.setColumnWidth(0,50)
    tv.setColumnWidth(1,50)
    tv.setColumnWidth(2,200)
    For a = 0 To 20
        Dim item(3) As Object
        item(0) = a
        item(1) = a * 2
        item(2) = "item" & a
    tv.Items.Add(item)
    Next
    MainForm.RootPane.AddNode(tv,20,50,300,300)
    ' highlight rows containing "item2"
    (Me).As(JavaObject).RunMethod("setRowColour",Array(tv,"item2"))
End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello World!", "B4X")
End Sub
#if java
import javafx.scene.control.*;

public static void setRowColour(TableView tabv, String keyValue){
    tabv.setRowFactory(tv -> new TableRow<Object[]>() {
        @Override
        protected void updateItem(Object[] item, boolean empty) {
            super.updateItem(item, empty);
            if (item == null || empty) {
                setStyle("");
            } else if (((String)item[2]).contains(keyValue)) {  // put your test here remember cols start at 0
                // Change the color of the row based on the value in the column
                setStyle("-fx-background-color: lightgreen;");
            } else {
                setStyle("");
            }
        }
    });
}
#End If
View attachment 151188
Works Perfectly,

THANK YOU.

1708940658212.png
 
Upvote 0
Top