B4J Question treeview and refreshing

electro179

Active Member
Licensed User
Longtime User
Hi

I have a treeview with a image for each treeview item.
I refresh each image with

TreeView_Item_CTRL(NumCtrl-1).Image = imggreen or
TreeView_Item_CTRL(NumCtrl-1).Image = imggreen

to see the changes on the treeview I must also change the text

TreeView_Item_CTRL(NumCtrl-1).Text = "1: " & Code_ADN.Cabinet(c-1).CTRL1


After +- 1000 refreshing the program crashes with this code
java.lang.OutOfMemoryError: Java heap space

How to refresh correctly the treeview ?


thank you
 

electro179

Active Member
Licensed User
Longtime User
if I remove the code :
TreeView_Item_CTRL(NumCtrl-1).Text = "1: " & Code_ADN.Cabinet(c-1).CTRL1

There is no error but there is not refreshing of image
 
Upvote 0

electro179

Active Member
Licensed User
Longtime User
I create a small program and there is the same error after +- 1390 changes
 

Attachments

  • test treeview.zip
    195.1 KB · Views: 366
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Seems similar to the bug discussed here: https://community.oracle.com/thread/2439068?tstart=0

It should be fixed in Java 8.

The workaround is to call setAll to set the nodes:
B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   
   Private timer1 As Timer
   
   Private treeview_1 As TreeView
   Dim treeview_item As TreeItem
   Private state As Boolean
   
   Dim lblcount As Label
   Dim counter As Int
   Dim imggreen As Image
   Dim imgred As Image
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.RootPane.LoadLayout("layout_main") 'Load the layout file.
   MainForm.Show
   
   
   timer1.Initialize("timer1_event",10)
   timer1.Enabled = True
   
   
   Dim img As Image
   img.Initialize("","green.jpg")
   treeview_item.Initialize("","item 1")
   treeview_item.Image = img
   state = True
   treeview_1.Root.Children.Add(treeview_item)
   imggreen.Initialize("","green.jpg")
   
   imgred.Initialize("","red.jpg")
End Sub

Sub timer1_event_tick
   counter = counter + 1
   lblcount.Text = counter
   Dim treeview_item As TreeItem
   If (state = True) Then
     treeview_item.Initialize("", "red")
     treeview_item.Image = imgred
     state = False
   Else
     treeview_item.Initialize("", "green")
     treeview_item.Image = imggreen
     state = True
   End  If
   Dim jo As JavaObject = treeview_1.Root.Children
   jo.RunMethod("setAll",  Array As Object(Array As Object(treeview_item)))
End Sub
 
Upvote 0

electro179

Active Member
Licensed User
Longtime User
ok thank you but if I have 2 treeview_item, the second disappears

B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
   
    Private timer1 As Timer
   
    Private treeview_1 As TreeView
    Dim treeview_item(2) As TreeItem
    Private state As Boolean
   
    Dim lblcount As Label
    Dim counter As Int
   
    Dim imggreen As Image
  Dim imgred As Image
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("layout_main") 'Load the layout file.
    MainForm.Show
   
   
    timer1.Initialize("timer1_event",1000)
    timer1.Enabled = True
   
   

    imggreen.Initialize("","green.jpg")
    imgred.Initialize("","red.jpg")   
   
    treeview_item(0).Initialize("","item 1")
    treeview_item(0).Image = imggreen
    treeview_item(1).Initialize("","item 1")
    treeview_item(1).Image = imgred
    state = True
    treeview_1.Root.Children.Add(treeview_item(0))
    treeview_1.Root.Children.Add(treeview_item(1))
   
End Sub

Sub timer1_event_tick
    counter = counter + 1
  lblcount.Text = counter
  Dim treeview_item(2) As TreeItem
  If (state = True) Then
    treeview_item(0).Initialize("", "red")
    treeview_item(0).Image = imgred
    state = False
  Else
    treeview_item(0).Initialize("", "green")
    treeview_item(0).Image = imggreen
    state = True
  End  If
  Dim jo As JavaObject = treeview_1.Root.Children
  jo.RunMethod("setAll",  Array As Object(Array As Object(treeview_item(0))))
End Sub
 
Upvote 0

electro179

Active Member
Licensed User
Longtime User
Thank you for your help

with java 1.8 I have no problem

In 1.7

If I replace the line I have a error

Program started.
main._timer1_event_tick (java line: 164)
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:80)
at b4j.example.main._timer1_event_tick(main.java:164)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:92)
at anywheresoftware.b4a.BA$3.run(BA.java:171)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ClassCastException: anywheresoftware.b4j.objects.TreeViewWrapper$ConcreteTreeItemWrapper cannot be cast to javafx.scene.control.TreeItem
at javafx.scene.control.TreeItem.updateChildrenParent(TreeItem.java:813)
at javafx.scene.control.TreeItem.updateChildren(TreeItem.java:801)
at javafx.scene.control.TreeItem.access$100(TreeItem.java:197)
at javafx.scene.control.TreeItem$2.onChanged(TreeItem.java:397)
at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:134)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:48)
at com.sun.javafx.collections.ObservableListWrapper.callObservers(ObservableListWrapper.java:97)
at com.sun.javafx.collections.ObservableListWrapper.setAll(ObservableListWrapper.java:303)
at com.sun.javafx.collections.ObservableListWrapper.setAll(ObservableListWrapper.java:314)
... 20 more
 
Upvote 0
Top