Help with newbie question please


#1

Why does this code placed in the main component constructor not work?

	LoadComponent1 = new LoadComponent;
	addAndMakeVisible (LoadComponent1);

	int Width = LoadComponent1 -> getParentWidth ();
	DBG (T("Width is ") + String (Width));
	int Height = LoadComponent1 -> getParentHeight ();
	DBG (T("Height is ") + String (Height));
	LoadComponent1 -> setSize (200, 300);
	LoadComponent1 -> setTopRightPosition (Width - 10, Height - 10);

Width and Height return 0…

I can get LoadComponent1 to display if I use absolute values for width and height

I have also tried using the MainComponent members getWidth and getHeight in the constructor but they also return 0

The only place I have found that they return the size of the MainComponent is in the paint member

What is going on here?


#2

in the time of the creation, the components width/height is may not set.
Do all you resizing stuff in the resized() callback.


#3

Ok but resizing is a dynamic process and I get how that would work but I am more interested in the component creation process. So are you saying that the only way to size the document window/content component and then child group components initially is with absolute values?

So here is the scenario, I am creating a number of group components that will be used as child components to the main component. I get this sizes of the child group components the way I want and then start placing them in the main component. If I decide I want to change the size of the main component, then I will have to go through and update the positions of all the child components. I suppose I could try using the relative position methods but that seems like a bunch of trial and error too.

I guess I am just not looking at this the “right” way but it seems to me that I should be able to make the positions of the child components a function of the main component size in terms of pixels not percentages.


#4

You can make them whatever kind of function you want - just set them in resized(), rather than in the constructor, because you have no way of knowing the parent’s size until resized() is called.


#5

No!

just do this in the resized callback of the main( or parent) component! it will be called after object creation, and every time you change its size.


#6

OK thanks


#7

Ok i have done that and it works just fine, thanks for the tip. You might want to add a note about the resizer getting called on component construction, right now it just says it is called when component is resized…or even better put the note in the component constructor info as associating resizer with initial creation is not very intuitive


#8

I don’t understand what you mean… resize() is indeed only called when the component is resized. It’s not called during construction (unless of course you set your component’s size during construction)


#9

OK, now I am confused. How and where is a components initial size normally set if not in a constructor. At what point after creating a component do it’s getWidth and getHeight methods return valid data?


#10

It’s really pretty simple…

A newly created component has size (0, 0) until something calls setSize() or setBounds() to change it.

I wouldn’t generally recommend setting the size in a constructor - let the parent component decide where to size and position its children, rather than trying to bake that logic directly into the child components themselves.


#11

Ok here is what I am doing, this is based on the starting point tutorial…

I the application startup code, I create a new main window (which is a document window) in the initialise method. I also set the main window size and visibility in this method.

In the main window constructor, I create a new main component which apparently is sized automatically by the main window setContentComponent method. Is this where the main component resizer method is getting called (because it is getting called from somewhere)

If this is the case, then it makes sense that the main component getWidth and getHeight methods are invalid in it’s constructor as the setContentComponent method is called after the main component constructor.

This seems to make sense…

Ok in looking at the documentation, I think I see where the confusion for me is. The component methods setSize and setBounds say that they call resizer if a components size is changed (I assume the default values are 0 when a component is created) but the setContentComponent doesn’t specifcally say that resizer is called but it makes sense that it would be.

I think I have got this now… thanks for the feedback


#12

Thanks Jules, I am sure it seems simple to you but to someone starting out it is not quite so clear.

Anyway, I get it now. The confusion was more around the Document windows/Main component sizing and resizing with the setComponentContent method.

Thanks


#13

Ah, I see. Yes, the document window/content stuff is a bit more complicated than the basic component resizing.