java.lang.IllegalArgumentException: adding a window to a container

1. Error description


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 com.you.cmdp.frame.CheckBox.(CheckBox.java:42)
	at com.you.cmdp.frame.CheckBox.main(CheckBox.java:62)

2. The reason for the error


public CheckBox()
	{
		frame.setBounds(100, 100, 400, 300);
		frame.setFont(font);
		frame.setForeground(Color.WHITE);
		frame.setBackground(Color.BLACK);
		add(frame);
		
		cbOne.setText("Section 1");
		cbTwo.setText("Section 2");
		cbThree.setText("Section 3");
		cbFour.setText("Section 4");
		
		panel.add(cbOne);
		panel.add(cbTwo);
		panel.add(cbThree);
		panel.add(cbFour);
		
		frame.add(panel);
	}

In the constructor, JFrame is inherited, but add(frame) will not be added again

3. Solution


public CheckBox()
	{
		frame.setBounds(100, 100, 400, 300);
		frame.setFont(font);
		frame.setForeground(Color.WHITE);
		frame.setBackground(Color.BLACK);
		//add(frame);
		
		cbOne.setText("Section 1");
		cbTwo.setText("Section 2");
		cbThree.setText("Section 3");
		cbFour.setText("Section 4");
		
		panel.add(cbOne);
		panel.add(cbTwo);
		panel.add(cbThree);
		panel.add(cbFour);
		
		frame.add(panel);
	}


Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *