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¶
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
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()
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