Exception : adding a window to a container. How to solve it?
Solution 1
What does this mean..
One top level container (dialog) cannot be added to another (frame).
..and how can i solve this ?
Just call setVisible(true)
on the Preferences
dialog, rather than adding it.
Solution 2
You don't add the JDialog to the JFrame, that makes no sense whatsoever since the add(...)
method is for adding components to be displayed in the container, not by the container. You display the JDialog from the JFrame's JButton's ActionListener. You also shouldn't be mixing AWT (Frame) components and Swing components together for no good reason.
Your question suggests that you would benefit greatly by going through the Swing tutorials.
Solution 3
JDialog and JFrame are top-level container. I suggest that you should have to use JFrame
, JInternalFrame
and JDesktopPane
.
Suhail Gupta
"There's nothing more permanent than a temporary hack." - Kyle Simpson "The strength of JavaScript is that you can do anything. The weakness is that you will." - Reg Braithwaite I am on internet Twitter @suhail3 E-mail [email protected]
Updated on January 03, 2022Comments
-
Suhail Gupta over 2 years
I have a
JDialog
class namedPreferences
. This class creates a constructor like:class Preferences extends javax.swing.JDialog { Preferences(java.awt.Frame parent,modal) { super(parent,modal); //...... } }
In my program I want this preferences dialog to open up as I click a button from a
JFrame
form. After I registered the action listener on the button, I wrote the code inside as:Frame fr = new Frame(); Preferences p = new Preferences(fr,false); fr.add(p); fr.setVisible(true);
When I run this code I get the following exception (as I click the button):
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container
What does this mean and how can I solve it?
-
LarsH about 12 yearsHe was actually trying to add it to a Frame rather than a JFrame. Not that it makes much difference to your point.