3

I've been working on a Java Swing project where I need to retrieve the object/instance that created a panel in order to call a simple save method particular to that instance.

You have a JFrame with a JTabbedPane that has tabs created by instancing a class which builds a JPanel and adds it to the JTabbedPane, I need to find the specific instance from the selected JPanel/tab on the JTabbedPane to then call it's save method.

Any ideas? Thanks 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's 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.
}
2
  • I think that for the best specific help, you'll need to tell us more of the details of your current code. Are you using code-generation software to create the GUI such as the NetBean's gui creator? Commented Mar 31, 2012 at 20:34
  • 2
    No, I'm doing by hand as to better learn about Java and Swing. Commented Mar 31, 2012 at 20:45

3 Answers 3

1

I figured out all I needed to do was store new tab objects in an ArrayList derp. Thanks for your attempts though guys!

Sign up to request clarification or add additional context in comments.

2 Comments

Congrats on the fix! When you are able, please make sure to mark your answer as 'accepted' so that others may learn from your success. Cheers~
Ooh, before that though, I need to say that the problem was caused by misuse of class extensions. When you extend a class it itself becomes the object you've extended which means you can -in my case- run the save command from it. Just remember the 'this' keyword when referring to the class itself!
0

Rather than just connecting back to the original creator, my approach to this was to create / use an interface that expicitly supports saving. I created something for this in TUS, my sourceforge project

http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/io/filepersist/

Check out Persistable and Persistable2. Of course anything can be a Persistable, but the abstraction let's you get away from explicit ties back to the creator class

1 Comment

I might give this a try, however I'm afraid I'm not the most skilled with interfaces ><
0

You can add a field in the new JPanels that point to the instance of the creator. I don't think there is any such method to point back to parent class in the API.

--EDIT-- You may want to check http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html getSelectedIndex() may be what you are looking for.

4 Comments

I know this sounds dumb, but how would I go about this? I've never used pointers directly, let alone in a language that doesn't have pointers in the typical sense. >< Ty for the reply though!
@user1305495: He means to simply to give your GUI class a variable that holds a reference to its creator, that's all.
I think you have the wrong idea, I'll update the question with some pseudo code to further elaborate as explaining in any other way gives me a bit of a headache...
Dear Edit, I'm afraid the getSelectedComponent/index returns the panel. The entire point of this question was to see if I could get the object from that panel :/ Thanks for the effort though!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.