Computing.Net > Forums > Programming > JAVA- adding window to a container ERROR

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

JAVA- adding window to a container ERROR

Reply to Message Icon

Name: Jason Humphries
Date: July 29, 2002 at 07:12:11 Pacific
Comment:

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??



Sponsored Link
Ads by Google

Response Number 1
Name: Jeff J
Date: July 29, 2002 at 15:10:04 Pacific
Reply:

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?


0

Response Number 2
Name: Jason Humphries
Date: July 29, 2002 at 15:30:52 Pacific
Reply:

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");
}
}
}

}


0

Response Number 3
Name: Jeff J
Date: July 29, 2002 at 17:41:12 Pacific
Reply:

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?


0

Response Number 4
Name: Jason Humphries
Date: July 29, 2002 at 19:20:13 Pacific
Reply:

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


0

Response Number 5
Name: Jeff J
Date: July 30, 2002 at 07:20:14 Pacific
Reply:

You can email me with any specific questions you may have. I do not use instant messenging.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Creating a list Create a table in access ...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: JAVA- adding window to a container ERROR

Adding text to a static box in c++ www.computing.net/answers/programming/adding-text-to-a-static-box-in-c/4941.html

Adding music to a webpage www.computing.net/answers/programming/adding-music-to-a-webpage/3614.html

(Java) adding listeners to Image ? www.computing.net/answers/programming/java-adding-listeners-to-image-/2646.html