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

Thread: JPanel not showing in JFrame

  1. #1
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default JPanel not showing in JFrame

    Well I had left GUI's for a while, and now I decided to create one. I got stuck adding my JPanel to my JFrame, and over Google all I found were tons of arguments with revalidate vs validate vs repaint vs awt vs swing. I have already looked at Using Top-Level Containers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components), JPanel (Java 2 Platform SE 5.0) and JFrame (Java Platform SE 6) but that didn't help for this particular problem. I created an SSCCE, here is the code.
    JFrame:
    import javax.swing.JFrame;
    import javax.swing.JComponent;
    public class basicJFrame extends JFrame
    {
      //Constructors
      /**
       * Create a very simple JFrame, create a new instance of ButtonGrid and show it.
       */
      public basicJFrame()
      {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        ButtonGrid jpanel = new ButtonGrid();
        getContentPane().add(jpanel);
        ((JComponent)getContentPane()).revalidate(); //Didn't do anything
        setVisible(true);
      }
      //Methods
      public static void main(String[] args)
      {
        new basicJFrame();
      }
    }

    JPanel:
    import java.awt.GridLayout;
     
    import javax.swing.JPanel;
    import javax.swing.JButton;
    /**
     * JButton Grid using JButtons.  Uses GridLayout
     */
    public class ButtonGrid extends JPanel 
    {
      /**
       * Creates the buttons and adds them
       */
      public void ButtonGrid() 
      {
        setLayout(new GridLayout(3,2));
        add(new JButton("1"));
        add(new JButton("2"));
        add(new JButton("3"));
        add(new JButton("4"));
        add(new JButton("5"));
        add(new JButton("6"));
      }
    }

    My problem is that the JPanel just will not show up in my JFrame. Thanks for any help

    **EDIT**
    This should probably be in AWT / Swing forums.. I can't believe I missed that.
    Last edited by Tjstretch; October 30th, 2011 at 12:58 PM. Reason: Wrong Place


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: JPanel not showing in JFrame

    Hi Tjstretch,

    I have moved this into the correct forum for you, please have a broswe of all forum title for future refence.

    In regards to your problem, you are over complicating it. Your ButtonGrid class doesn't have a constructor, try removing void

    Everyone hates being caught by silly things like that haha!

    Regards,
    Chris

  3. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: JPanel not showing in JFrame

    Thank you for moving it, and as to the constructor, I can't believe I missed that. Sigh, obviously it was to early when I did that >.>

    It works! Setting the thread to solved.

Similar Threads

  1. Dynamically add a Jpanel to a Jframe
    By Closet_Rambo in forum AWT / Java Swing
    Replies: 6
    Last Post: October 3rd, 2011, 03:51 AM
  2. Showing JPanel error
    By Fermen in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 12:12 PM
  3. Making a little Lua script 'generator', JFrame showing up blank.
    By scribbleno1 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 2nd, 2010, 07:00 PM
  4. JPanel in JFrame
    By maele in forum AWT / Java Swing
    Replies: 2
    Last Post: March 8th, 2010, 04:12 AM
  5. need help with ActionListener,JPanel,JFrame
    By amahara in forum AWT / Java Swing
    Replies: 5
    Last Post: February 3rd, 2010, 01:40 PM