Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem displaying components added to a container

Status
Not open for further replies.

gwaihir

Programmer
Sep 13, 2001
4
GB
Hi

I'm having a problem with adding an object derived from a JPanel to a container and having it display correctly.

The relevant pieces of the code are as follows;

...

public class TraceTool extends JFrame implements ActionListener{

...

private Container content;
private ContentPane panel; // a class which extends JPanel and is serializable.

...

public TraceTool() {

...

content = getContentPane();
panel = new ContentPane();
content.add(panel, BorderLayout.CENTER);

...


public void actionPerformed(ActionEvent e) {

Object source = e.getSource();

if (source == fileNew) {

fileDialog.setMode(FileDialog.SAVE);
fileDialog.setTitle("New");
fileDialog.show();
newFile = fileDialog.getFile();
directory = fileDialog.getDirectory();
window.setTitle(appTitle + " - " + newFile);
IOServices.newFile(requirementList, newFile, directory, panel); // serializes ContentPane object.

}
else if (source == fileOpen) {

fileDialog.setMode(FileDialog.LOAD);
fileDialog.setTitle("Open");
fileDialog.show();
newFile = fileDialog.getFile();
directory = fileDialog.getDirectory();
window.setTitle(appTitle + " - " + newFile);
content.remove(panel); // this appears to work
panel = IOServices.openFile(requirementList, newFile, directory); // deserializes ContentPane object.
content.add(panel, BorderLayout.CENTER); // doesn't update display.

...

I have tried adding a JButton to panel before serialization to see if it is displayed after it is deserialized and added to the container (content). I've tested that the serialization is working properly by checking the number of components contained within panel by calling the getComponentCount() method on the deserialized object. Adding one JButton returns 1 etc. although the JButton doesn't appear. Having added components displaying correctly only seems to work if it is done within the constructor.

I've been scratching my head over this for some time and would be grateful for any help. I have the feeling I'm missing something very obvious.

Thanks
 
where do you specify that content has a border layout manager?
bruce21.jpg
 
or is that the default layout, I can't remember!!
bruce21.jpg
 
According to the Sun Java Swing tutorial, the BorderLayout manager is the default.
 
Try explicitly setting the size of the panel you are adding, perhaps it is actually there but is of a size 0,0 or something. It has happened to me a couple of times, but I can't remember the circumstances.
bruce21.jpg
 
The reason I say this is because unless you create a component with a default value (as you would by using a constructor other than the default one) it will be the minimum size. Most components require something adding to them in order to make it worth their while expanding to show the said value/item.
bruce21.jpg
 
I've managed now to get around this by adding components to the container in methods other than actionPerformed() (i.e., by calling other methods from within this method). Indeed, this seems to be what was causing the problem.

Thanks for all the suggestions though :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top