Retrieving an Object from a Java Swing Component
I was working on a Java Swing project where I need to get the object / instance that the panel created in order to call a simple save method, especially for that instance.
You have a JFrame with a JTabbedPane that has tabs created by creating a class that creates the JPanel and adds it to the JTabbedPane. I need to find a specific instance from the selected JPanel / tab on the JTabbedPane in order to then call its save method.
Any ideas? Thank you for your time!
public class frame extends JFrame implements ActionListener{
Builds a frame dubbed "frame" that is static.
Builds a static JTabbedPane dubbed "pane"and adds it to the frame.
Creates a button that creates a new instance of sheet.
public void actionPerformed(MAGIC!){
See if a button on the panel has been pressed and uses the currently selected tab to locate the correct instance of sheet to run it save method.
}
}
public class sheet extends JPanel{
In constructor makes a JPanel and adds it to "pane"
Describes a save method that outputs a variable unique to the instance.
}
source to share
You can add a field to new JPanels that point to the creator instance. I don't think there is such a method to point to the parent class in the API.
- EDIT-- You can check http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html getSelectedIndex () might be what you are looking for.
source to share
Rather than just hooking into the original creator, my approach to doing this was to create / use an interface that explicitly supports persistence. I created something for this in TUS, my sourceforge project
http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/io/filepersist/
Check Persistable and Persistable2. Of course, everything can be Persistable, but abstraction allows you to get away from explicit links to the creator class.
source to share