B4J Library ListView and TableView CellFactory Helper

This is an interface library to help with managing CellFactories for ListView and TableViews.

The principle job of a cell factory is to display data into a cell. The standard ListView and TableViews use a default CellFactory. With this interface, you can create a custom cellfactory and change the appearance of the cells within a ListView and columns within a TableView.

The Library is in two parts, a Java Library that manages the callback required for the ListView to display the data as configured by the cell factory. And a B4j class that allows manipulation of the cell factory.

Usage :

To set up a cell factory, you first need to create an instance of the LVCellFactoryHelper (or TVCellFactoryHelper):

B4X:
    Dim CFH As LVCellFactoryHelper
    CFH.Initialize(CFH.TYPETEXT,"Id",lvName)

It's parameters are a Type, either Text or Graphic(which is a bit of a misnomer as it is actually a Node that can hold other objects), an ID for use within the CellObject Class and the ListView or TableView Column that will hold the CellFactoryObjects.

Then, rather than adding our data directly into a List or Column, we need to create a CellObject for all of the items in a list or column that we want to manage. The CellObject will create and initialize a CellFactoryObject which is the object that needs to be added to the ListView or Column and can be obtained by calling the CellFactoryObject method of the CellObject:

B4X:
Dim CO As CellObject
CO.Initialize("Text","Btn" )
Row(0) = CO.CellFactoryObject

Then we can write the code within the CellObject class that will determine how the data is displayed.


Examples :

There are currently 4 examples attached to this post, I plan at least one more on utilizing Drag and Drop.

The Simple Text example allows a firstname and lastname field to be reversed, and contains a numeric score which triggers a Style to color the cell if the score is >= 90.

The Simple Graphic example does the same, but the Score is in a separate label and so appears aligned.

The Graphic Object Example manages two lists with one CellObject (I could have written two separate classes) And shows or hides a button on each of the lists.

The TableView Editable Example uses an editable TextField and a hideable button in one column.


Download and unzip the Library Jar and Xml files in jCellFactoryHelper.zip to your Addl Libs directory.


I hope it all makes sense. By its nature, it is not particularly simple but I hope you can make use of it.
 

Attachments

  • jCellFactoryHelper.zip
    7.5 KB · Views: 728
  • SimpleTextExample.zip
    4.5 KB · Views: 700
  • SimpleGraphicExample.zip
    4.9 KB · Views: 582
  • GraphicObjectExample.zip
    4.9 KB · Views: 578
  • TableViewEditable.zip
    4.8 KB · Views: 774
Last edited:

stevel05

Expert
Licensed User
Longtime User
Updated TableViewEditable Example to fix a problem where fast scrolling of a TableView can create a transient additional row at the end of the table, causing the data update process to fail.
 

moster67

Expert
Licensed User
Longtime User
Steve - thank you for this.

Did you ever get around to do the example with Drag and Drop? Would like to use the TableView where the user could drag a row into a new position (down or up)..
 

tdocs2

Well-Known Member
Licensed User
Longtime User
Thank you, Steve.

I get a blank listview and tableview....


upload_2015-8-13_2-10-46.png
upload_2015-8-13_2-12-31.png
 

tdocs2

Well-Known Member
Licensed User
Longtime User
Steve,

I am running on Win 10...

I tried the Graphics and it received an error...

B4X:
Program started.
main._lvname_selectedindexchanged (java line: 118)
java.lang.RuntimeException: Object should first be initialized (AnchorPane).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:32)
    at b4j.example.main._lvname_selectedindexchanged(main.java:118)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.BA$2.run(BA.java:165)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1334252076.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1685538367.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/2058534881.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
 

stevel05

Expert
Licensed User
Longtime User
@moster67 Sorry I missed your post, I've not been around much this summer, I should get some time next week to review. I'll make a note to look at it.
@tcdocs I will have a look next week, not going to be able to this week, sorry.
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Hi @tdocs2 I downloaded the GraphicsObjectExample and it runs as is on Win10. Are you running the demo project? Can you post your project if not.
 

stevel05

Expert
Licensed User
Longtime User
Hi @moster67 I've had a look at the project, I did some work on it but it's not ready to release yet, I changed the way the library works which is incompatible with the released version so I can't just release it as is. I'll get to work on finishing it as you have a need for the functionality.
 

TorontoJim

Member
Licensed User
Longtime User
I'm getting an error that TVCellFactoryHelper is an unknown type.

Is there a particular library I need to load for this?
 

stevel05

Expert
Licensed User
Longtime User
The library is in the file jcellfactoryhelper.zip in the first post
 
Top