Ivan Aldwin A. Cristobal Ivan Aldwin A. Cristobal - 2 months ago 12
Java Question

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container

Okay so, i again seek your help, we have been doing this program for our school project and ended up getting:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container

and the ff errors too:


at java.awt.Container.checkNotAWindow(Unknown Source) at
java.awt.Container.addImpl(Unknown Source) at
java.awt.Container.add(Unknown Source) at
finalProject.TupanessGenre.initialize(TupanessGenre.java:88) at
finalProject.TupanessGenre.(TupanessGenre.java:37) at
finalProject.TupanessHome$2.actionPerformed(TupanessHome.java:55) at
javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at
javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at
javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at
javax.swing.DefaultButtonModel.setPressed(Unknown Source) at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown
Source) at java.awt.Component.processMouseEvent(Unknown Source) at
javax.swing.JComponent.processMouseEvent(Unknown Source) at
java.awt.Component.processEvent(Unknown Source) at
java.awt.Container.processEvent(Unknown Source) at
java.awt.Component.dispatchEventImpl(Unknown Source) at
java.awt.Container.dispatchEventImpl(Unknown Source) at
java.awt.Component.dispatchEvent(Unknown Source) at
java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at
java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at
java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at
java.awt.Container.dispatchEventImpl(Unknown Source) at
java.awt.Window.dispatchEventImpl(Unknown Source) at
java.awt.Component.dispatchEvent(Unknown Source) at
java.awt.EventQueue.dispatchEventImpl(Unknown Source) at
java.awt.EventQueue.access$500(Unknown Source) at
java.awt.EventQueue$3.run(Unknown Source) at
java.awt.EventQueue$3.run(Unknown Source) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown
Source) at
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown
Source) at java.awt.EventQueue$4.run(Unknown Source) at
java.awt.EventQueue$4.run(Unknown Source) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown
Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at
java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at
java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at
java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at
java.awt.EventDispatchThread.pumpEvents(Unknown Source) at
java.awt.EventDispatchThread.pumpEvents(Unknown Source) at
java.awt.EventDispatchThread.run(Unknown Source)


The Affected is the: Game Genre Selection Menu

HOME PAGE:

public class TupanessHome {

private JFrame home;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TupanessHome window = new TupanessHome();
window.home.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public TupanessHome() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
public void initialize() {
home = new JFrame();
home.setTitle("Tupaness - Home");
home.setBounds(100, 100, 338, 322);
home.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
home.setVisible(true);
home.getContentPane().setLayout(null);

JButton btnNewButton = new JButton("Buy Games");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TupanessGenre gen = new TupanessGenre();
gen.initialize();
}
});
btnNewButton.setBounds(29, 134, 111, 51);
home.getContentPane().add(btnNewButton);

JButton btnBuyConsoles = new JButton("Buy Consoles");
btnBuyConsoles.setBounds(172, 134, 111, 51);
home.getContentPane().add(btnBuyConsoles);

JLabel lblMainMenu = new JLabel("Main Menu");
lblMainMenu.setHorizontalAlignment(SwingConstants.CENTER);
lblMainMenu.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
lblMainMenu.setBounds(117, 30, 87, 33);
home.getContentPane().add(lblMainMenu);

JButton btnNewButton_1 = new JButton("Back");
btnNewButton_1.setBounds(103, 250, 89, 23);
home.getContentPane().add(btnNewButton_1);
}
}


FOR THE GENRE MENU:

public class TupanessGenre {

private JFrame genre;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TupanessGenre window = new TupanessGenre();
window.genre.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public TupanessGenre() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
public void initialize() {
genre = new JFrame();
genre.setTitle("Select Genre");
genre.setBounds(100, 100, 252, 300);
genre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
genre.setVisible(true);
genre.getContentPane().setLayout(null);

JFrame frame_1 = new JFrame();
frame_1.setBounds(-10008, -10030, 283, 300);
frame_1.getContentPane().setLayout(null);

JLabel label = new JLabel("Select a Genre");
label.setBounds(24, 23, 80, 14);
frame_1.getContentPane().add(label);

JRadioButton radioButton = new JRadioButton("Racing");
radioButton.setBounds(6, 61, 109, 23);
frame_1.getContentPane().add(radioButton);

JRadioButton radioButton_1 = new JRadioButton("First Person Shooter");
radioButton_1.setBounds(6, 87, 163, 23);
frame_1.getContentPane().add(radioButton_1);

JRadioButton radioButton_2 = new JRadioButton("RTS");
radioButton_2.setBounds(6, 139, 109, 23);
frame_1.getContentPane().add(radioButton_2);

JRadioButton radioButton_3 = new JRadioButton("Sports");
radioButton_3.setBounds(6, 113, 109, 23);
frame_1.getContentPane().add(radioButton_3);

JRadioButton radioButton_4 = new JRadioButton("Fighting");
radioButton_4.setBounds(6, 165, 109, 23);
frame_1.getContentPane().add(radioButton_4);

JRadioButton radioButton_5 = new JRadioButton("Online");
radioButton_5.setBounds(6, 191, 109, 23);
frame_1.getContentPane().add(radioButton_5);

JButton button = new JButton("Proceed");
button.setBounds(80, 228, 89, 23);
frame_1.getContentPane().add(button);
frame_1.setTitle("Select A Genre - USER");
frame_1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
genre.getContentPane().add(frame_1);

JRadioButton rdbtnNewRadioButton = new JRadioButton("Racing");
rdbtnNewRadioButton.setBounds(41, 57, 109, 23);
genre.getContentPane().add(rdbtnNewRadioButton);

JRadioButton rdbtnSimulation = new JRadioButton("Simulation");
rdbtnSimulation.setBounds(41, 83, 109, 23);
genre.getContentPane().add(rdbtnSimulation);

JRadioButton rdbtnFirstpersonShooter = new JRadioButton("FirstPerson Shooter");
rdbtnFirstpersonShooter.setBounds(41, 109, 144, 23);
genre.getContentPane().add(rdbtnFirstpersonShooter);

JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Sports");
rdbtnNewRadioButton_1.setBounds(41, 135, 109, 23);
genre.getContentPane().add(rdbtnNewRadioButton_1);

JLabel lblSelectGameGenre = new JLabel("Select Game Genre");
lblSelectGameGenre.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
lblSelectGameGenre.setBounds(54, 27, 129, 23);
genre.getContentPane().add(lblSelectGameGenre);

JButton btnOkay = new JButton("Okay");
btnOkay.setBounds(61, 228, 89, 23);
genre.getContentPane().add(btnOkay);

JRadioButton rdbtnOpenworld = new JRadioButton("OpenWorld");
rdbtnOpenworld.setBounds(41, 187, 109, 23);
genre.getContentPane().add(rdbtnOpenworld);

JRadioButton rdbtnRealtimeStrategy = new JRadioButton("Realtime Strategy");
rdbtnRealtimeStrategy.setBounds(41, 161, 144, 23);
genre.getContentPane().add(rdbtnRealtimeStrategy);
}
}


We might still ask more questions since the time pressure doesn't really help, and we have a LOT of projects to work on.

Answer

You cannot add JFrame to anothers JFrame content pane (container) and this is what u are doing in line

 genre.getContentPane().add(frame_1);

where both genre and frame_1 are JFrames

if you need some sort of popup window use JDialog or switch to JPanel insteed of frame_1

Comments