Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
whenever i create a AWT Frame and i try to add an AWT Panel to it in the Frame constructor, i first get the errors of "instead of Panel.setLayout(), do Panel.getContentPane().setLayout()," and the same for any other things that i do with the Panel.
then I change those things and then it says "IllegalArgumentException: adding a window to a container"
what is this and how can i fix it??

I'm afraid I cannot reproduce this. Is there any way you can post example source? It's possible there may be an unintentional mixing of AWT and Swing going on. Are you using JFrame?

Here is the source code. I make this ClosableFrame class and when i try to extends it, i get the error i described.
below is first the ClosableFrame code, then another class that extends it.import java.awt.*;
import java.awt.event.*;public class ClosableFrame extends Frame {
public ClosableFrame(String title) {
super(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}public void processWindowEvent(WindowEvent event) {
super.processWindowEvent(event); //handle listeners
if (event.getID() ==WindowEvent.WINDOW_CLOSING) {
//if used in applet, call dispose()
System.exit(0);
}}
}
now, the class SavedFrame, which purpose is my learning serialization, that extends ClosableFrame
import java.awt.*;
import java.awt.event.*;
import java.io.*;public class SavedFrame extends ClosableFrame implements ActionListener {
public static void main(String[] args) {
SavedFrame frame;
File serializeFile = new File(serializeFilename);
if (serializeFile.exists()) {
try {
FileInputStream fileIn = new FileInputStream(serializeFile);
ObjectInputStream in = new ObjectInputStream(fileIn);
frame = (SavedFrame)in.readObject();
frame.setVisible(true);
} catch (IOException ieo) {
System.out.println("Error reading file - "+ieo);
} catch (ClassNotFoundException cnfe) {
System.out.println("No such class: "+cnfe);
}
} else {
frame= new SavedFrame();
}}
private static String serializeFilename="SavedFrame.ser";
private CirclePanel circlePanel;
private Button clearButton, saveButton;public SavedFrame() {
super("SavedFrame");
setBackground(Color.white);
setFont(new Font("Serif",Font.BOLD,18));
circlePanel = new CirclePanel();
add("Center", circlePanel);
Panel buttonPanel = new Panel();
buttonPanel.setBackground(Color.lightGray);
clearButton = new Button("Clear");
saveButton = new Button("Save");
buttonPanel.add(clearButton);
buttonPanel.add(saveButton);
add(buttonPanel,BorderLayout.SOUTH);
clearButton.addActionListener(this);
saveButton.addActionListener(this);
setSize(300,300);
setVisible(true);
}public void actionPerformed(ActionEvent avt) {
if (avt.getSource() == clearButton) {
circlePanel.removeAll();
circlePanel.repaint();
} else if (avt.getSource() == saveButton) {
try {
FileOutputStream fileOut = new FileOutputStream("SavedFrame.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.flush();
out.close();
} catch (IOException ioe) {
System.out.println("Error saving frame");
}
}
}}

I was able to run it, though admittedly I had to comment out the CirclePanel stuff. I tested the buttonPanel, and it worked. I invoked setLayout with a new layout manager, and I received no errors.
Is the problem with CirclePanel? If not, perhaps there is a versioning problem (not uncommon in Java). I am using Forte with the 1.4.0 SDK, are you using something different?

well I tried commenting out circlePanel and it still is messing up. can I contact you somehow, such as in an instant message? email me if you don't want to put it in this forum

![]() |
Creating a list
|
Create a table in access ...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |