Hello.

I am constructing a database using GUI. I wanted to have all my containers to be on one window. I am having a problem on how to have these containers on one window. Any suggestions? I am very sorry if the code was seem weird.

Here's the code:

public class EmployeeDatabase extends JFrame{

	public static void main(String[] args) {
		EmployeeDatabase ed = new EmployeeDatabase();
		ed.setVisible(true);
	}
	
	Container main = getContentPane();
	Container frontpage = getContentPane();
	Container node = getContentPane();
	
	JButton addButton;
	
	JLabel d;
	
	public EmployeeDatabase(){
		super("EneBio Employee Database System");
		frontPage();
		/*super("EneBio Employee Database System");
		this.setSize(650, 450);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
		
	}
	
	public void frontPage(){
		this.setSize(650, 450);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frontpage.setLayout(null);
		
		frontpage.add(main);
		frontpage.add(node);
	}
	
	class mainPage{{
		//add button
		addButton = new JButton("New profile");
		addButton.setFont(new Font("Courier New Bold", Font.ITALIC, 15));
		addButton.setLocation(400, 50);
		addButton.setSize(170, 50);
		addButton.setToolTipText("Inserts a new employee's profile.");
		addButton.setBackground(new Color(45, 38, 72));
		addButton.setForeground(new Color(92, 224, 132));
		//addButton.addMouseListener(new addButtonListener());
		main.add(addButton);
		main.setVisible(true);
		node.setVisible(false);
	}

	class newProfile{{
		d = new JLabel("DEPARTMENT: ");
		d.setFont(new Font("Courier New Bold", Font.ITALIC, 16));
		d.setForeground(new Color(92, 224, 132));
		d.setLocation(220, 70);
		d.setSize(150, 30);
		node.add(d);
		node.setVisible(true);
		main.setVisible(false);
	}}

	}
}

The error I always received was kind of Illegal Argument Exception : adding container to itself. How to solve that problem?

Container main = getContentPane();
Container frontpage = getContentPane();
Container node = getContentPane();

...

frontpage.add(main);
frontpage.add(node);

Here you create 3 references to the content pane of your JFrame, then try to add 2 of those references to the third.

I would get rid of all references to main and node variables, and just use frontpage as your reference to the content pane. If you want to add more containers, then create new instances of a Container subclass and add them to frontpage.

EDIT: Also I don't think that mainPage and newProfile should be inner classes, they look like methods to me, but that's a different issue I suppose...

I would get rid of all references to main and node variables, and just use frontpage as your reference to the content pane. If you want to add more containers, then create new instances of a Container subclass and add them to frontpage.

Would you not mind if you explain this to me further? I am really stuck regarding to this matter. Sorry if it sounds demanding on your part.

Also I don't think that mainPage and newProfile should be inner classes, they look like methods to me, but that's a different issue I suppose...

Actually, they were supposed in separate classes. But they always open a new window that's why I decided to put them as inner classes. Well, it causes a lot of errors. :(

Would you not mind if you explain this to me further? I am really stuck regarding to this matter. Sorry if it sounds demanding on your part.

Here is how you add containers to your content pane:

Container frontpage = getContentPane();
Container anotherContainer = new Container();
frontpage.add(anotherContainer);

The getContentPane() call gets a reference to the Window's main drawing area (known as the content pane). You were trying to get three references to it (ie all the same object) and then add itself to itself. What I think you actually wanted to do was create another container and add it to the content pane.

Actually, they were supposed in separate classes. But they always open a new window that's why I decided to put them as inner classes. Well, it causes a lot of errors. :(

It causes lots of errors because your syntax is completely wrong. You are trying to write code in a class that executes like a method. If it is supposed to be a separate class, then create another class with methods in it that perform what you are trying to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.