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)
You can confirm this yourself by settings up an account at vmWare (go here) and download vmWare v15 which is a really good Mac emulator. Then you can set it up with an El capitan vmware image(AMD) or Mojave (demands an Intel CPU). Search Youtube to find out how it's done depending which system you've got. The GetFileIcon code works on Windows but needs to be initialized on Mac. Please prove the final solution here once you have confirmed that it works on a Mac emulator or real hardware.

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

upload_2018-11-24_15-58-48.png


Result on Mac
upload_2018-11-24_15-59-38.png


Java version (MacOSX El Capitan)

upload_2018-11-24_16-9-55.png


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

Attachments

  • jDragAndDrop3.zip
    367.9 KB · Views: 305
  • jDDR3.jar
    386.6 KB · Views: 314
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
Top