Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Reusing Panel code in each tabs on a tabbed pane

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reusing Panel code in each tabs on a tabbed pane

    Hi
    Im current developing an app which has two tabs (in the future there may be more added) which have to exact same variables, fields, Jtables, and so on.

    currently i have named everything textbox1 and textbox2, and i pass in a port number to each method used.

    just wondering if theres a way i can set a panel to show twice (or as many times as needed) on each tab of the JTabbedPane. This will help me stop repeating code.

    Thanks
    Kurt


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Reusing Panel code in each tabs on a tabbed pane

    What you probably want to do is make an Object that contains everything under a single tab. Then just create a new instantiation of that Object for each tab, passing in whatever you want into the constructor.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Nov 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reusing Panel code in each tabs on a tabbed pane

    How would i go about doing this. currently everthing is added to the panel and the panel is added to the tab but i tried adding the same panel to the different tabs but it didnt work.

    Thanks
    Kurt

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Reusing Panel code in each tabs on a tabbed pane

    No, you don't want to add the same panel to the tab, it doesn't work that way (which you discovered). Components can only be added to one Container.

    What you could do it write a class that contains (or extends) the JPanel and includes all of the Components that get added to it. Then you create multiple instances of that class and add those instances to the tab.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Nov 2010
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reusing Panel code in each tabs on a tabbed pane

    Right! I have this now.
    I created a seperate class which holds the panel with all the components and added two instances like you said.

    I had to create an array to pass the parameters but it works

    thanks for your help
    Kurt

Similar Threads

  1. Panel Problems?
    By Dellick17 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 22nd, 2010, 01:31 PM
  2. Creating a custom panel:
    By xterradaniel in forum AWT / Java Swing
    Replies: 19
    Last Post: October 3rd, 2010, 07:15 PM
  3. [SOLVED] simple question, coding unrelated, how to delete multiple tabs?
    By Perd1t1on in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 07:44 PM
  4. [SOLVED] How to close all tabs in JTabbedPane?
    By LeonLanford in forum AWT / Java Swing
    Replies: 7
    Last Post: June 28th, 2010, 10:58 AM
  5. JOption Pane help
    By dallas1125 in forum AWT / Java Swing
    Replies: 5
    Last Post: November 18th, 2009, 05:08 PM