Could Swing work with JPython?

Matthew Hawkins matthew at topic.com.au
Mon Aug 7 00:16:03 EDT 2000


Mark Wu <mark at advtek.com.tw> in comp.lang.python:

>    Could Swing work with JPython? If possile! Could some one give me an
>example. I could not find any information about swing in www.jpython.org,

Swing most definately works.  For a simple example, look at the
following.  It's what I've hacked together so far in trying to learn
this stuff myself.  Yes, the documentation for JPython is very poor and
there are very very few online examples.

Note with the actionPerformed and valueChanged examples below how
trivial event handling is in JPython compared to Java.

#!/usr/bin/env jpython

from pawt.swing import *
from pawt.awt import BorderLayout

def exit(event):
	raise SystemExit

class MyFunkyTestTree:
	def __init__(self, name="Sample Tree", style="None"):
                self.frName = name
		self.lineStyle = style
		self.datai = MyDataInterface()
		self.button = JButton('Close Me!', actionPerformed=exit)
		self.textbox = JTextField('Current Selection: None')
		self.tree = JTree(self.datai.createNodes())
		self.tree.getSelectionModel().setSelectionMode(tree.TreeSelectionModel.SINGLE_TREE_SELECTION)
		self.tree.putClientProperty("JTree.lineStyle", self.lineStyle)
		self.tree.valueChanged = self.valueChanged
		self.pane = JPanel(BorderLayout())
		self.pane.add(JScrollPane(self.tree), BorderLayout.CENTER)
		self.pane.add(self.textbox, BorderLayout.NORTH)
		self.pane.add(self.button, BorderLayout.SOUTH)
		self.frame = JFrame(self.frName, visible=0)
		self.frame.contentPane.add(self.pane)
		self.frame.pack()
	
	def show(self):
		self.frame.visible = 1
	
	def hide(self):
		self.frame.visible = 0
	
	def getTree(self):
		return self.tree
	
	def getTextbox(self):
		return self.textbox
	
	def valueChanged(self, event):
		self.textbox.setText("Current selection: " + self.tree.getLastSelectedPathComponent().toString())

class MyDataInterface:
        def __init__(self):
		self.login()

	def createNodes(self):
		fooray = ['First', 'Second', 'Third', 'Fourth', 'Fifth']
		top = tree.DefaultMutableTreeNode("Funky Tree")
		wakko = tree.DefaultMutableTreeNode("First Floor, ladies and mens underwear")
		top.add(wakko)
		for fname in fooray:
			yakko = tree.DefaultMutableTreeNode([fname, "Blah"])
			wakko.add(yakko)
		wakko = tree.DefaultMutableTreeNode("Second Floor, habidashery")
		top.add(wakko)
		for fname in fooray[:3]:
			yakko = tree.DefaultMutableTreeNode([fname, "Foo"])
			wakko.add(yakko)
		return top

	# fake function, could authenticate with a database for example
        def login(self):
		pass

if __name__ == '__main__':
	sometree = MyFunkyTestTree('Neato','Angled')
	sometree.show()

HTH,

-- 
Matt



More information about the Python-list mailing list