B4J Question [B4J] B4XPages - AppDash UI based on the ThreePagesExample, how to resolve ClassCastException on ImageView [Solved]

JGParamo

Active Member
Licensed User
While working on an enhanced version (with stored background/theme/style/form size/position, fine dragging form) of Erel's ThreePagesExample, a B4J ClassCastException - b4j.objects.Pane cannot be cast to class javafx.scene.image.ImageView is thrown when retrieving bitmap as background from KeyValueStore. See code and source below. Still work in progress, any suggestion on resolving such exception? Thanks.
(ClassCastException) java.lang.ClassCastException: class anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane cannot be cast to class javafx.scene.image.ImageView (anywheresoftware.b4j.objects.PaneWrapper$ConcretePaneWrapper$NonResizePane is in unnamed module of loader 'app'; javafx.scene.image.ImageView is in module javafx.graphics of loader 'app')
SetBitmap from KeyValueStore:
Sub UpdateImageVw (appVw As B4XView)
    Try
        If appVw.IsInitialized Then
            appVw.SetBitmap(kvs.GetBitmap("Canvas"))
        End If
    Catch
        Log("Error in UpdateImageVw: " & appVw.Tag.As(String) & " - " & LastException)
    End Try
End Sub
 

Attachments

  • ThreePagesAppUI.zip
    242.7 KB · Views: 116
Last edited:

PaulMeuris

Well-Known Member
Licensed User
The UpdateImageVw is called from the UpdateImageVwFrmCvs in the AppDashUIcodes module.
1717729700555.png

In the AppDashUIstyle1 you are using a variable imgvwBG that is of type ImageView (i guess because the variable is declared as a B4XView).
1717729771963.png

In the AppDashUIstyle2 you are using a variable pnlDrawingCanvas that is of type Pane (i guess again declared as a B4XView).
The SetBitmap accepts an argument of type Image and the appVw is a Pane, right?
1717730372646.png

1717730425442.png

You might also have an issue with the SetBitmap method because the types are not the same.
Declaring variables as B4XView is OK if you are programming for the 3 platforms (B4A, B4I and B4J).
The type conversions can cause some problems.
Using a prefix (img or pnl) with the name of the variables is always a good idea.
 
Upvote 0

JGParamo

Active Member
Licensed User
... might also have an issue with the SetBitmap method because the types are not the same.
Declaring variables as B4XView is OK if you are programming for the 3 platforms (B4A, B4I and B4J).
The type conversions can cause some problems.
My intent is to make it applicable to these 3 platforms. As an ImageView.SetBitmap() requires a javafx.scene.image.Image as parameter and returns an Image, while B4X Canvas.GetBitmap returns a B4XBitmap, the type casting issue could have arise. Is there a method to convert B4XBitmap to Image? I tried something like imgVw.SetBitmap(cvsDrawEdit.CreateBitmap.As(Image)) so that Canvas drawing in AppDashUIstyle2 will be shown as background image in AppDashUIstyle1, to no avail.
 
Upvote 0

JGParamo

Active Member
Licensed User
I have modified/improved the codes and the exception is gone. Thank you PaulMeuris for the comments. Uploaded improved source, "jumping form" between page transition also resolved.

Improvement from UpdateImageVwFrmCvs, UpdateImageVw, UpdateCvsFromBitmap:
Sub UpdateImgVwFrmSavedBitmap (imgVw As B4XView)
    Try
        imgVw.Parent.Color = panelBGColor
        imgVw.SetBitmap(kvs.GetBitmap("Canvas"))
    Catch
        Log("Error in UpdateImgVwFrmSavedBitmap: " & imgVw.Tag.As(String) & " - " & LastException)
    End Try
End Sub

Sub InitializeCvsDrawEdit (Pane As B4XView)
    Try
        If Pane.IsInitialized Then
            cvsDrawEdit.Initialize(Pane)
            cvsDrawEdit.DrawRect(cvsDrawEdit.TargetRect, panelBGColor, True, 0)
            cvsDrawEdit.DrawBitmap(kvs.GetBitmap("Canvas"), cvsDrawEdit.TargetRect)
        End If
    Catch
        Log("Error in InitializeCvsDrawEdit: " & Pane.Tag.As(String) & ", " & LastException)
    End Try
End Sub
 

Attachments

  • ThreePagesAppUI-v3.zip
    243.7 KB · Views: 107
Last edited:
Upvote 0
Top