Swing Examples

Legacy Wiki Page

This page was migrated from the old MoinMoin-based wiki. Information may be outdated or no longer applicable. For current documentation, see python.org.

Swing Examples in Jython

DocumentationAndEducation


Using Java Swing with Jython is a lot of fun and makes it really easy to develop a nice UI in much less code then a similar Java app. All of the examples below work with Jython 2.2 and a 1.5 (or greater) JVM is strongly recommended.

Many of these have been tested and will work using jdk/jre <1.5. If you are using an older JVM will need to change the code from frame.add to frame.contentPane.add

Below there are several examples that cover various swing components. Some time in the past I found a Jython script on the Georgia Tech web site that showed off a number of components, being that the web is constantly changing I have provided a copy of the SwingSampler with the permission of the professor who believes he is the author.

In March 2007, Anton Vredegoor asked the Jython mailing list for help converting Java Simple Editor and Jeff Emanuel provided complete conversion to Jython. Here is the JythonNutpad that was posted.

The Jython Mailing list is a great source of help. If you have questions or problems the mailing list is a good place to look for help but there is no substitute for RTFM or Google searches before hand.

Simple Swing examples

These are fairly simple examples but show components.

Hello, World!

These are some ways of creating a simple frame with the title ‘Hello, World!’ on it.

   1 from javax.swing import JFrame
   2 JFrame('Hello, World!', defaultCloseOperation=JFrame.EXIT_ON_CLOSE, size=(300, 300), locationRelativeTo=None).setVisible(True)
   1 from javax.swing import JFrame
   2 f = JFrame('Hello, World!', defaultCloseOperation=JFrame.EXIT_ON_CLOSE, size=(300, 300), locationRelativeTo=None)
   3 f.setVisible(True)

The above examples can also be done “Java-style”, i.e. a bit more verbose as shown below:

   1 from javax.swing import JFrame
   2 f = JFrame('Hello, World!')
   3 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
   4 f.setSize(300, 300)
   5 f.setLocationRelativeTo(None)
   6 f.setVisible(True)

And yet another way of doing it is shown below:

   1 from javax.swing import JFrame
   2 f = JFrame('Hello, World!')
   3 f.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
   4 f.size = (300, 300)
   5 f.locationRelativeTo = None
   6 f.visible = True

JButton and Button events

Just a simple example.

   1 """
   2 A simple example that shows button event handling
   3 
   4 Greg Moore
   5 Sept 2007
   6 """
   7 
   8 from javax.swing import *
   9 from java.awt import BorderLayout
  10 
  11 class Example:
  12   def setText(self,event):
  13       self.label.text = 'Button clicked.'
  14 
  15   def __init__(self):
  16     frame = JFrame("Jython Example JButton")
  17     frame.setSize(100, 100)
  18     frame.setLayout(BorderLayout())
  19     self.label = JLabel('Hello from Jython')
  20     frame.add(self.label, BorderLayout.NORTH)
  21     button = JButton('Click Me',actionPerformed=self.setText)
  22     frame.add(button, BorderLayout.SOUTH)
  23     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
  24     frame.setVisible(True)
  25 
  26 if __name__ == '__main__':
  27         Example()

JTextField

   1 """
   2 Swing JTextField example in Jython.
   3 
   4 Creates 2 text fields and clicking the button copies text in one textfield
   5 to the other. 
   6 
   7 Greg Moore
   8 Sept 2007
   9 """
  10 
  11 from javax.swing import *
  12 from java.lang import *
  13 
  14 class Example:
  15 
  16   def copyText(self,event):
  17       self.textfield2.text = self.textfield1.text
  18 
  19   def __init__(self):
  20         # These lines setup the basic frame, size.
  21         # the setDefaultCloseOperation is required to completely exit the app
  22         # when you click the close button
  23     frame = JFrame("Jython JText Field Example")
  24     frame.setSize(200, 150)
  25     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
  26 
  27     # Create Panel and add swing components and show it.
  28     pnl = JPanel()
  29     frame.add(pnl)
  30     self.textfield1 = JTextField('Type something here',15)
  31     pnl.add(self.textfield1)
  32     self.textfield2 = JTextField('and click Copy', 15)
  33     pnl.add(self.textfield2)
  34     copyButton = JButton('Copy',actionPerformed=self.copyText)
  35     pnl.add(copyButton)
  36     frame.pack()
  37     frame.setVisible(True)
  38 
  39 if __name__ == '__main__':
  40   #start things off.
  41   Example()

JRadioButton

   1 """
   2 Swing JRadioButton example in Jython.
   3 
   4 Creates radio buttons and event handler
   5 
   6 Greg Moore
   7 Sept 2007
   8 """
   9 
  10 # Using import * is bad form but since this is just a example I'll take the
  11 # easy way out instead of specifing each package.
  12 from javax.swing import *
  13 from java.awt import *
  14 
  15 
  16 class RadioButtonExample:
  17   def radioBtnbCheck(self,event):
  18     # Process the events and update the label
  19     statusText = ""
  20     if self.radioBtn1.isSelected():
  21       statusText = "Radio Button 1 is selected "
  22     if self.radioBtn2.isSelected():
  23       statusText = statusText + "Radio Button 2 is selected "
  24     self.rbStatusLabel.text = statusText
  25 
  26   def __init__(self):
  27         # These lines setup the basic frame, size and layout
  28         # the setDefaultCloseOperation is required to completely exit the app
  29         # when you click the close button
  30     frame = JFrame("Jython Example JRadioButton")
  31     frame.setSize(400, 150)
  32     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
  33     frame.setLayout(BorderLayout())
  34 
  35     # create 2 panels the top one containing radio buttons and the bottom one
  36     # has the label and button. add them to the frame and show it.
  37     rbPanel = JPanel()
  38     frame.add(rbPanel,BorderLayout.NORTH)
  39     self.radioBtn1 = JRadioButton('Radio Button 1')
  40     self.radioBtn2 = JRadioButton('Radio Button 2')
  41     rbPanel.add(self.radioBtn1)
  42     rbPanel.add(self.radioBtn2)
  43     rbBtnGroup = ButtonGroup()
  44     rbBtnGroup.add(self.radioBtn1)
  45     rbBtnGroup.add(self.radioBtn2)
  46 
  47     panel = JPanel()
  48     panel.setLayout(BorderLayout())
  49     frame.add(panel,BorderLayout.SOUTH)
  50     button = JButton('Check',actionPerformed=self.radioBtnbCheck)
  51     panel.add(button,BorderLayout.WEST)
  52     self.rbStatusLabel =JLabel()
  53     panel.add(self.rbStatusLabel,BorderLayout.EAST)
  54     frame.pack()
  55     frame.setVisible(True)
  56 
  57 if __name__ == '__main__':
  58   #start things off.
  59   RadioButtonExample()

JCheckBox

   1 """
   2 Swing JCheckBox example in Jython.
   3 
   4 Creates a couple checkboxes and and event handler
   5 
   6 Greg Moore
   7 Sept 2007
   8 """
   9 
  10 # Using import * is bad form but since this is just a example I'll take the
  11 # easy way out instead of specifing each package.
  12 from javax.swing import *
  13 from java.awt import *
  14 
  15 class CheckBoxExample:
  16 
  17   def cbEvents(self,event):
  18         # Process the events and update the label
  19     cbStatusMsg=""
  20     if self.chkbox1.isSelected():
  21       cbStatusMsg = "checkbox1 is selected "
  22     if self.chkbox2.isSelected():
  23       cbStatusMsg = cbStatusMsg + "checkbox2 is selected"
  24     self.cbStatusLabel.text = cbStatusMsg
  25 
  26   def __init__(self):
  27         # These lines setup the basic frame, size and layout
  28         # the setDefaultCloseOperation is required to completely exit the app
  29         # when you click the close button
  30         frame = JFrame("Jython JCheckBox Example")
  31         frame.setLayout(BorderLayout())
  32         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
  33         
  34         # this could be done with only one JPanel or use different layout
  35         # create a panel for the checkboxes
  36         cbPanel = JPanel()
  37         frame.add(cbPanel,BorderLayout.NORTH)
  38         self.chkbox1 = JCheckBox('checkbox 1')
  39         self.chkbox2 = JCheckBox('checkbox 2')
  40         cbPanel.add(self.chkbox1)
  41         cbPanel.add(self.chkbox2)
  42 
  43         # create a second panel with the button and label
  44         panel = JPanel()
  45         panel.setLayout(BorderLayout())
  46         frame.add(panel,BorderLayout