Differences between revisions 5 and 6
Revision 5 as of 2014-04-12 15:41:27
Size: 2714
Editor: AdamBurke
Comment: revert spam
Revision 6 as of 2014-05-10 23:28:14
Size: 200
Editor: RGipson
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#format wiki
#language en
#pragma section-numbers off

= Swing JTtee Example =

back to SwingExamples

----

{{{#!python
"""
Swing JTree example in Jyhton.

Sticking with my City theme, this createa a J tree with 2 branches
one with cities starting with M and the other branch with cties starting
with S. This examples using a button event to show the selected city.

Greg Moore
Sept 2007
"""

from javax.swing import *
from java.awt import *
from javax.swing.tree import DefaultMutableTreeNode

class Example:

  def __init__(self):
    mCitiesData = ['Memphis', 'Melbourne', 'Milan',
                   'Marrakech', 'Moscow', 'Munich']

    sCitiesData = ['San Francisco', 'Salzburg', 'Santiago',
                   'Sydney', 'Sandnessjoen', 'Stockholm']

    frame = JFrame("Jython JTree Example")
    frame.setSize(400, 350)
    frame.setLayout(BorderLayout())

    root = DefaultMutableTreeNode('Cities')
    mCities = DefaultMutableTreeNode('Cities starting with M')
    sCities = DefaultMutableTreeNode('Cities starting with S')
    root.add(mCities)
    root.add(sCities)

    #now add the cities starting with M & S
    self.addCities(mCities, mCitiesData)
    self.addCities(sCities, sCitiesData)
    self.tree = JTree(root)

    scrollPane = JScrollPane() # add a scrollbar to the viewport
    scrollPane.setPreferredSize(Dimension(300,250))
    scrollPane.getViewport().setView((self.tree))

    panel = JPanel()
    panel.add(scrollPane)
    frame.add(panel, BorderLayout.CENTER)

    btn = JButton('Select', actionPerformed = self.citySelect)
    frame.add(btn,BorderLayout.SOUTH)
    self.label = JLabel('Select city')
    frame.add(self.label, BorderLayout.NORTH)
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
    frame.setVisible(True)
    self.addCities

  def addCities(self, branch, branchData=None):
    ''' add data to tree branch
         requires branch and data to add to branch
    '''
    # this does not check to see if its a valid branch
    if branchData == None:
        branch.add(DefaultMutableTreeNode('No valid data'))
    else:
        for item in branchData:
          # add the data from the specified list to the branch
          branch.add(DefaultMutableTreeNode(item))

  def citySelect(self, event):
    selected = self.tree.getLastSelectedPathComponent()
    #check to make sure a city is selected
    if selected == None:
      self.label.text = 'No city selected'
    else:
      self.label.text = str(selected)
    #this is more Jythonic then:
    #self.label.text = selected.toString()

if __name__ == '__main__':
 Example()
}}}

back to SwingExamples
I like Chainmail making. Seems boring? Not!<<BR>>
I also to learn Portuguese in my spare time.<<BR>>
<<BR>>
Feel free to surf to my weblog :: [[http://fvv.kr/i7gamingcomputer89703|gaming mouse]]

I like Chainmail making. Seems boring? Not!
I also to learn Portuguese in my spare time.

Feel free to surf to my weblog :: gaming mouse

JtreeExample (last edited 2014-05-11 02:13:18 by AdamBurke)