I am writing a program that will help keep track of characters (like from books, movies, etc). As of right now I have a method that will create a JPanel and add it to the JTabbedPane. I could also probably delete them at will. But because the number of tabs is undetermined at runtime, I have to do this:
The JPanelMaker is a JPanel form that I have. It is my intention to have a method go through each and every currently open tab and get the data from it (JTextFields and such). If possible, I would like a way to use the methods of the component in a particular tab. Kinda like how I do with vectors so I can load a file and be right where I left off.
Is there a way to access methods from component in a specified tab?
This message was edited 1 time. Last update was at by Charles Mulloy
Ernest Friedman-Hill
author and iconoclast
Marshal
The getComponentAt() method gives you the component at a particular index; the indexes run from 0 to (number of tabs) - 1. The return value is a Component, but you can cast it to whatever you know it to be (i.e., a JPanel).
Charles Mulloy
Ranch Hand
Joined: Oct 25, 2009
Posts: 30
posted
0
I have no clue how to cast. I googled it and the tutorials all use arithmetic examples. When I add to the tabbedPane I do this.
CharacterPanel extends JPanel, and getStringFromUser() is a dialog method I use to get strings from the user. How would I cast in this instance?
edit: I think I should also mention that CharacterPanel has a getText() method that returns the text in a JTextField.
This message was edited 2 times. Last update was at by Charles Mulloy
Ernest Friedman-Hill
author and iconoclast
Marshal
Whenever you have a reference whose type is some parent class (like JPanel,) but you know that actually it's pointing to an instance of some subclass (like CharacterPanel), you can use a cast. The cast in this case is telling the compiler that you know something it does not. In this case is would like like