How to exit awt frame politely from jython
Submitted by Saturday, 10 April, 2004 - 07:20
on
I have been toying with java gui + jython under jedit. I found the code below as a starting point. It works!
The only problem is that when the jython "exit(1)" statement is invoked it not only exits the current (jython+java) window but jedit completely and anything jedit has spawned.
So, what I need for a well-behaved popup frame is one that will not exit jedit when I close it, but rather the frame only.
My guess is I will find the answer as I learn more Java (I'm trying to get an easy intro through jython and jedit). But if anyone has a simple solution, that would be great: I would like to have menus pop up for certain macros and this looks like a good (?) way to do it -- though I'm sure beanshell would work just as well.
(PS: I think the following code formatting gets zapped by this forum software.)
import sys,time
from java import awt
# This is an edited version of a QA tool that I wrote
# Not all the variable names will make sense in this example
# like, 'proclist' and 'job', but you can still figure it out.
#
# The app is based off of the style that wxPython uses, which makes it really easy to
# extend.
class App:
def __init__(self):
procList = []
for i in range(1,11):
procList.append("Test #"+str(i))
self.frame = awt.Frame("Perform Work for Java", visible=1)
self.mainpanel = awt.Panel(awt.GridLayout(0,2))
#added this... does not seem to work; might need to use swing
#frame.setDefaultCloseOperation(awt.Frame.EXIT_ON_CLOSE)
self.cb = {}
for p in procList:
cBox,txtField = self.choiceBox(p)
self.cb[cBox] = txtField
self.mainpanel.add(cBox)
self.mainpanel.add(txtField)
exitBtn = awt.Button("exit", actionPerformed=self.exit)
printBtn = awt.Button("Do Tests", actionPerformed=self.doTest)
self.mainpanel.add(printBtn)
self.mainpanel.add(exitBtn)
self.frame.add(self.mainpanel)
# I've found that packing the frame is a must, though I haven't seen many examples
# that use it. Maybe I'm missing something.
self.frame.pack()
def choiceBox(self,label):
checkBox = awt.Checkbox(label)
numberBox = awt.TextField()
panel = awt.Panel(awt.GridLayout(1,2))
return checkBox,numberBox
def exit(self,e):
sys.exit(1) #here is where it shuts everything down.
pass
def doTest(self,e):
# this pops up a dialog
dlg = MsgDlg("Running Tests")
for job in self.cb.keys():
if job.getState():
print job.getLabel()+" --> "+ self.cb[job].text
time.sleep(2)
# remove the dialog
dlg.frame.removeNotify()
class MsgDlg:
def __init__(self,msg):
self.frame = awt.Frame("Doing Tests", visible=1)
self.panel = awt.Panel(awt.GridLayout(1,1))
self.panel.add(awt.Label(msg))
self.frame.add(self.panel)
self.frame.pack()
if __name__ == '__main__':
# this starts the main window up
app = App()
The only problem is that when the jython "exit(1)" statement is invoked it not only exits the current (jython+java) window but jedit completely and anything jedit has spawned.
So, what I need for a well-behaved popup frame is one that will not exit jedit when I close it, but rather the frame only.
My guess is I will find the answer as I learn more Java (I'm trying to get an easy intro through jython and jedit). But if anyone has a simple solution, that would be great: I would like to have menus pop up for certain macros and this looks like a good (?) way to do it -- though I'm sure beanshell would work just as well.
(PS: I think the following code formatting gets zapped by this forum software.)
import sys,time
from java import awt
# This is an edited version of a QA tool that I wrote
# Not all the variable names will make sense in this example
# like, 'proclist' and 'job', but you can still figure it out.
#
# The app is based off of the style that wxPython uses, which makes it really easy to
# extend.
class App:
def __init__(self):
procList = []
for i in range(1,11):
procList.append("Test #"+str(i))
self.frame = awt.Frame("Perform Work for Java", visible=1)
self.mainpanel = awt.Panel(awt.GridLayout(0,2))
#added this... does not seem to work; might need to use swing
#frame.setDefaultCloseOperation(awt.Frame.EXIT_ON_CLOSE)
self.cb = {}
for p in procList:
cBox,txtField = self.choiceBox(p)
self.cb[cBox] = txtField
self.mainpanel.add(cBox)
self.mainpanel.add(txtField)
exitBtn = awt.Button("exit", actionPerformed=self.exit)
printBtn = awt.Button("Do Tests", actionPerformed=self.doTest)
self.mainpanel.add(printBtn)
self.mainpanel.add(exitBtn)
self.frame.add(self.mainpanel)
# I've found that packing the frame is a must, though I haven't seen many examples
# that use it. Maybe I'm missing something.
self.frame.pack()
def choiceBox(self,label):
checkBox = awt.Checkbox(label)
numberBox = awt.TextField()
panel = awt.Panel(awt.GridLayout(1,2))
return checkBox,numberBox
def exit(self,e):
sys.exit(1) #here is where it shuts everything down.
pass
def doTest(self,e):
# this pops up a dialog
dlg = MsgDlg("Running Tests")
for job in self.cb.keys():
if job.getState():
print job.getLabel()+" --> "+ self.cb[job].text
time.sleep(2)
# remove the dialog
dlg.frame.removeNotify()
class MsgDlg:
def __init__(self,msg):
self.frame = awt.Frame("Doing Tests", visible=1)
self.panel = awt.Panel(awt.GridLayout(1,1))
self.panel.add(awt.Label(msg))
self.frame.add(self.panel)
self.frame.pack()
if __name__ == '__main__':
# this starts the main window up
app = App()