Jtree Example¶
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 JTtee Example¶
back to SwingExamples
1 """
2 Swing JTree example in Jyhton.
3
4 Sticking with my City theme, this createa a J tree with 2 branches
5 one with cities starting with M and the other branch with cties starting
6 with S. This examples using a button event to show the selected city.
7
8 Greg Moore
9 Sept 2007
10 """
11
12 from javax.swing import *
13 from java.awt import *
14 from javax.swing.tree import DefaultMutableTreeNode
15
16 class Example:
17
18 def __init__(self):
19 mCitiesData = ['Memphis', 'Melbourne', 'Milan',
20 'Marrakech', 'Moscow', 'Munich']
21
22 sCitiesData = ['San Francisco', 'Salzburg', 'Santiago',
23 'Sydney', 'Sandnessjoen', 'Stockholm']
24
25 frame = JFrame("Jython JTree Example")
26 frame.setSize(400, 350)
27 frame.setLayout(BorderLayout())
28
29 root = DefaultMutableTreeNode('Cities')
30 mCities = DefaultMutableTreeNode('Cities starting with M')
31 sCities = DefaultMutableTreeNode('Cities starting with S')
32 root.add(mCities)
33 root.add(sCities)
34
35 #now add the cities starting with M & S
36 self.addCities(mCities, mCitiesData)
37 self.addCities(sCities, sCitiesData)
38 self.tree = JTree(root)
39
40 scrollPane = JScrollPane() # add a scrollbar to the viewport
41 scrollPane.setPreferredSize(Dimension(300,250))
42 scrollPane.getViewport().setView((self.tree))
43
44 panel = JPanel()
45 panel.add(scrollPane)
46 frame.add(panel, BorderLayout.CENTER)
47
48 btn = JButton('Select', actionPerformed = self.citySelect)
49 frame.add(btn,BorderLayout.