When I try to run the file I get this error, amen't sure how to fix it.

Exception in thread "main" java.lang.IllegalArgumentException: illegal component
        at java.awt.Container.addImpl(Container.java:1035)
        at java.awt.Container.add(Container.java:408)
        at EmailWindow.CreateContent(EmailWindow.java:65)
        at EmailWindow.<init>(EmailWindow.java:22)
        at Lab6a.main(Lab6a.java:10)

Heres my code sorry for lack of comments

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class EmailWindow extends JFrame implements ActionListener
	//Declaring the Components
	Container cpane;
	JPanel textPanel;
	JPanel labelPanel;
	JPanel textFieldPanel;
	JPanel buttonPanel;
	JPanel buttonPanel2;
	ColouredPanel p;
	JButton ok,exit;

	public EmailWindow()
		JFrame frame = new JFrame();
		this.setTitle("My Frame");
		this.setSize(800, 800);

	public Container CreateContent()
		cpane = new JPanel(new BorderLayout());
		textPanel = new JPanel(new BorderLayout());
		labelPanel = new JPanel(new GridLayout(2,0));
		textFieldPanel = new JPanel(new GridLayout(2,0));
		buttonPanel = new JPanel(new FlowLayout());
		buttonPanel2 = new JPanel(new GridLayout(1,0));
		p = new ColouredPanel(Color.white, "Nothing yet....");

		JLabel name = new JLabel("Name: ");
		JLabel email = new JLabel("E-mail Address: ");

		JTextField namefield = new JTextField(20);
		JTextField emailfield = new JTextField(40);

		ok = new JButton("OK");
		exit = new JButton("EXIT");




		textPanel.add(labelPanel, BorderLayout.WEST);
		textPanel.add(textFieldPanel, BorderLayout.CENTER);


		buttonPanel.add(buttonPanel2, FlowLayout.RIGHT);

		cpane.add(textPanel, BorderLayout.NORTH);
		cpane.add(buttonPanel, BorderLayout.SOUTH);
		return cpane;


	public void actionPerformed(ActionEvent e)
		Object objectUsed = e.getSource();

		if(objectUsed instanceof JButton)
			if(e.getSource() == ok)

				p.changeText("Pressed Ok");

				p.changeText("Press Exit");

Any help much appericated

see this error occurs when you are trying to insert a component in a content pane which is full like a Panel or Frame in GridLayout or you are trying to insert more components in an unresizeable Frame or there is a fault in absolute positioning

This article has been dead for over six months. Start a new discussion instead.