How Do I Create J/Link Graphical User Interfaces in Mathematica?

These basic examples show how to create J/Link graphical user interfaces (GUIs) within a Mathematica notebook. We start with simple examples and move on to more difficult ones as we introduce and explain additional features. In these examples, we go through the steps to explain how to create each GUI.


A Simple "Hello World!" Example

Here's a snapshot of the "Hello World!" example:

Here's the Mathematica code for the example:

Get["JLink`"]
InstallJava[];
frm = JavaNew["com.wolfram.jlink.MathJFrame"];
panel = JavaNew["javax.swing.JPanel"];
label = JavaNew["javax.swing.JLabel", "Hello World!"];
panel@add[label];
frm@getContentPane[]@add[panel];
frm@setSize[50, 50];
JavaShow[frm];

This is one of the simplest J/Link examples you can write. It doesn't do much, but the code demonstrates the basic code of most J/Link programs:

1. How to load JLink

2. How to start the Java Runtime Environment

3. How to create a frame

4. How to add something to your frame

5. How to show your frame


Creating a Simple Action to Your Interface

Let's look at SimpleModal[], another simple program. Each time the user clicks the button (JButton), the text field (JTextField) is updated.

Here's the code that initializes the button:

button = JavaNew["javax.swing.JButton", "Increment by one"];
buttonListener = JavaNew["com.wolfram.jlink.MathActionListener"];
buttonListener@setHandler["actionPerformed", "buttonFunc"];
button@addActionListener[buttonListener];

The first line creates the button. The second line sets buttonListener to be a MathActionListener object. The third line sets the Mathematica function buttonFunc[] to be called when buttonListener is called. The fourth line registers an event handler for the button click.

Here's the code that initializes and manipulates the text field:

textField = JavaNew["javax.swing.JTextField", "1", 10];

buttonFunc[_, _] :=
JavaBlock[Module[{curText, newVal}, curText = textField@getText[];
newVal = ToExpression[curText] + 1;
textField@setText[ToString[newVal]]]];

Now that you know how to set up buttons, you also know how to set up check boxes and radio buttons as they are similar to this button example.


Creating a Simple Plot Interface

Let's look at how you can have Mathematica plot your input in PlotApplet[]. Here you enter your function, and each time that you click the Plot button, the plot gets updated.

In this example, we will take input from a text field, wrap it with a Mathematica command, and send it to a MathCanvas object.

This is how to load the MathCanvas object into Mathematica.

mathCanvas = JavaNew["com.wolfram.jlink.MathCanvas"];

We then create an appropriate button attached with a button listener. When the button gets called, it will run the function buttonFunc.

plotButton = JavaNew["javax.swing.JButton", "Plot"];
buttonListener = JavaNew["com.wolfram.jlink.MathActionListener"];
buttonListener@setHandler["actionPerformed", "buttonFunc"];
plotButton@addActionListener[buttonListener];

Here is the function that gets called by the button. This function will extract your input from the text field and send it to MathCanvas for evaluation. This is done by using the setMathCommand method. We have wrapped our input with the Mathematica command Plot for the desired output.

buttonFunc[_, _] :=
JavaBlock[
Module[{plotFunction}, plotFunction = functionField@getText[];
mathCanvas@setMathCommand["Plot[" <> plotFunction <> ", {x, 0, 10},
Background -> GrayLevel[.80]]"]]];


Adding an Adjustment Listener to the Plot Interface

Let's look at adding an action listener to our Plot example in PlotAppletwithAdjustment[]. Here you enter your function, adjust the slider bar, and click the Plot button, which then updates the plot.

In this example, we will take input from a text field and a slider bar, wrap it with a Mathematica command, and send it to a MathCanvas object.

Here we create an appropriate scroll bar attached with MathAdjustmentListener, an appropriate listener. When the scroll bar changes, it runs the function scrollbarFunc. This function simply updates plotDomainText, the JLabel.

plotDomainText = JavaNew["javax.swing.JLabel", "10"];

scrollbar = JavaNew["javax.swing.JScrollBar", 0, 10, 1, 10, 50];

scrolllistener = JavaNew["com.wolfram.jlink.MathAdjustmentListener"];
scrolllistener@setHandler["adjustmentValueChanged", "scrollbarFunc"];
scrollbar@addAdjustmentListener[scrolllistener];

scrollbarFunc[_, _, scrollPos_] :=
JavaBlock[Module[{}, plotDomainText@setText[ToString[scrollPos]]]];

When you click on the Plot button, it will run the function buttonFunc. This function will extract your input from the text field, get the text from plotDomainText, and send it to MathCanvas for evaluation.

buttonFunc[_, _] :=
JavaBlock[
Module[{plotFunction, plotDomain},
plotFunction = functionField@getText[];
plotDomain = plotDomainText@getText[];
mathCanvas@
setMathCommand[
"Plot[" <> plotFunction <> ",{x, 0, " <> plotDomain <> "},
Background -> GrayLevel[.8], PlotStyle -> RGBColor[0, 0, 1]]"]]];


Interacting with the Files on Your File System

Let's look at linking with your file system. Here we create an interface, which sends back the location of the file to the Mathematica front end when you select a file.

See the Mathematica code for the file chooser example.