Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Help with JPanel

    Hoping for a little expert guidance. I'm new to programming so am trying to get a feel for the basics. At the moment i'm trying to make a basic GUI with different layout types on different JPanels and am running into some trouble and don't quite understand where my problem is. The goal is to make a GUI with four JPanels in a BorderLayout with various buttons asserted to each. The problem is when I compile all i get is a grey screen. I've tried commenting out various portions of the code to see where the trouble might be and on occasion it will behave the way i expect it to but i can't find any common thread here. Any insight would be appreciated.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;


    public class Panel extends JFrame {

    private static final long serialVersionUID = 5509155261502497671L;

    public Panel(){

    setSize(1280,720);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBackground(Color.DARK_GRAY);
    setVisible(true);

    JPanel jp1 = new JPanel();
    jp1.setBackground(Color.GRAY);
    jp1.setPreferredSize(new Dimension(0,60));
    JPanel jp2 = new JPanel();
    jp2.setBackground(Color.GRAY);
    jp2.setPreferredSize(new Dimension(0,120));
    JPanel jp3 = new JPanel();
    jp3.setBackground(Color.DARK_GRAY);
    jp3.setPreferredSize(new Dimension(240,0));
    JPanel jp4 = new JPanel();
    jp4.setPreferredSize(new Dimension(240,0));
    jp4.setBackground(Color.DARK_GRAY);


    JButton tbutton1 = new JButton("Button1");
    tbutton1.setPreferredSize(new Dimension(200,50));
    JButton tbutton2 = new JButton("Button2");
    tbutton2.setPreferredSize(new Dimension(200,50));
    JButton tbutton3 = new JButton("Button3");
    tbutton3.setPreferredSize(new Dimension(200,50));
    JButton tbutton4 = new JButton("Button4");
    tbutton4.setPreferredSize(new Dimension(200,50));

    jp1.add(tbutton1);
    jp1.add(tbutton2);
    jp1.add(tbutton3);
    jp1.add(tbutton4);

    GridLayout gl = new GridLayout(4,2,10,10);

    /* JButton lbutton1 = new JButton("Button1");
    JButton lbutton2 = new JButton("Button2");
    JButton lbutton3 = new JButton("Button3");
    JButton lbutton4 = new JButton("Button4");

    jp4.add(lbutton1);
    jp4.add(lbutton2);
    jp4.add(lbutton3);
    jp4.add(lbutton4);*/

    add(jp1, BorderLayout.NORTH);
    add(jp2, BorderLayout.SOUTH);
    add(jp3, BorderLayout.WEST);
    add(jp4, BorderLayout.EAST);

    }

    }

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

    Default Re: Help with JPanel

    In order for a container to use a layout, it must call setLayout(someLayout) passing in the layout into the method. In order for a component to show up on a JFrame window (or any other top level window) it must be added to the top level window or to a component whose ancestor tree ultimately reaches the top level window. I don't see you adding anything to the JFrame, so it doesn't surprise me that it's blank.

  3. #3
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Help with JPanel

    Fubarable, thanks for taking a look. I had thought (perhaps incorrectly) that since my class extended JFrame I didn't need to create an instance of JFrame, and could add JPanels to the JFrame with the following -

    add(jp1, BorderLayout.NORTH);
    add(jp2, BorderLayout.SOUTH);
    add(jp3, BorderLayout.WEST);
    add(jp4, BorderLayout.EAST);

    I'll go ahead and try to create an instance of JFrame and add the JPanels to it and see how that works. Thank you.

  4. #4
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Help with JPanel

    So I tried creating an instance of a JFrame and a BorderLayout, and I seem to be getting the same result. I find if i comment out the button logic it will draw as I expect.

    Help with JPanel-jframe.png

    Modified code is below-

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;


    public class Panel extends JFrame {

    private static final long serialVersionUID = 5509155261502497671L;

    public Panel(){

    JFrame frame = new JFrame();
    BorderLayout border = new BorderLayout();

    frame.setSize(1280,720);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setLayout(border);
    frame.setBackground(Color.DARK_GRAY);
    frame.setVisible(true);

    JPanel jp1 = new JPanel();
    jp1.setBackground(Color.GRAY);
    jp1.setPreferredSize(new Dimension(0,60));
    JPanel jp2 = new JPanel();
    jp2.setBackground(Color.GRAY);
    jp2.setPreferredSize(new Dimension(0,120));
    JPanel jp3 = new JPanel();
    jp3.setBackground(Color.DARK_GRAY);
    jp3.setPreferredSize(new Dimension(240,0));
    JPanel jp4 = new JPanel();
    jp4.setPreferredSize(new Dimension(240,0));
    jp4.setBackground(Color.DARK_GRAY);


    /* JButton tbutton1 = new JButton("Button1");
    tbutton1.setPreferredSize(new Dimension(200,50));
    JButton tbutton2 = new JButton("Button2");
    tbutton2.setPreferredSize(new Dimension(200,50));
    JButton tbutton3 = new JButton("Button3");
    tbutton3.setPreferredSize(new Dimension(200,50));
    JButton tbutton4 = new JButton("Button4");
    tbutton4.setPreferredSize(new Dimension(200,50));


    jp1.add(tbutton1);
    jp1.add(tbutton2);
    jp1.add(tbutton3);
    jp1.add(tbutton4);*/


    /* JButton lbutton1 = new JButton("Button1");
    JButton lbutton2 = new JButton("Button2");
    JButton lbutton3 = new JButton("Button3");
    JButton lbutton4 = new JButton("Button4");

    jp4.add(lbutton1);
    jp4.add(lbutton2);
    jp4.add(lbutton3);
    jp4.add(lbutton4);*/

    frame.add(jp1, border.NORTH);
    frame.add(jp2, border.SOUTH);
    frame.add(jp3, border.WEST);
    frame.add(jp4, border.EAST);

    }

    }

  5. #5
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Help with JPanel

    So I added an instance of FlowLayout and passed it to the JPanel, and it did the trick. Here is the code if anyone else has this issue.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;


    public class Panel extends JFrame {

    private static final long serialVersionUID = 5509155261502497671L;

    public Panel(){

    JFrame frame = new JFrame();
    BorderLayout border = new BorderLayout();
    FlowLayout flow = new FlowLayout(); //THIS FIXED IT************************************************ **********

    frame.setSize(1280,720);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setLayout(border);
    frame.setBackground(Color.DARK_GRAY);
    frame.setVisible(true);

    JPanel jp1 = new JPanel(flow); //PASSED HERE*************************************
    jp1.setBackground(Color.GRAY);
    jp1.setPreferredSize(new Dimension(0,60));
    JPanel jp2 = new JPanel();
    jp2.setBackground(Color.GRAY);
    jp2.setPreferredSize(new Dimension(0,120));
    JPanel jp3 = new JPanel();
    jp3.setBackground(Color.DARK_GRAY);
    jp3.setPreferredSize(new Dimension(240,0));
    JPanel jp4 = new JPanel();
    jp4.setPreferredSize(new Dimension(240,0));
    jp4.setBackground(Color.DARK_GRAY);


    JButton tbutton1 = new JButton("Button1");
    tbutton1.setPreferredSize(new Dimension(200,50));
    JButton tbutton2 = new JButton("Button2");
    tbutton2.setPreferredSize(new Dimension(200,50));
    JButton tbutton3 = new JButton("Button3");
    tbutton3.setPreferredSize(new Dimension(200,50));
    JButton tbutton4 = new JButton("Button4");
    tbutton4.setPreferredSize(new Dimension(200,50));


    jp1.add(tbutton1);
    jp1.add(tbutton2);
    jp1.add(tbutton3);
    jp1.add(tbutton4);


    /* JButton lbutton1 = new JButton("Button1");
    JButton lbutton2 = new JButton("Button2");
    JButton lbutton3 = new JButton("Button3");
    JButton lbutton4 = new JButton("Button4");

    jp4.add(lbutton1);
    jp4.add(lbutton2);
    jp4.add(lbutton3);
    jp4.add(lbutton4);*/

    frame.add(jp1, border.NORTH);
    frame.add(jp2, border.SOUTH);
    frame.add(jp3, border.WEST);
    frame.add(jp4, border.EAST);

    }

    }

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

    Default Re: Help with JPanel

    I was in error in my answer to you previous, I'm very sorry. You did in fact add the components to the JFrame, but you did err in calling setVisible(true) before adding all components to the JFrame. Be sure to make this call after all components have been added, else there's a chance that they won't be shown.

  7. #7
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Help with JPanel

    Quote Originally Posted by Fubarable View Post
    but you did err in calling setVisible(true) before adding all components to the JFrame. Be sure to make this call after all components have been added, else there's a chance that they won't be shown.
    Wow! This was the cause of all the wonkiness i've been seeing. I was seeing instances where the same code would react different when run in succession (being all grey). Thank you!

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2011, 03:38 AM
  2. Change JPanel text of Parent JPanel from JDialog
    By bikashlama in forum AWT / Swing
    Replies: 7
    Last Post: 12-09-2011, 04:47 AM
  3. Adding a jpanel to a customized Jpanel Class
    By trishtren in forum AWT / Swing
    Replies: 7
    Last Post: 04-05-2011, 07:52 PM
  4. Placing a new JPanel over a paint overriden JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-08-2010, 07:00 PM
  5. Replies: 3
    Last Post: 04-21-2009, 12:31 AM

Posting Permissions

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