Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Error encountered while working with Swing components

3440279Nov 30 2017 — edited Feb 12 2018

@

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();

    setTitle("FrameDemo");

        setVisible(true);

setSize(300, 300);

        obj2 = new JPanel();

        obj2.setTitle("PanelDemo");

    obj2.setVisible(true);

obj2.setSize(300, 100);               

        obj1.add(obj2);

          

}

public static void main(String[] args) {

        JPanel jp = new JPanel();

              jp.initialize();

     

      

    }}

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(Container.java:490)

at java.awt.Container.addImpl(Container.java:1091)

at java.awt.Container.add(Container.java:1005)

at javax.swing.JFrame.addImpl(JFrame.java:567)

at java.awt.Container.add(Container.java:417)

at uicontrols.JPanel.initialize(JPanel.java:24)

at uicontrols.JPanel.main(JPanel.java:30)

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 12 2018
Added on Nov 30 2017
1 comment
1,725 views