Hi, I'm having some problems with my program and I'm not sure how to fix them. I have a main method that starts out with a frame.

public class Cryptography
{
	public static void main(String[] args)
	{
			CryptoMainMenu mainMenu = new CryptoMainMenu();
			JFrame frame = new JFrame("Cryptography");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.getContentPane().add(mainMenu);
			frame.pack();
			frame.setVisible(true);
	}
}

In CryptoMainMenu, I've basically just added some panels from my Templates class and a button to primaryPanel. When the algorithm button is pressed it goes to CryptoCategoriesMenu.

public class CryptoMainMenu extends JPanel implements ActionListener {
public CryptoMainMenu()
	{	
		Templates template = new Templates();

		//setting up the primary panel
		primaryPanel = new JPanel();
		primaryPanel.setLayout(new BorderLayout());
		
		//setting up algorithm button
		algorithm = new JButton("Algorithm");
		algorithm.addActionListener(this);
		
		getContentPane().add(primaryPanel);
		setSize(730, 400);

	}
}

My first problem is that I get the error IllegalArguementException: adding window to container. I'm not understanding why I'm getting this error though. I've had similar programs before and they worked.

public class CryptoCategoriesMenu extends JFrame implements ActionListener
{
if (event.getSource() == back)
		{
			CryptoMainMenu main = new CryptoMainMenu();
			main.setVisible(true);
			this.setVisible(false);
		}
}

My second problem is that I can't use getContentPane() in CryptoMainMenu if I'm extending JPanel but if I get rid of it and just have add(primaryPanel), then my program doesn't work because I linked all of my GUI classes together, so that when it gets to CryptoCategoriesMenu, and if I try pressing back, CryptoMainMenu shows as blank.

Why do you have primaryPanel at all? CryptoMainMenu is a panel, so just do the layout and add the button directly on that.

Why do you have primaryPanel at all? CryptoMainMenu is a panel, so just do the layout and add the button directly on that.

I have primaryPanel because I'll be in the same situation as if I only had add(primaryPanel).

primaryPanel.add(headingPanel, BorderLayout.NORTH);
		primaryPanel.add(leftSubPanel, BorderLayout.WEST);
		primaryPanel.add(rightSubPanel, BorderLayout.EAST);
		headingPanel.add(heading, BorderLayout.NORTH);
		leftSubPanel.add(algorithm);
		leftSubPanel.add(searchField);

When I get rid of primaryPanel and just have add, it'll show the first time but when I go to CryptoCategoriesMenu and press back, it'll disappear.

OK, I haven't studied your code enough to see why. But is you really need that extra panel just add it

public CryptoMainMenu()
   ...
   add(primaryPanel);

the getContentPane fails because panels don't have, or need, content panes.

If I just do add(primaryPanel), then in CryptoCategoriesMenu when I press the button to try going back, CryptoMainMenu doesn't show.

When I extend JFrame in CryptoMainMenu instead of JPanel and use getContentPane().add(primaryPanel) and not use my main method, that'll work but I wasn't sure if I should have a program without a main method.

If I just do add(primaryPanel), then in CryptoCategoriesMenu when I press the button to try going back, CryptoMainMenu doesn't show.

When I extend JFrame in CryptoMainMenu instead of JPanel and use getContentPane().add(primaryPanel) and not use my main method, that'll work but I wasn't sure if I should have a program without a main method.

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.