B4J Question Get icon from a file (PC and Mac Final solution)

ThRuST

Well-Known Member
Licensed User
Longtime User
I have tried this code which works fine on PC. However it generates an "run-time initialization" error on Mac. See the screenshot. I will install JDK v9.0.4 on my virtual Mac to see if that helps.

UPDATE:
See Erel's post #5 for the solution.

In case you want to set up vmWare (Mac emulator for PC)

From original post
ImageView1.SetImage(GetFileIcon("C:\Program Files (x86)\Anywhere Software\B4J", "B4J.exe"))

B4X:
Sub GetFileIcon(Dir As String, FileName As String) As Image
    Dim jo As JavaObject
    Dim JavaFile As JavaObject
    JavaFile.InitializeNewInstance("java.io.File", Array(File.Combine(Dir, FileName)))
    Dim icon As JavaObject = jo.InitializeStatic("sun.awt.shell.ShellFolder").RunMethodJO( _
     "getShellFolder", Array(JavaFile)).RunMethod("getIcon", Array(True))
    Return AwtImageToImage(icon)
End Sub

Private Sub AwtImageToImage(img As JavaObject) As Image
    Dim jo As JavaObject
    Return jo.InitializeStatic("javafx.embed.swing.SwingFXUtils").RunMethod("toFXImage", Array(img, Null))
End Sub

B4J caller code



Result on Mac


Java version (MacOSX El Capitan)



Provided is a drag and drop example. Source code and JAR file.
 

Attachments

  • jDragAndDrop3.zip
    367.9 KB · Views: 198
  • jDDR3.jar
    386.6 KB · Views: 208
Last edited:

ThRuST

Well-Known Member
Licensed User
Longtime User
Mac OSX

Native Java
B4X:
javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
Icon icon = fc.getUI().getFileView(fc).getIcon(file);
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…