•Adding New Menu

New menu is created and added to the menu bar.

In[115]:=

menuFilter = NewJMenu[menuBar, "Filter", "F"]

Menu item is added to the menu.

In[116]:=

itemApply = NewJMenuItem[menuFilter, "Apply", "A"]

Listener class for menu action is loaded.

In[117]:=

LoadJavaClass["JLinkProgramming.JavaSwing.ThreadMathActionListener"]

In[118]:=

listenerItemApply = JavaNew["JLinkProgramming.JavaSwing.ThreadMathActionListener"]

A command handler is defined and is set to the listener.

In[119]:=

handleItemApply[event_, command_] := JavaBlock[Module[ {saved},  saved = frame @ getTitle[] ;  ... quot;handleItemApply was called..."] ;   frame @ setTitle[saved] ;  ReleaseObject[event] ; ]]

In[120]:=

listenerItemApply @ setHandler["actionPerformed", ToString[handleItemApply]]

The listener itself is attached to the menu item.

In[121]:=

itemApply @ addActionListener[listenerItemApply]

BEFORE USING ANY MENU COMMAND DEFINED IN MATHEMATICA, SHARING FRONTEND SHOULD BE STARTED.

In[122]:=

sfe = ShareFrontEnd[]

Now clicking a menu item makes Mathematica print a string. Because this 'calculation' is done very fast, it might be impossible to see the change of frame's title.

handleItemApply was called...

handleItemApply was called...

handleItemApply was called...

(sharing) In[123]:=

LongCalculation[notify_: False] := Module[ {sum = 0, cnt = 0},  Do[ sum += Random[] ;  ++ cnt  ... rame @ setTitle["cnt=50000, sum=" <> ToString[sum]]] ],  {100000}] ;  Print[sum] ]

(sharing) In[124]:=

LongCalculation[] // Timing

The definition of the handlerItemFilter[ ] is changed.

(sharing) In[125]:=

handleItemApply[event_, command_] := JavaBlock[Module[ {saved},  saved = frame @ getTitle[] ;  ... called..."] ;  LongCalculation[True] ;   frame @ setTitle[saved] ;  ReleaseObject[event] ; ]]

Clicking a menu item invokes this command handler.

handleItemApply was called...

cnt=50000, sum=  24897.651045024762`

49821.49984809181`

handleItemApply was called...

cnt=50000, sum=  24995.229573904682`

49998.91736390086`

handleItemApply was called...

cnt=50000, sum=  24919.757038765583`

49917.11297560461`


Converted by Mathematica  (July 17, 2003)