• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I use methods from tabs in a JTabbedPane.

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you asking (1) how to iterate over the tabs, or (2) how to do something with a tab once you've got a reference to it as a Component?
 
Charles Mulloy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to reference the component in the tab, which will always be a JPanel.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

CharacterPanel myPanel = (CharacterPanel) myTabbedPane.getComponentAt(0);

That "(CharacterPanel)" is the cast. You might want to read these two articles, and then I guarantee you'll understand this stuff perfectly:

http://www.javaranch.com/campfire/StoryCups.jsp
http://www.javaranch.com/campfire/StoryPassBy.jsp
 
Charles Mulloy
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome! It works perfectly. Now I have to figure out an easy way to delete the tabs and I'll be on easy street.

Also thanks for the links. They help immensely.
 
reply
    Bookmark Topic Watch Topic
  • New Topic