Results 1 to 10 of 10
  1. #1
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Question Problem extending JFrame with JTabbedPane in it

    Hi!

    I want to use one JFrame with JTabbedPane in it as a prototype for the few other forms (frames) and got the following code for this prototype:
    Java Code:
    public class ProtoForm extends JFrame
    	{
    	JTabbedPane tabbedPane;
    	JPanel p1;
    	JPanel p2;
     
    	public ProtoForm()
    		{
    		super();
    		setBounds(100, 100, 500, 375);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     		tabbedPane = new JTabbedPane();
    		getContentPane().add(tabbedPane);
     
    		panel = new JPanel();
    		tabbedPane.addTab("Main panel", null, panel, null);
     
     		panel = new JPanel();
    		tabbedPane.addTab("Main panel", null, panel, null);
    		}
    	}
    Here is a code for the extending (inheriting) form:

    Java Code:
    public class NewForm extends ProtoForm
    	{
     
    	public NewForm()
    		{
    		super();
    		setBounds(100, 100, 500, 375);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
    		}
    	}

    The very same code works without any problems when I put JButton's or JTextField's. But JTabbedPane seems to be not inheritable yet :).
    If anyone had same problem or knows how to solve it please leave a message.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Don't subclass JFrame here. You're not extending JFrame's behavior and doing this will diminish your app's flexibility. Rather either subclass JPanel or extend through composition (which is my preference).

    2) If you want to be able to access components contained in your class, give the class public methods just for that purpose. For instance, your top class could have a method like so:
    Java Code:
    public void addTab(String title, Component component) {
       tabbedPane.addTab(title, component);
    }
    This way classes that inherit from your base class can add to the tabbed pane. You will likely need several more similar methods to give your class the functionality you desire.

  3. #3
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    1) Don't subclass JFrame here. You're not extending JFrame's behavior and doing this will diminish your app's flexibility. Rather either subclass JPanel or extend through (which is my preference).

    2) If you want to be able to access components contained in your class, give the class public methods just for that purpose. For instance, your top class could have a method like so:
    Java Code:
    public void addTab(String title, Component component) {
       tabbedPane.addTab(title, component);
    }
    This way classes that inherit from your base class can add to the tabbed pane. You will likely need several more similar methods to give your class the functionality you desire.
    Hi Fubarable!

    Actually I tried to make a function like the one you have in 2 paragraph. But it did not help, though the same example was working with JButtons and other components.
    Thanks, I'll try to do it with JPanel instead of JFrame.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Cybex View Post
    Actually I tried to make a function like the one you have in 2 paragraph. But it did not help, though the same example was working with JButtons and other components.
    Likely there was an error in your attempted implementation, but without the code, it's difficult to say what. You may wish to show your attempt here.
    Thanks, I'll try to do it with JPanel instead of JFrame.
    No prob, but it still won't solve your initial question.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Cross posted

    And here is the reply from the OP when he got friendly advice not to cross post:

    camrick, you are one funny dude.
    I use multiple sources to get my answer faster. Ever heard of it?
    And I posted it simultaneously on both forums.
    Also I still did not receive real answer.
    By the way the OP was asked hours ago for a SSCCE and ignored that advice as well.

  6. #6
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post

    And here is the reply from the OP when he got friendly advice not to cross post:



    By the way the OP was asked hours ago for a SSCCE and ignored that advice as well.
    Somebody, help me!
    There is a stalker here :).

    That SSCCE had nothing to do with the question I asked.
    And if you read I updated my question in the thread you mentioned.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    OP: Most of us don't mind you cross-posting, but we do mind your cross-posting without being up front about it in your initial post(s). If you don't post a link to the main cross-post, we volunteers risk wasting our time answering a question that's already been answered. So if you appreciate our help and respect us, you'd let us know if you've cross-posted and where.

    camrick, you are one funny dude.
    I use multiple sources to get my answer faster. Ever heard of it?
    And I posted it simultaneously on both forums.
    Also I still did not receive real answer.
    This suggests that you really don't respect us, or our help. Is that the message that you're trying to convey? If so, it might induce myself and others not to help you now and in the future. You may wish to revise this statement.

    Much luck

  8. #8
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    OP: Most of us don't mind you cross-posting, but we do mind your cross-posting without being up front about it in your initial post(s). If you don't post a link to the main cross-post, we volunteers risk wasting our time answering a question that's already been answered. So if you appreciate our help and respect us, you'd let us know if you've cross-posted and where.



    This suggests that you really don't respect us, or our help. Is that the message that you're trying to convey? If so, it might induce myself and others not to help you now and in the future. You may wish to revise this statement.

    Much luck
    Actually I did not know about posting a cross-link and camrick just was preaching about usage of multiple sources without saying that I could just add cross-link.
    Never had such problem on any other forum.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Cybex View Post
    Actually I did not know about posting a cross-link and camrick just was preaching about usage of multiple sources without saying that I could just add cross-link.
    Never had such problem on any other forum.
    Well feel free to post elsewhere, but before doing so, you might want to read their policy statements and FAQ. For instance at JavaRanch.com, you'll find this: BeForthrightWhenCrossPostingToOtherSites

  10. #10
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    I solved this problem by removing setLayout() call from the code. Now it works like it should. If you are closing threads with solved problems feel free to close this one.

Similar Threads

  1. Problem-----JFrame
    By HariPrasad in forum AWT / Swing
    Replies: 2
    Last Post: 07-28-2009, 12:34 PM
  2. Problem Adding Content To JTabbedPane
    By JDCAce in forum AWT / Swing
    Replies: 5
    Last Post: 10-18-2008, 09:45 AM
  3. [SOLVED] Problem with extending classes...
    By Bizmark in forum New To Java
    Replies: 4
    Last Post: 04-07-2008, 11:21 PM
  4. JFrame problem
    By vassil_zorev in forum AWT / Swing
    Replies: 1
    Last Post: 01-25-2008, 02:53 AM
  5. JFrame problem
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-11-2008, 05:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •