JMath user interfaces

George Kambouroglou and Bhuvanesh Bhatt

Last updated on Wednesday, May 1, 2002

Initialization

<< JLink`

InstallJava[];

Off[General::"spell1"]

Methods of MathJFrame

Methods[frame]

Definitions

NewFrame[title_String] := 
      Module[{frm}, 
        frm = JavaNew["com.wolfram.jlink.MathJFrame", title];  
        frm@setJMenuBar[JavaNew["javax.swing.JMenuBar"]];  
        frm@setModal[];  frm]

NewButton[text_String,  handler_String: ""] := 
      Module[{btn},  btn = JavaNew["javax.swing.JButton", text];  
        If[handler =!= "", 
          btn@addActionListener[
              JavaNew["com.wolfram.jlink.MathActionListener", 
                handler]]];  btn]

NewButton[frame_?JavaObjectQ,  text_String,  
          handler_String: ""] := 
        frame@getContentPane[]@
            add[NewButton[text,  handler]];

NewLabel[text_String] := 
      JavaNew["javax.swing.JLabel", text]

NewLabel[frame_?JavaObjectQ,  text_String] := 
        frame@getContentPane[]@add[NewLabel[text]];

NewTextField[text_String,  handler_String: ""] := 
      Module[{txtfield},  
        txtfield = JavaNew["javax.swing.JTextField", text];  
        If[handler =!= "", 
          txtfield@
            addActionListener[
              JavaNew["com.wolfram.jlink.MathActionListener", 
                handler]]];  txtfield]

NewTextField[frame_?JavaObjectQ,  text_String,  
          handler_String: ""] := 
        frame@getContentPane[]@
            add[NewTextField[text,  handler]];

NewMenuBar[frame_?JavaObjectQ,  menus___] := 
      Module[{},  Map[
            frame@getJMenuBar[]@
                  add[NewMenuItem[
                      Sequence @@ #]] &,  {menus}];]

NewMenuItem[title_String,  handler_String,  
        options___] := 
      Module[{menuitem},  
        menuitem = JavaNew["javax.swing.JMenuItem", title];  
        menuitem@
          addActionListener[
            JavaNew["com.wolfram.jlink.MathActionListener", handler]];  
        menuitem]

NewMenuItem[title_String,  submenus_List,  
        options___] := 
      Module[{menuitem},  
        menuitem = JavaNew["javax.swing.JMenu", title];  
        Map[Which[Length[#] === 0, menuitem@addSeparator[], True, 
                menuitem@add[NewMenuItem[Sequence @@ #]]]; &, submenus];  
        menuitem]

NewToolBar[frame_?JavaObjectQ,  buttons___List] := 
      Module[{tb},  tb = JavaNew["javax.swing.JToolBar"];  
        Map[Which[Length[#] === 0, tb@addSeparator[], True, 
              tb@add[NewButton[Sequence @@ #]]] &, buttons];  
        frame@getContentPane[]@add[tb];]

This is an uneditable combo box.

NewComboBox[frame_?JavaObjectQ,  list__List,  
        handler___String : ""] := 
      Module[{cb},  
        cb = JavaNew["javax.swing.JComboBox", MakeJavaObject[list]];  
        If[handler =!= "", 
          cb@addActionListener[
              JavaNew["com.wolfram.jlink.MathActionListener", 
                handler]]];  frame@getContentPane[]@add[cb];]

EmptyHandler[_, _] := Null;

Example

Create a frame.

frame = NewFrame["Title"];

Add a label and a button. You can specify the button handler as an additional argument to NewButton.

NewLabel[
        frame, "Try out the button, menus, toolbar, and combo box"];
ButtonHandler[_, _] := 
        Print["Button clicked!"];
NewButton[
        frame, "Click me", "ButtonHandler"];

Add a toolbar. You can specify handlers: {{"Button1","handler1"},{},{"Button2","handler2"}}

NewToolBar[
      frame, {{"Button1"}, {}, {"Button2"}}]

Add a combo box. Editable combo boxes haven't been implemented yet.

ComboBoxHandler[event_, ___] := 
      Module[{tmp = event@getSource[]},  
        Print["Combo box element selected: " <> 
            JavaObjectToExpression@
              JavaNew["java.lang.String", 
                tmp@getSelectedItem[]]]]

NewComboBox[
      frame, {"a", "b"},  "ComboBoxHandler"]

Add a menubar. Evaluate one of the following two cells.

\(TraditionalForm\`NewMenuBar[
      frame, {"Menu", {{"Submenu 1", ""}, {"Submenu 2", ""}, {}, {"Submenu 3", {{"Subsubmenu 1", ""}, {"Subsubmenu 2", ""}}}}}]\)

\(TraditionalForm\`NewMenuBar[
      frame, {"Menu 1", {{"Submenu 11", ""}, {"Submenu 12", ""}, {}, {"Submenu 13", {{"Subsubmenu 131", ""}, {"Subsubmenu 132", ""}}}}}, {"Menu 2", {{}, {"Submenu 21", {{"Subsubmenu 211", ""}, {}, {"Subsubmenu 212", ""}}}, {"Submenu 22", ""}, {"Submenu 23", {{"Subsubmenu 231", ""}, {"Subsubmenu 232", ""}}}}}]\)

Set the layout and show the frame. MathJFrame automatically does frame@dispose[];

frame@getContentPane[]@
          setLayout[
            JavaNew["java.awt.GridBagLayout"]];
frame@pack[];
JavaShow[frame];
DoModal[];

Menu creation structure

Option 1 (current implementation choice):
CreateMenuBar[Menu1, Menu2, Menu3, ...]
where each (Subscript[Menu, i]) looks like:
{"Title", {MenuItem1, MenuItem2, ...}, MenuItemOptions -> ...}
and each (Subscript[MenuItem, i]) looks like:
{"Title", {Submenu1, Submenu2, ...}, MenuItemOptions -> ...}
Of course, submenus can have subsubmenus and so on.
Each leaf in a menu has a handler, which specifies what code to
execute when the menu item is clicked:
{"Title", "handlerFunc", MenuItemOptions -> ...}
I guess the current way separators are inserted should still work.

Option 2:
CreateMenuBar[Menu1, Menu2, Menu3, ...]
where each (Subscript[Menu, i]) looks like:
{"Title", {MenuItem1, MenuItem2, ...}, MenuItemOptions -> ...}
and each (Subscript[MenuItem, i]) looks like:
{"Title", MenuItemOptions -> ..., Submenus -> ...}
Of course, submenus can have subsubmenus and so on.
One of the MenuItemOptions would be a MenuItemHandler option,
which specifies what code to execute when the menu item is clicked.
I guess the current way separators are inserted should still work.

Useful menu item methods

void setToolTipText(String)
({{"void setMnemonic(char)"}, {"void setMnemonic(int)"}})
javax.swing.KeyStroke[] getRegisteredKeyStrokes()

"void \
addMenuKeyListener(javax.swing.event.MenuKeyListener)"

MenuItemOptions

Icon

ToolTip

Font

VerifyInput (use javax.swing.InputVerifier)

Accelerator

Mnemonic

Add ability to set an icon for the JFrame

How to convert Mathematica bitmaps into Java icons?

We might have to create our own Layout Managers

More components

Dialog box

Toolbar

Internal frame (documents within a parent window)

List box

Combo box

Automatic numbering of components

Need a SetLayout[obj_?JavaObjectQ, layout_String] function, which should work for various objects

"Modal" should be an option for frames and dialog boxes

Moving JTools functions to JTools` context

Should have good type-checking and error messages

Possible future direction: a graphical way of creating a GUI from within Mathematica? Perhaps a palette would work. Eventually, I would like a very graphical GUI creation environment, with the ability to resize and position components.