Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I should use a JPanel?

Tags:

java

swing

jpanel

What's the difference between:

public class Test {

    public static void main(String[] args) {
        JButton button= new JButton("1");
        button.setVisible(true);
        JPanel panel= new JPanel();
        panel.add(button);
        panel.setVisible(true);
        JFrame frame = new JFrame();
        frame.add(panel);
        frame.setVisible(true);
        frame.pack();

    }
}

and

public class Test {

    public static void main(String[] args) {
        JButton button= new JButton("1");
        button.setVisible(true);
        JFrame frame = new JFrame();
        frame.add(button);
        frame.setVisible(true);
        frame.pack();

    }
}

I know that a JPanel is a container for GUI components but I really don't see the utility of use it. Certainly I'm very wrong but I'm starting with Swing, so... Why I should use a JPanel? What's really the purpose?

like image 595
Vinicius Avatar asked Mar 19 '16 12:03

Vinicius


People also ask

Should I use JFrame or JPanel?

While JPanel is light and is used for organising Graphical User Interface (GUI) components. JFrame is a window used for creating independent GUI applications. While Jpanel is a space where one can put together a group of complex components or operations. Being a window, JFrame contains a title bar.

Why do we use panel in Java?

A panel provides space in which an application can attach any other component, including other panels. The default layout manager for a panel is the FlowLayout layout manager.

What is the use of JPanel in graphical user interface?

JPanel is a generic container that can hold other elements. It can be visible, changing the background color, the image model or having a border, or be invisible, only used for the hierarchy of the content. It is recommended not to place elements directly in a JFrame, but in a JPanel that is placed in a JFrame.


1 Answers

Why I should use a JPanel?

You use a JPanel for one or more of the following benefits:

  • To group components together.
  • To better organize your components.
  • To enable us to use multiple layouts and combine their effects. (For example a GridLayout for number pad, a CardLayout for display panel where you can switch drawings).
  • To break down a large problem into sub-problems. So instead of implementing everything in one big frame, you can concentrate implementing the properties of each individual panels (such as layout, dimension, background image, background color..etc)
  • For reusability if you have customized panels. A common panel can be used by different classes.
  • For easy debugging, since you can test each panel individually.
  • For scalability.
  • For maintainability.

Usually I perceive a JFrame like a real life painting frame and JPanel like a piece of blank paper. We don't paint directly onto the frame. Instead, we paint on a piece of paper, and insert the paper into the frame. Painting directly on the frame is possible, but no one does that.

The same goes with JFrame and JPanel. We can add components directly to the frame, but usually we add it to the panel, then add the panel to the frame.

like image 116
user3437460 Avatar answered Sep 25 '22 16:09

user3437460