HI guys,
as I'm gettng into Java GUI again after a long time, I'm trying hard to remember things, but not everything is that clear anymore.
I'm in the process of building a very simple GUI application with a few buttons.
First question is about the JFrame and JPanel. From what I remember - and what I found online - the JFrame is at the top level of the hierarchy, but do I need to have a JPanel in my application or can I put my GUI components directly onto the JFrame (providing my class extends the JFrame of course)? It's a little comfusing for me because I've seen code with a JPanels and GUI components on them but I've also seen JFrames with GUI components on them and I'm not sure why we can do both or when we should do one or the other.
thanks

Recommended Answers

All 5 Replies

Yes, you can put components directly into a JFrame (well, actually there are some panels that are part of every JFrame, and one of those is where your components are placed, but that's hidden from you so you don't need to worry about it unless you have some special requirement. You can just think of it as adding components directly to the JFrame).

You use JPanels to organise and simplify your layout, eg if you have a row of buttons across the bottom its easier to put a panel at the bottom then put the buttons in the panel. If panels don't help the layout then don't use them.

ps You shouldn't extend JFrame unless you have some particular reason to do so. You can just have a class that one or more JFrames as instance variables and that class can populate them, respond to them, make them visible or invisible etc

Thanks. Maybe it's better if we look at some code.
Here is the application (2 files) with no JPanel:

/*CreateFrame.java*/
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.BorderLayout;
public class CreateFrame extends JFrame{
    //create and instantiating new JPanel
    //private JPanel panel = new JPanel();
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private BorderLayout layout;

    public CreateFrame(){
        super("border layout buttons");
        layout = new BorderLayout(5,5);
        setLayout(layout);

        //panel = new JPanel();
        button1 = new JButton("Button1");
        add(button1, BorderLayout.NORTH);
        button2 = new JButton("Button2");
        add(button2, BorderLayout.SOUTH);
        button3 = new JButton("Button3");
        add(button3, BorderLayout.CENTER);      
    }
}//end of CreateFrame    


    /*CreateFrameTest.java*/
import javax.swing.JFrame;
public class CreateFrameTest{
    public static void main(String[] args){
        CreateFrame createFrame = new CreateFrame();
        createFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        createFrame.setSize( 300, 200);
        createFrame.setVisible( true );
    }
}//end of CreateFrameTest

I'm extending JFrame only because that's the way the book is telling me to do it. I suspect that it's preparing me for when extending that class is necessary (presumably the author will be later overriding or extending some functionality of the JFrame class, not sure).
So, let's say now that I want to add those three buttons in a row at the bottom of the window as you said, I'll rewrite this application with a JPanel in. I have a question thought: do I have to add the components to the JPanel the same way I add them to the JFrame (method add()), then give the JPanel a layout, then add the JPanel to the JFrame and add a layout to the JFrame too? Is that the way it works?
thanks

Let's see if I got it right...here is the version with JPanel instead, and I've used two layout managers, a BorderLayout (which I used to place the JPanel at the bottom of the JFrame) and a GridLayout to place the buttons inside the JPanel.
It seems to work, here is a screenshot:
with_jpanel.png

/*CreateFrame.java*/
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
public class CreateFrame extends JFrame{
    //create new JPanel
    private JPanel panel;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private GridLayout gLayout;
    private BorderLayout bLayout;

    public CreateFrame(){
        super("border layout buttons");//cal super class constructor
        panel = new JPanel();//create new JPanel
        bLayout = new BorderLayout();
        gLayout = new GridLayout(1,3);//create new gridbag 1 row and three cols     
        panel.setLayout(gLayout);//set layout of JPanel to gridbag  

        button1 = new JButton("Button1");
        panel.add(button1);//add button to JPanel
        button2 = new JButton("Button2");
        panel.add(button2);//add button to JPanel
        button3 = new JButton("Button3");       
        panel.add(button3);//add button to JPanel   
        setLayout(bLayout);
        add(panel, BorderLayout.SOUTH);//adding the JPanel at the bottom of the JFrame      
    }
}//end of CreateFrame



/*CreateFrameTest.java*/
import javax.swing.JFrame;
public class CreateFrameTest{
    public static void main(String[] args){
        CreateFrame createFrame = new CreateFrame();
        createFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        createFrame.setSize( 300, 200);
        createFrame.setVisible( true );
    }
}//end of CreateFrameTest

Perfect!

cool! Perhaps the choice of layouts might be questionable, but I will slowly get to the gridBagLayout eventually!
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.