Jython and
Swing
If it were
any simpler, it would be illegal.
Rob Andrews, with help from the
Jython Users Email List
The following is an example of
using the Jython interactive interpreter from the Windows 2000 command
prompt. On this page, we demonstrate several Jython basics, including:
- use of the jython interpreter from the MS Windows 2000 command prompt
- collection of user input with javax.swing.JOptionPane.showInputDialog()
- conversion of strings to integers, and integers to strings, plus simple
addition
- display of output with javax.swing.JOptionPane.showMessageDialog()
Please note that your web browser will probably wrap some longer
lines of code.
In our example, Jython is installed in the \bin\ folder of a
fairly typical Java installation. My comments are
interjected into the session, indicated by the traditional #.
C:\j2sdk1.4.0\bin>jython
Jython 2.1 on java1.4.0 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> import javax.swing as sshwing
>>> firstNum = sshwing.JOptionPane.showInputDialog("Enter
an integer: ")
>>> secondNum = sshwing.JOptionPane.showInputDialog("Enter
an integer: ")
# Each of the
two previous lines of code caused a separate swing dialog box asking the
user for an integer.
# The user's
input is stored as a string, not dynamically determined to be an integer.
This is easily demonstrated
# as follows:
>>> firstNum
'1'
>>> secondNum
'2'
>>> firstNum + secondNum
'12'
# In the above
lines of code, you see that the user input '1' and '2' in the input dialog
boxes, which are strings
# and subject to concatenation.
# Observe what happens if you try a traditional Java approach
to converting the string to an integer:
>>> num1 = Integer.parseInt(firstNum)
Traceback (innermost last):
File "<console>", line 1, in ?
NameError: Integer
# If you want
to use Integer.parseInt(), first use one of the following two import statements:
#
from java.lang import Integer(which imports Integer
from java.lang), or
# from
java.lang import * (which imports all classes in java.lang, in
case you need several of them).
#
# A Python
built-in function, however, does the trick:
>>> num1 = int(firstNum)
>>> num1
1
>>> num2 = int(secondNum)
>>> num2
2
>>> num1 + num2
3
>>> sum12 = num1 + num2
>>> sum12
3
# Another Python
built-in handily converts the sum on-the-fly to a string:
>>> sum12 = str(num1
+ num2)
>>> sum12
'3'
# Output of the
sum in a swing message box is relatively painless. But in Jython, you
use "None" in place of
# the more Java-esque "Null" in the following examples:
>>> sshwing.JOptionPane.showMessageDialog(None,
"The sum of your integers is " + sum12)
# The same output
may be tweaked to show the box with a label "Sum" and with a more generic
look:
>>> sshwing.JOptionPane.showMessageDialog(None,
"The sum of your integers is " + sum12, "Sum", sshwing.JOptionPane.PLAIN_MESSAGE)
# A little tweak
adds a "." to the end of the Sum dialog statement.
>>> sshwing.JOptionPane.showMessageDialog(None,"The
sum is " + sum12 + ".", "Sum", sshwing.JOptionPane.PLAIN_MESSAGE)
# Easy as Py!
And, of course, using the interactive prompt is only one option. Save a .py
source file and run it without a separate compile step.
--Rob Andrews
Special thanks to Jim Carroll, Kevin Butler, Jesse D. Sightler, and
Geoffrey S. Knauth for their helpful suggestions.