Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Listeners And OOP

 
ray tucson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic