I was able to get a listener working for a JFrame, but I'm missing something with doing the same thing with a JInternalFrame. I built up some basic code from various samples I found, and I'm hoping someone can tell me what I'm doing wrong in creating the internal frame listener. I made the code as simple as possible, and all I want to do change the size of the internal frame and button when I click on NewIframe>Open.
edit: I think I got it working, but didn't want to leave this hanging. I thought I had to use the InternalFrameAdapter, but I used the ComponentAdapter instead. Now, componentResized is called whenever I change the size of the internal frame I created.
from javax.swing import JButton, JFrame, JPanel,JMenuBar,JMenu,JMenuItem,JInternalFrame,JDesktopPane,JScrollPane
from java.awt.event import ComponentListener,ComponentAdapter
from javax.swing.event import InternalFrameListener,InternalFrameAdapter
class TestApp(JFrame):
def __init__(self):
JFrame.__init__(self,'Main Window',size=(500, 500),defaultCloseOperation=JFrame.EXIT_ON_CLOSE)
self.createDesktop()
self.createMenus()
def createDesktop(self):
self.desktop = JDesktopPane()
self.contentPane.add(JScrollPane(self.desktop)) # This is the same as self.getContentPane().add(...)
def createMenus(self):
bar = JMenuBar()
self.setJMenuBar(bar)
f = JMenu("New IFrame")
newfile = JMenuItem("Open",actionPerformed = self.createInternalFrame)
f.add(newfile)
bar.add(f)
def createInternalFrame(self, event):
iframe = JInternalFrame('InternalFrame', 1, 1, 1, 1, size=(400, 400), visible=1)
iframe.setLayout(None)
c= JPanel()
c.setBounds(10,10,60,60)
a = JButton("Report",actionPerformed=self.onClick)
c.add(a)
iframe.add(c)
iframe.addComponentListener(listen(iframe))
self.desktop.add(iframe)
def onClick(self,widget):
print 'Report'
class listen(ComponentAdapter):
def __init__( self, app ):
print 'listen'
self.app = app
def componentResized( self, ce):
print "Hey, I'm listening!"
#--Change frame size to (450,450)
#--set button bounds to (20,20,70,70)
if __name__ == '__main__':
a = TestApp()
a.setLocation(100, 100)
a.show()
Ignore that I'm not using a layout manager and that I don't check that the frame is already open- I'll fix that later. I just want to get the listener working. Thank you!