aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Listeners And OOP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Listeners And OOP" Watch "Listeners And OOP" New topic
Author

Listeners And OOP

ray tucson
Greenhorn

Joined: Nov 29, 2001
Posts: 1
I've been twisting on this problem and I need help. My program has a class with a JFrame. This JFrame class is big with many JTabbedPanes. I also have many classes that extend JPanel. So,
the class with the JFrame implements new instances of the JPanels. My problem is one of the JPanels uses JButtons. The number of JButtons is determined by the number of records returned in a recordset. I'm having trouble attaching an ActionListener to the JButtons Example:
class SomeGUI extends JPanel {
... some GUI code
while(ResultSet.next()) {
button = new JButton("Edit");
... more stuff
}
}
class MasterFrame extends JFrame {
SomeGUI guiONE = new SomeGUI();
JPanel masterPanel = new JPanel();
masterPanel.add(guiONE);
masterPanel.add(guiTWO); // I didn't show guiTWO above but
// you get the idea.
this.getContentPane().add(masterPanel);
}

The part that doesn't work is when I try to add an ActionListener to the JButtons in SomeGUI. If I add an inner class to SomeGUI, I don't see how I'll be able to get that inner class to work with my instance of MasterFrame. If I write an inner class in MasterFrame, I don't see how to add a listener to each instance of Button(because button is created many times in the while loop).
I've hit a wall. Please Help.
Ray
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Ray,
The easiest approach would be to have the SomeGUI handle the button clicks. If it requires a handle to MasterFrame then you need to pass it into the SomeGUI constructor and keep a copy of it as a private class variable. For example:

Regards,
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Listeners And OOP