The future of Java-based GUI interfaces

GUIKit and the Super Widget Package compared with notebook-based GUI's

David Bailey

http://www.dbaileyconsultancy.co.uk

Abstract

The Mathematica front end is acquiring many exciting GUI facilities. It will be possible to create notebooks on the fly that look like dialog boxes, and which can be used to input and display data. In combination with the advanced plotting capabilities, this will undoubtedly result in a host of new applications.

However, it has been possible to create Java-based GUI applications with Mathematica for some time - first with J/Link, and more recently with GUIKit and the author's Super Widget Package (SWP). This paper will compare these two approaches to GUI construction, and argue that while interactive notebooks will be of enormous value for applications aimed at Mathematica users, the alternative Java-based GUIKit/SWP approach is preferable for applications that need to appear as industry-standard as possible.

Java has a huge GUI API - all of which is accessible from Mathematica. This talk will illustrate a number of SWP techniques that access Java GUI features with no simple front end equivalent. For example, Java (and hence the GUIKIt/SWP) permits HTML to be used in many circumstances where a simple string is more usually employed. This is key to many attractive GUI features. The SWP also makes it  easy to set up wizards to guide users through a complicated input task.

The Java-based approach can also be beneficial for some applications aimed at Mathematica users. This will be illustrated by an enhanced edit panel which improves the notebook editing experience in several ways. Because this panel is not a notebook, typing into its data fields does not change the currently selected notebook, or its selection position – which is important for applications such as this.

Java based GUI's in Mathematica

A simple SWP/GUIKit example

xx=42;
SuperGUIRunModal[SuperWidgetIntegerBox[xx]]

Although this example is extremely simple, it does illustrate a number of important points:

•	The SWP syntax is very compact.

•	The super widget 'knows' about integers – you can't insert other characters.

•	The super widget 'knows' that you may need to transition through invalid numbers – such as the empty field, or a field consisting of just a minus sign – to enter certain values.

xx=42.5;
SuperGUIRunModal[SuperWidgetRealBox[xx]]

Here it is also necessary to cater for partially written exponents.

When to use the 6.0 GUI features

Although this paper is concerned to point out the value of the Java-based approach to Mathematica GUI's – even with version 6.0 – the new FrontEnd GUI features are an obvious bonus, and will be used to create a host of new applications. In particular:

•	GUI applications constructed from the FronEnd will often be the best way to deliver functionality to Mathematica-aware users.

•	Applications that are already based round notebooks will absorb the new functionality to become even more interactive.

Reasons to use a Java-based GUI (1)

Modern Java can deliver a GUI that has the 'Look and Feel' of the platform on which it is running.

SuperWidgetFrame[xxx,{(Subscript[¶, 10]),{(Subscript[¶, 10]),"Activate the menu with the ALT key under Windows",(Subscript[¶, 10])},(Subscript[¶, 10])},Menu->{{"&File",{{"&Open",f1},{"&Save",f2},{"E&xit",f3}}}}
]//SuperGUIRunModal

Reasons to use a Java-based GUI (2)

Java can create widgets that are not (yet) offered by the FrontEnd.

Needs["SuperWidgetPackage`"]

x=Tree˘Node["Language",0,0,{Tree˘Node["French",1,0,{Tree˘Node["Country",0,0],Tree˘Node["Dictionary",0,0]}],Tree˘Node["English",0,0,{Tree˘Node["Country",0,0],Tree˘Node["England",0,0],Tree˘Node["USA",0,0],Tree˘Node["Australia",0,0]}],Tree˘Node["Dictionary",0,0]}];

tree˘fn[x_]:=Print[Cases[x,Tree˘Node[_,_,1],∞]];
build˘tree[prefix_String,items_List]:=Module[{s},
If[Length[items]==0,
Tree˘Node[prefix,0,0],
Tree˘Node[prefix<>"...",0,0,Map[build˘tree[prefix<>ToString[#],DeleteCases[items,#]]&,items]
]
]
];

x=build˘tree["",{1,2,3,4,5}];

SuperWidgetTree[x,SelectionFunction->tree˘fn,Pixel˘Width->300,Pixel˘Height->300]//SuperGUIRunModal

Some widgets, such as the slider, have enhanced functionality.

Clear[f];
labelText[]:=
(
Image˘Expression[StyleForm[SequenceForm["x= ",NumberForm[x˘val,{10,10}],",  Sin[x]= ",NumberForm[Sin[x˘val],{10,10}]],FontSize->18]]
)

f[x_]:=Set˘Label˘Contents[xxx,labelText[]];
x˘val=3.0;
{
(Subscript[¶, 10]),
{(Subscript[¶, 0]),SuperWidgetLabel[xxx,labelText[],Label˘Alignment->Center,Pixel˘Width->450],(Subscript[¶, 0])},
(Subscript[¶, 10]),
{(Subscript[¶, 0]),SuperWidgetHorizontalSlider[x˘val,3.0,3.5,ChangeFunction->f,Zoom˘Menu->True],(Subscript[¶, 0])},
(Subscript[¶, 10])
}//SuperGUIRunModal

Reasons to use a Java-based GUI (3)

Java can access other useful GUI technologies, for example HTML and animated GIF's.

Needs["SuperWidgetPackage`"]

{{(Subscript[¶, 0]),Image˘File["c:/maths/conference_2006/SWP_Paper/Bart.gif"],(Subscript[¶, 0])},
{(Subscript[¶, 10]),"Click OK when you have seen enough!",(Subscript[¶, 10])},
{(Subscript[¶, 0]),SuperWidgetButton[Null,"OK",1],(Subscript[¶, 0])},(Subscript[¶, 10])}//SuperGUIRunModal;

Reasons to use a Java-based GUI (4)

Programs that manipulate other notebooks work best if they themselves do not create a notebook. Here is the control panel for my private extended notebook editor:

Because this panel is a Java-based SWP window, clicking on it does not disturb the currently selected notebook, not the location of the cursor within it.

Further reasons to use a Java-based GUI

•	The Java API is mature – a project will not get snagged on a FrontEnd bug that can't be fixed until the next version!

•	The SWP can generate applications in which Mathematica is totally concealed. Consider the following Windows command line:
@"c:\program files\wolfram research\mathematica\5.2\Mathkernel" -mathlink -initfile standalone.m

•	Modern Java code is fast – which opens up some interesting new possibilities.

The speed potential of Java

•	Java used to be slow because it was interpreted.

•	Modern Java uses JIT technology, so it is essentially as fast as C.

•	Accessing Java via J/Link incurs an overhead, partly because of Mathlink.

•	Creating Java code within Mathematica and compiling it can access the full performance of Java.

Conclusion

Although version 6.0 GUI features will soon be available, Java-based GUI's still offer important advantages for some applications.