jEdit Community - Resources for users of the jEdit Text Editor
How to exit awt frame politely from jython
Submitted by WGW on Saturday, 10 April, 2004 - 07:20
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()
Comment viewing options
Select your preferred way to display the comments and click 'Save settings' to activate your changes.
Try self.frame.dispose()
by Anonymous on Sat, 10/04/2004 - 13:22
Try self.frame.dispose()
 
Excellent !
by WGW on Sun, 11/04/2004 - 01:39
Thanks! That did the trick, and by solving that niggling problem I can actually go read more about java and hopefully get through these newbie questions.

Your remark led me to http://mindprod.com/jgloss/jgloss.html, which looks like a good reference for someone just learning. I found destroy() and dispose() explained there.
User login
Browse archives
« March 2024  
MoTuWeThFrSaSu
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Poll
Are you interested in language packs for jEdit?
Yes, and I could help maintain translations
26%
Yes, I'd like to have translations
32%
Indifferent
35%
No, that'd be bad (please comment)
7%
Total votes: 1093
Syndication
file   ver   dls
German Localization light   4.4.2.1   82338
Context Free Art (*.cfdg)   0.31   46043
JBuilder scheme   .001   18487
BBEdit scheme   1.0   18108
ColdFusion scheme   1.0   18015
R Edit Mode - extensive version   0.1   17461
Advanced HTML edit mode   1.0   16194
Matlab Edit Mode   1.0   16057
jEdit XP icons   1.0   15220
XP icons for jEdit   1.1   14282