LucaMs Expert Licensed User Longtime User Dec 13, 2022 #1 Sub Test: pnContainer, "a B4XView Pane", contains 6 "B4XView Panes". The first time you call this Sub, no errors; the second time, it throws the casting exception as per the title of this thread, for the first Pane contained, Pane2. Sub Test2: It works without error. B4X: Copy Private Sub Test For Each xView As B4XView In pnContainer.GetAllViewsRecursive Log(xView.Tag) Dim xCanvas As B4XCanvas xCanvas.Initialize(xView) xCanvas.Release Next End Sub B4X: Copy Private Sub Test2 Log("Test2") Dim xCanvas As B4XCanvas xCanvas.Initialize(Pane2) xCanvas.Release Dim xCanvas As B4XCanvas xCanvas.Initialize(Pane2) xCanvas.Release End Sub Attachments CanvasErr.zip CanvasErr.zip 2.3 KB · Views: 378 Last edited: Dec 13, 2022
Sub Test: pnContainer, "a B4XView Pane", contains 6 "B4XView Panes". The first time you call this Sub, no errors; the second time, it throws the casting exception as per the title of this thread, for the first Pane contained, Pane2. Sub Test2: It works without error. B4X: Copy Private Sub Test For Each xView As B4XView In pnContainer.GetAllViewsRecursive Log(xView.Tag) Dim xCanvas As B4XCanvas xCanvas.Initialize(xView) xCanvas.Release Next End Sub B4X: Copy Private Sub Test2 Log("Test2") Dim xCanvas As B4XCanvas xCanvas.Initialize(Pane2) xCanvas.Release Dim xCanvas As B4XCanvas xCanvas.Initialize(Pane2) xCanvas.Release End Sub
P PaulMeuris Well-Known Member Licensed User Dec 13, 2022 #2 This code doesn't give an error. Code: Copy Sub Class_Globals Private Root As B4XView Private xui As XUI Private Pane1 As Pane Private Pane2 As Pane Private Pane3 As Pane Private Pane4 As Pane Private Pane5 As Pane Private Pane6 As Pane Private Pane7 As Pane Private pncontainer As Pane End Sub Public Sub Initialize B4XPages.GetManager.LogEvents = True End Sub Private Sub B4XPage_Created (Root1 As B4XView) Root = Root1 Root.LoadLayout("MainPage") Test Root.LoadLayout("MainPage") Test Test2 End Sub Private Sub Test For Each xView As B4XView In pncontainer.GetAllViewsRecursive Log(xView.Tag) Dim xCanvas As B4XCanvas xCanvas.Initialize(xView) xCanvas.Release Next End Sub Private Sub Test2 Log("Test2") Dim xCanvas As B4XCanvas xCanvas.Initialize(Pane2) xCanvas.Release Dim xCanvas As B4XCanvas xCanvas.Initialize(Pane2) xCanvas.Release End Sub The log lines: Waiting for debugger to connect... Program started. *** mainpage: B4XPage_Created Pane1 Pane2 Pane3 Pane4 Pane5 Pane6 Pane7 Pane1 Pane2 Pane3 Pane4 Pane5 Pane6 Pane7 Test2 *** mainpage: B4XPage_Appear *** mainpage: B4XPage_Resize [mainpage] Click to expand... Maybe because of this: Happy coding! Upvote 1
This code doesn't give an error. Code: Copy Sub Class_Globals Private Root As B4XView Private xui As XUI Private Pane1 As Pane Private Pane2 As Pane Private Pane3 As Pane Private Pane4 As Pane Private Pane5 As Pane Private Pane6 As Pane Private Pane7 As Pane Private pncontainer As Pane End Sub Public Sub Initialize B4XPages.GetManager.LogEvents = True End Sub Private Sub B4XPage_Created (Root1 As B4XView) Root = Root1 Root.LoadLayout("MainPage") Test Root.LoadLayout("MainPage") Test Test2 End Sub Private Sub Test For Each xView As B4XView In pncontainer.GetAllViewsRecursive Log(xView.Tag) Dim xCanvas As B4XCanvas xCanvas.Initialize(xView) xCanvas.Release Next End Sub Private Sub Test2 Log("Test2") Dim xCanvas As B4XCanvas xCanvas.Initialize(Pane2) xCanvas.Release Dim xCanvas As B4XCanvas xCanvas.Initialize(Pane2) xCanvas.Release End Sub The log lines: Waiting for debugger to connect... Program started. *** mainpage: B4XPage_Created Pane1 Pane2 Pane3 Pane4 Pane5 Pane6 Pane7 Pane1 Pane2 Pane3 Pane4 Pane5 Pane6 Pane7 Test2 *** mainpage: B4XPage_Appear *** mainpage: B4XPage_Resize [mainpage] Click to expand... Maybe because of this: Happy coding!
LucaMs Expert Licensed User Longtime User Dec 13, 2022 #3 PaulMeuris said: Maybe because of this: Click to expand... No, it's because you load the layout two times. Remove the declarations of the panes and it will still work. Upvote 0
PaulMeuris said: Maybe because of this: Click to expand... No, it's because you load the layout two times. Remove the declarations of the panes and it will still work.
klaus Expert Licensed User Longtime User Dec 13, 2022 #4 As PaulMeuris mentiones, the problem is that in B4J a Canvas is a View on its own. I have modified your Test routine like below to log the xView type.: B4X: Copy Private Sub Test For Each xView As B4XView In pnContainer.GetAllViewsRecursive Log(xView.Tag) Log(GetType(xView)) Dim xCanvas As B4XCanvas xCanvas.Initialize(xView) xCanvas.Release Next End Sub And in the result you see that at the second run you get a javafx.scene.canvas.Canvas object, which cannot be xCanvas.Release does nothing in B4J nor in B4A. Waiting for debugger to connect... Program started. Pane 2 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane Pane 3 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane Pane 4 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane Pane 5 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane Pane 6 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane Pane 7 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane Pane 2 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane javafx.scene.canvas.Canvas Click to expand... What do you expect with this routine ? You define different B4XCanvases with the same name. Where and how do you want to use these B4XCanavases ? Upvote 0
As PaulMeuris mentiones, the problem is that in B4J a Canvas is a View on its own. I have modified your Test routine like below to log the xView type.: B4X: Copy Private Sub Test For Each xView As B4XView In pnContainer.GetAllViewsRecursive Log(xView.Tag) Log(GetType(xView)) Dim xCanvas As B4XCanvas xCanvas.Initialize(xView) xCanvas.Release Next End Sub And in the result you see that at the second run you get a javafx.scene.canvas.Canvas object, which cannot be xCanvas.Release does nothing in B4J nor in B4A. Waiting for debugger to connect... Program started. Pane 2 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane Pane 3 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane Pane 4 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane Pane 5 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane Pane 6 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane Pane 7 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane Pane 2 anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane javafx.scene.canvas.Canvas Click to expand... What do you expect with this routine ? You define different B4XCanvases with the same name. Where and how do you want to use these B4XCanavases ?
LucaMs Expert Licensed User Longtime User Dec 13, 2022 #5 klaus said: And in the result you see that at the second run you get a javafx.scene.canvas.Canvas object, which cannot be Click to expand... Ok but the second time the views and their types, of course, are the same. Even their number, 6, is the same, the second time (at least apparently); adding: B4X: Copy Log("NumberOfViews: " & pnContainer.NumberOfViews) you get 6 both times. If you remove all views before calling Test the second time, no exception will be raised. B4X: Copy Private Sub Test Log("NumberOfViews: " & pnContainer.NumberOfViews) pnContainer.RemoveAllViews MainForm.RootPane.LoadLayout("Layout1") Obviously I would load a layout into the pnContainer; here I do it like this to still have Button1. In B4A that "project" works without the need to remove views. Last edited: Dec 13, 2022 Upvote 0
klaus said: And in the result you see that at the second run you get a javafx.scene.canvas.Canvas object, which cannot be Click to expand... Ok but the second time the views and their types, of course, are the same. Even their number, 6, is the same, the second time (at least apparently); adding: B4X: Copy Log("NumberOfViews: " & pnContainer.NumberOfViews) you get 6 both times. If you remove all views before calling Test the second time, no exception will be raised. B4X: Copy Private Sub Test Log("NumberOfViews: " & pnContainer.NumberOfViews) pnContainer.RemoveAllViews MainForm.RootPane.LoadLayout("Layout1") Obviously I would load a layout into the pnContainer; here I do it like this to still have Button1. In B4A that "project" works without the need to remove views.
LucaMs Expert Licensed User Longtime User Dec 13, 2022 #6 klaus said: What do you expect with this routine ? Click to expand... Obviously that's a test project, similar to my real project but without the code that uses neither canvases nor panes, but having run Test twice and getting that error, I wanted to ask the question. Upvote 0
klaus said: What do you expect with this routine ? Click to expand... Obviously that's a test project, similar to my real project but without the code that uses neither canvases nor panes, but having run Test twice and getting that error, I wanted to ask the question.
klaus Expert Licensed User Longtime User Dec 13, 2022 #7 LucaMs said: Ok but the second time the views and their types, of course, are the same. Even their number, 6, is the same, the second time (at least apparently); adding: B4X: Copy Log("NumberOfViews: " & pnContainer.NumberOfViews) you get 6 both times. Click to expand... Because the Canvases are childs Panes and not of pnContainer. With For Each xView As B4XView In pnContainer.GetAllViewsRecursive you get also the children of Pane2. Add Log(Pane2.NumberOfViews) in the routine below. B4X: Copy Private Sub Test Log(pnContainer.NumberOfViews) For Each xView As B4XView In pnContainer.GetAllViewsRecursive Log(Pane2.NumberOfViews) Log(xView.Tag) Log(GetType(xView)) Dim xCanvas As B4XCanvas xCanvas.Initialize(xView) xCanvas.Release Next End Sub At the second run you will see that Pane2 has one view ! Upvote 0
LucaMs said: Ok but the second time the views and their types, of course, are the same. Even their number, 6, is the same, the second time (at least apparently); adding: B4X: Copy Log("NumberOfViews: " & pnContainer.NumberOfViews) you get 6 both times. Click to expand... Because the Canvases are childs Panes and not of pnContainer. With For Each xView As B4XView In pnContainer.GetAllViewsRecursive you get also the children of Pane2. Add Log(Pane2.NumberOfViews) in the routine below. B4X: Copy Private Sub Test Log(pnContainer.NumberOfViews) For Each xView As B4XView In pnContainer.GetAllViewsRecursive Log(Pane2.NumberOfViews) Log(xView.Tag) Log(GetType(xView)) Dim xCanvas As B4XCanvas xCanvas.Initialize(xView) xCanvas.Release Next End Sub At the second run you will see that Pane2 has one view !
LucaMs Expert Licensed User Longtime User Dec 13, 2022 #8 klaus said: Because the Canvases are childs Panes and not of pnContainer. Click to expand... Very right; I wasn't at my best "at the time" The problem, however, remains. I'll read more and try again "soon". Thank you, Klaus. Upvote 0
klaus said: Because the Canvases are childs Panes and not of pnContainer. Click to expand... Very right; I wasn't at my best "at the time" The problem, however, remains. I'll read more and try again "soon". Thank you, Klaus.
LucaMs Expert Licensed User Longtime User Dec 13, 2022 #9 Yep, that's the problem anyway. Even considering that B4J handles B4XCanvas differently (and in fact with B4A there is no error), I don't understand why that exception is triggered in the second run but not in the first. java.lang.ClassCastException: javafx.scene.canvas.Canvas cannot be cast to javafx.scene.layout.Pane Erel, can you say something? Upvote 0
Yep, that's the problem anyway. Even considering that B4J handles B4XCanvas differently (and in fact with B4A there is no error), I don't understand why that exception is triggered in the second run but not in the first. java.lang.ClassCastException: javafx.scene.canvas.Canvas cannot be cast to javafx.scene.layout.Pane Erel, can you say something?
kimstudio Active Member Licensed User Longtime User Dec 13, 2022 #10 Try my understanding: In B4J B4XCanvas is a view, so xCanvas.Initialize(xView) will create a canvas view then add it to pane, which here is the xView. The second run pnContainer.GetAllViewsRecursive will get the canvas added in first run as xView, so xCanvas.Initialize(xView) will create another canvas but it can't be added to xView, as this xView is actually a canvas, it tried to cast it to pane as it asks for a pane parameter but it can't cast the canvas to a pane, hence "java.lang.ClassCastException: javafx.scene.canvas.Canvas cannot be cast to javafx.scene.layout.Pane" in B4A B4XCanvas is not a view. Internally it gets the view's GraphicContext something. Upvote 0
Try my understanding: In B4J B4XCanvas is a view, so xCanvas.Initialize(xView) will create a canvas view then add it to pane, which here is the xView. The second run pnContainer.GetAllViewsRecursive will get the canvas added in first run as xView, so xCanvas.Initialize(xView) will create another canvas but it can't be added to xView, as this xView is actually a canvas, it tried to cast it to pane as it asks for a pane parameter but it can't cast the canvas to a pane, hence "java.lang.ClassCastException: javafx.scene.canvas.Canvas cannot be cast to javafx.scene.layout.Pane" in B4A B4XCanvas is not a view. Internally it gets the view's GraphicContext something.
LucaMs Expert Licensed User Longtime User Dec 14, 2022 #11 All this gives me problems, due to my poor memory The purpose of B4XCanvas is obviously to try to make our source code as compatible as possible with the 3 "languages", B4A, B4J and B4I, otherwise it would not have been created, since the Canvases already exist for each of these languages. Is it possible to improve B4XCanvas to avoid these problems? Upvote 0
All this gives me problems, due to my poor memory The purpose of B4XCanvas is obviously to try to make our source code as compatible as possible with the 3 "languages", B4A, B4J and B4I, otherwise it would not have been created, since the Canvases already exist for each of these languages. Is it possible to improve B4XCanvas to avoid these problems?
Erel B4X founder Staff member Licensed User Longtime User Dec 15, 2022 #12 LucaMs said: Is it possible to improve B4XCanvas to avoid these problems? Click to expand... No. This is how Canvas works in JavaFX. It is a separate node. Upvote 0
LucaMs said: Is it possible to improve B4XCanvas to avoid these problems? Click to expand... No. This is how Canvas works in JavaFX. It is a separate node.