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 4 of 4

Thread: Imported Class of Components are Hidden

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Imported Class of Components are Hidden

    I'm not sure how to word this, but I did my best in the title. I'm creating a GUI and for simplicity it has a frame, a tabbed pane, a panel, and a button. Originally, they started in the same class, but I want to try splitting them into two classes.

    So now I have this...

    import FirstPanel.java
     
    public class GUI extends JFrame{
    private JFrame frame;
    private JTabbedPane tabs;
    private FirstPanel firstPanel;
     
    //Constructor
    public GUI(){
    frame = new JFrame();
    tabs = new JTabbedPane();
    firstPanel = new FirstPanel();
     
    tabs.add("Tab #1", firstPanel);
    frame.add(tabs);
    //Set frame visible and the size etc...
     
    }
     
    public static void main(String[] args){
    GUI gui = new GUI();
    gui.setVisible(true);
    }

    What is FirstPanel? It's the second class!

    public class FirstPanel extends JPanel{
    private JPanel panel;
    private JButton button;
     
    //Constructor
    public FirstPanel(){
     
    panel = new JPanel();
    button = new JButton("Why can't you see me?");
     
    panel.add(button);
    }
    }

    The output I am looking for is a frame with a tab with a panel with a button.

    Now, when I run GUI, I get a frame with a tab with a panel but there is no button displayed. I thought at first that it was because all components need to be in the same class, but I dismiss that because I am at least getting ONE panel.

    I get no errors or output from this, only a missing button.

    Any help is greatly appreciated!
    Cheers!
    -Polaris395


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

    Default Re: Imported Class of Components are Hidden

    FirstPanel IS A JPanel (it extends JPanel). It also HAS A JPanel (named panel). You're adding the JButton to the HAS A JPanel, but you're adding the IS A JPanel to the JFrame.
    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. The Following User Says Thank You to KevinWorkman For This Useful Post:

    polaris395 (July 25th, 2011)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Imported Class of Components are Hidden

    Oh! Duh! I guess I've been looking at my code to long to notice that, lol.

    Thanks!

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

    Default Re: Imported Class of Components are Hidden

    Quote Originally Posted by polaris395 View Post
    Oh! Duh! I guess I've been looking at my code to long to notice that, lol.

    Thanks!
    No problem. This is one reason people argue for "composition over inheritance". If FirstPanel doesn't actually modify the functionality of JPanel, there's no reason for it to extend JPanel. Instead, just have a getPanel() method that returns the JPanel after whatever initialization is done. I might not be explaining it that great, but a google of "composition over inheritance" brings back some good results.
    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!

Similar Threads

  1. how to position gui components
    By programmer1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 11th, 2011, 01:45 PM
  2. [SOLVED] Add more components from JFrame Form
    By zecute in forum AWT / Java Swing
    Replies: 3
    Last Post: May 11th, 2011, 08:30 AM
  3. Interaction between components.
    By SyntheticD in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 9th, 2011, 07:32 PM
  4. Getting Size of Components
    By aussiemcgr in forum AWT / Java Swing
    Replies: 3
    Last Post: July 26th, 2010, 04:41 PM
  5. How do you layout these components centered
    By robertbob in forum AWT / Java Swing
    Replies: 2
    Last Post: May 25th, 2010, 12:52 AM