5. Control Specific Issues

This section covers specific controls (widgets) that have differences and additions in wx4j.

5.1. wxApp

As in C++, there must be one and only one subclass of wxApp per running application. Also, as in C++, the application's GUI entry point is OnInit(). However, unlike C++, there is no macro to setup the main entry point. So, your main() must instantiate an instance of a wxApp subclass and invoke MainLoop() to begin the wxWidgets event loop. Here is a short example:

public class MyApp extends wxApp
{
    public boolean OnInit()
    {
        // ... initialize a frame and other controls ...
        return true;
    }

    public static void main(String args[])
    {
        MyApp app = new MyApp();
        app.MainLoop();
    }
}

5.2. wxTreeCtrl

Comparing items and setting item data is different in wx4j.

5.2.1. Comparing Tree Items

To compare items of a wxTreeCtrl, instead of overrideing OnCompareItems(), a new interface has been created, called wxTreeCtrlComparator, with the following definition:

/**
 * Interface for handling comparisions between tree items.
 */
public interface wxTreeCtrlComparator
{
    /**
     * Invoked when tree items need to be compared
     *
     * @param item1 First tree item
     * @param item2 Second tree item
     * @return -1, 0, 1 to indicate the first item is less than, equal
     * to, or greater than the second item.
     *
     * @see wxTreeCtrl
     */
    public int compareItems(wxTreeItemId item1, wxTreeItemId item2);
}

A new method named SetComparator() sets the comparator for a given tree. For example:

public class MyTreeCtrl extends wxTreeCtrl implements wxTreeCtrlComparator
{
    public MyTreeCtrl(wxWindow parent, int id, wxPoint pos, wxSize size,
                      int style)
    {
        super(parent, id, pos, size, style);
        SetComparator(this);
        // ... other initialization ...
    }

    public int compareItems(wxTreeItemId item1, wxTreeItemId item2)
    {
        // ... logic to compare two tree items ...
    }
}

5.2.2. Setting Item Data

wxJTreeItemData is a subclass of wxTreeItemData that can hold a Java object. Helper methods named SetJData() and GetJData() take care of managing this object and calling SetItemData() and GetItemData(). You would use these helper methods like:

wxTreeItemId item = tree.AddRoot("Root", image, image);
tree.SetJData(item, "Root Item");
// ... later ...
item = tree.GetRootItem();
String data = (String) tree.GetJData(item);

5.3. wxListCtrl

Sorting list items is different in wx4j.

5.3.1. Sorting List Items

To sort a wxListCtrl, instead of SortItems() taking a function pointer callback, it takes a wxListCtrlComparator interfacek, with the following definition:

/**
 * Interface for handling comparisions between list items.
 */
public interface wxListCtrlComparator
{
    /**
     * Invoked to compare two list items.
     *
     * @param itemData1 List data associated with first item
     * @param itemData2 List data associate with second item
     * @param data Data passed from {@link wxListCtrl#SortItems
     * wxListCtrl.SortItems()}
     * @return -1, 0, 1 to indiciate the first item is less then,
     * equal to, or greater than the second item.
     *
     * @see wxListCtrl#SortItems
     */
    public int compareListItems(int itemData1, int itemData2, int data);
}

This could be used like:

public class MyPanel extends wxPanel implements wxListCtrlComparator
{
    public MyPanel(wxWindow parent)
    {
        super(parent, -1);
        mList = new wxListCtrl(/* ... */);

        // ... later ...
        mList.SortItems(this, data);
    }

    public int compareListItems(int itemData1, int itemData2, int data)
    {
        // ... logic to compare two list items ...
    }
}