8. wxWidgets XML Resources

XRC is the new, XML-based resource file format for wxWidgets. It is documented in various places, and there are various commercial and open source editors for the format.

8.1. Getting Started

To load an XML resource file, do something like this:

wxXmlResource xrc = wxXmlResource.Get();
xrc.InitAllHandlers();
xrc.Load(XRC_FILE_PATH);

The path can be a regular filesystem path, or it can be in the form java:/path/to/file, in which case it will be retrieved using java's getResourceAsStream() infrastructure.

8.2. Class Generator

For convenience, especially when working with modern Java IDEs, you can autogenerate a class that contains create methods for all of the top level components of your XRC file, and get methods for all named children of those components. To do this, you can use the class org.wxwindows.tools.XrcClassGenerator. The constructor takes the a File object of the xrc source, a File object pointing to the directory that will contain the new class, and a string of the full class name to be generated, including package. Then call the .generateClass() method on it. This will create the java file, or throw an Exception. For example, this code:


File src = new File("project/program/xrc/dialogs.xrc");
File dest = new File("build/program/generated/java");
String classname = "org.wxwindows.demo.xrc.dialogs";
XrcClassGenerator generator = new XrcClassGenerator(src, dest, classname);
generator.generateClass();

This would create a file called build/program/generated/java/org/wxwindows/demo/xrc/dialogs.java which contains java source.

8.3. Output Java

The file contains string constants for each name you define, and create and get methods for each; something like this:

  public static final String DEMO_MENUBAR = "demo_menubar";
  public static final String DEMO_MENU_FILE_OPEN = "demo_menu_file_open";
  public static final String DEMO_FRAME = "demo_frame";
  public static final String DEMO_FRAME_EDIT_TEXT = "demo_frame_edit_text";
  public static wxMenuBar createDemoMenubar(wxWindow parent);
  public static int getDemoMenuFileOpenId();
  public static wxFrame createDemoFrame(wxWindow parent);
  public static wxTextCtrl getDemoFrameEditText(wxWindow parent);

And your application code could use this by doing something like this:

wxFrame mainframe = dialogs.createDemoFrame(null); // top level frame, no parent
wxTextCtrl edit = dialogs.getDemoFrameEditText(mainframe);
wxMenuBar mb = dialogs.createDemoMenubar(mainframe);
mainframe.SetMenuBar(mb);
mainframe.EVT_MENU(dialogs.getDemoMenuFileOpenId(), new OnOpenCommandHandler());

Items that are not derived from wxWindow (menu, menuitem) only have get...Id() methods, because you can't get a handle to the object directly from XRC, but also becuase you almost never need anything more than the integer ID to deal with it in events. For simplified event handling, buttons have both a wxButton getName(parent) method AND an int getNameId() function.

8.4. Ant Task

For ease of use, there's also a handy ant task wrapper. You can call it more or less as follows:

    
    <taskdef name="xrcgen" classname="org.wxwindows.tools.XrcClassTask"
    classpath="${wx4j.jar}"/>
    <xrcgen src="program/src/resources/dialogs.xrc"
    dest="build/program/generated/java"
    className="org.wxwindows.demo.xrc.dialogs" />
      

The Ant task will only regenerate the file if the java file does not exist or has an older timestamp than the XRC source file. You will still need to compile the output .java file into a .class file of course.

8.5. IDE Integration

I'm not aware of any Java IDEs that let you edit XRC inline, which is not surprising considering the complexity of integration and the relative youth of the wx4j project. However, you can still point your IDE at the output java file and be able to tab complete on the various get methods within it.

In IntelliJ IDEA, for example, you would simply add the destination directory to your list of source dirs. Click File, Project Properties, Sourcepath tab, Add..., and select the destination directory. You'll want to generate the class once before doing so. If you rebuild the class in an IDEA session and the new methods aren't appearing, click File, Synchronize.