7. Using Java Resources

A Java resource is basically a file that is located within the runtime class path. It is common technique to place non-class files such as images and i18n resources within the bundle. wx4j provides a few helper functions to load wxWidgets images, bitmaps, and icons from Java resources. The following helper methods on the wxJUtil class:

The type must be one of the wxBITMAP_TYPE_* constants. These functions all use ClassLoader.getReourcesAsStream() and wxImage behind the scenes. Icons and bitmaps are converted from the wxImage. This is because only a wxImage may be created from an input stream. This also means you need to call wxInitAllImageHandlers() prior to using any of these functions.

Here is an example of loading an icon from a resource taken from the minimal sample:

public MyFrame(String title, wxPoint pos, wxSize size)
{
    super(null, -1, title, pos, size, wxDEFAULT_FRAME_STYLE);

    SetIcon(wxJUtil.iconFromResource("images/minimal/mondrian.gif",
                                     wxBITMAP_TYPE_GIF));
    // ... other stuff ...
}

As a convenience, the stringFromResource() method is provided to load the contents of a resource into a string. This may be handy for storing HTML or text files as resources.