Forum Stats

  • 3,854,261 Users
  • 2,264,344 Discussions


Error encountered while working with Swing components

Hi ,

I am totally new to java and exploring the Swing class concepts with the help of the below program.Basically add a panel to a frame.

package uicontrols;

import java.awt.*;

import javax.swing.*;

//import javax.swing.JPanel;

public class JPanel extends JFrame


JPanel obj2;

JFrame obj1;

public initialize()


    obj1 = new JFrame();



setSize(300, 300);

        obj2 = new JPanel();



obj2.setSize(300, 100);               




public static void main(String[] args) {

        JPanel jp = new JPanel();





The program runs and show me a Frame Window and on top of it a Panel but gives the below errors as well.Can some pleas help me understand the cause of these errors.

Thanks in advance.

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container

at java.awt.Container.checkNotAWindow(

at java.awt.Container.addImpl(

at java.awt.Container.add(

at javax.swing.JFrame.addImpl(

at java.awt.Container.add(

at uicontrols.JPanel.initialize(

at uicontrols.JPanel.main(


  • Mridula03
    Mridula03 Member Posts: 2
    edited Feb 12, 2018 12:46AM

    Your code is not visible. Moreover you have not specified your requirements clearly. Still if this can help you, here it is....

    1) Set the layout of the content pane to null by calling getContentPane.setLayout(null);

    2) Add the panel to the content pane.

    3) Define the bounds of the panel by calling panel.setBounds(int x, int y, int width, int height) where x,y are the location and the width and height are the dimensions of the panel.

This discussion has been closed.