Main window position and size

Hello there (!)

I can set the size and position of an App’s main window using i.e. setBounds(50, 50, 400, 300) inside MainWindow… but I cannot use it inside MainComponent. In MainComponent, I can only set the size -using setSize(400, 300)- but NOT the position. Why is that? I tried hard but… nothing.

I need to set BOTH window size and position inside MainComponent code. Is there a way to do this?

Your help is highly appreciated.
Regards, Hugo

@HugoMas the Component class has a setBounds() member. Are you getting an error when calling it? could you post some code, so we can see what might be going wrong?

Uhm, if MainComponent is set as the content component of your MainWindow, then the two are tightly coupled in the sense that the content component always occupies the whole “user area” of the window, meaning that you cannot reposition it inside the window.

If you want to place a component at a specific position inside your window, you should make that component a child of MainComponent. In that case, component.setBounds() will place it according to the desired position and size starting from the top-left corner of the content component

1 Like

Thank you cpr2323 (!) I illustrate the problem for you. We want to write a simple program. When running, the App will display the typical Hello! main window and a button.

In the window the user may press the button (TextButton Component) at anytime. Pressing it will move the window from one place to another onscreen.

That is it. That is the challenge. A program which can re-locate its window to a new place onscreen!

OK, let us write the code–following what the Juce Tutorials say:

First create a Main.cpp file. It contains a MainWindow (the DocumentWindow class). Inside we may include a statement like

centreWithSize(getWidth(), getHeight()); or
setBounds(0, 0, getWidth(), getHeight()); or
setBounds(50, 100, getWidth(), getHeight()); or even
setBounds(getX, getY, getWidth(), getHeight())

Now, we create a MainComponent.h file. It contains a MainComponent (the Component class). Inside the MainComponent constructor we write

setSize(400, 300); // Window size is 400,300

When running this program, we get the window onscreen. Initially, we can position the window at the screen center, or at any other place (0,0 or 100,50 etc.) as we please–no problem. We do that using the different statement-examples mentioned above (inside MainWindow code).

However, and this is the problem, we cannot change the position of the window when we press the button?! The reason being here, at MainComponent, we are allowed to issue only the setSize() method. We cannot issue setBounds() or setTopLeftPosition(). They simply do NOT work. Therefore, only size may be manipulated inside the MainComponent program–not position! Why?

That is major limitation. At MainComponent, we cannot write a button routine that changes the -seemingly fixed/frozen position- of the main window.

Any suggestions, cpr2323? Thanks!

Thank you yfede (!) Well, the idea would be to reposition the whole thing–the DocumentWindow-MainWindow window along with its content–the Component-MainComponent window content, right?

If I may, I will further illustrate ‘the problem’ for you. We want to write a simple program. When running, the App will display the typical Hello! main window and a button.

In the window the user may press the button (TextButton Component) at anytime. Pressing it will move the window from one place to another onscreen.

That is it. That is the challenge. A program which can re-locate its window to a new place onscreen!

OK, let us write the code–following what the Juce Tutorials say:

First create a Main.cpp file. It contains a MainWindow (the DocumentWindow class). Inside we may include a statement like

centreWithSize(getWidth(), getHeight()); or
setBounds(0, 0, getWidth(), getHeight()); or
setBounds(50, 100, getWidth(), getHeight()); or even
setBounds(getX, getY, getWidth(), getHeight())

Now, we create a MainComponent.h file. It contains a MainComponent (the Component class). Inside the MainComponent constructor we write

setSize(400, 300); // Window size is 400,300

When running this program, we get the window onscreen. Initially, we can position the window at the screen center, or at any other place (0,0 or 100,50 etc.) as we please–no problem. We do that using the different statement-examples mentioned above (inside MainWindow code).

However, and this is the problem, we cannot change the position of the window when we press the button?! The reason being here, at MainComponent, we are allowed to issue only the setSize() method. We cannot issue setBounds() or setTopLeftPosition(). They simply do NOT work. Therefore, only size may be manipulated inside the MainComponent program–not position! Why?

That is major limitation. At MainComponent, we cannot write a button routine that changes the -seemingly fixed/frozen position- of the main window.

Any suggestions, yfede? Thanks!

You need to change the position of the topLevelComponent, not the MainComponent.

Just for fun I added this timerCallback to an Application:

void MainComponent::timerCallback()
{
    if (auto* top = getTopLevelComponent())
    {
        auto  area = getParentMonitorArea();
        auto& r = juce::Random::getSystemRandom();
        auto  x = r.nextInt (area.getWidth());
        auto  y = r.nextInt (area.getHeight());
        
        top->setTopLeftPosition (area.getX() + x, area.getY() + y);
    }
}

And startTinerHz (1); in the constructor. Have fun closing the window :wink:

Thanks Daniel, I’ll try this out. But… what do you mean by And startTinerHz (1) ?? - Hugo.

I put that code into a timerCallback. When running the timer the window will jump every second over the screen :wink: Just to demo that it works…

I tried it out–and it works! Thank you very much, daniel (!) Greetings, Hugo.