# Switching between windows

**URL:** <https://forum.juce.com/t/switching-between-windows/32603>\
**Category:** General JUCE discussion\
**Created:** [March 31, 2019, 3:50pm UTC](https://forum.juce.com/t/switching-between-windows/32603 "2019-03-31T15:50:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chi79](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chi79/32/6077_2.png) [@Chi79](https://forum.juce.com/u/Chi79)\
**Post date:** [March 31, 2019, 3:50pm UTC](https://forum.juce.com/t/switching-between-windows/32603/1 "2019-03-31T15:50:18Z")

</div>

I have been reading bits and pieces about adopting an MVC style approach to UI in Juce.

A very brief example of this is given in a presentation at approx 27 mins into the video: [Modern GUIs with Juce](https://www.youtube.com/watch?v=YW1HS-hQY78&t=1655s)

I have used both MVP and MVC for presentation in .Net and enjoy the separation of view from non-UI related logic and would like to experiment with this when working in Juce.

It seems like the `Component` class could be set up to behave as a ‘view’ similarly to the way `MainComponent` is used in all the tutorials I have been following. A ‘Controller’ class could then be defined to accept a ‘view’ and listen/react to components from the view.

What I am struggling to understand is how to switch between views. I’m used to web apps where some action like a button click in a view would be sent to a controller, the controller would then redirect to another url and show another view - here I am a little lost.

From what I can gather the `setContentOwned` method of the `ResizableWindow` is what I would need to pass a view to. So what I have tried is…

1. Create another Component just like the `MainComponent` but with a few small changes - lets call this `OtherMainComponent` just for example.
2. Declare a `DocumentWindow` in `MainComponent` and `OtherMainComponent` like:

```auto
//in MainComponent
DocumentWindow window{"Other Window", Desktop::getInstance().getDefaultLookAndFeel()
		.findColour(ResizableWindow::backgroundColourId), DocumentWindow::allButtons };

```

and:

```auto
// in OtherMainComponent
	DocumentWindow window{ "Main Window", Desktop::getInstance().getDefaultLookAndFeel()
		.findColour(ResizableWindow::backgroundColourId), DocumentWindow::allButtons };

```

1. Inside a button click event in `MainComponent` I then call the `setContentOwned` like:

```auto
		window.setUsingNativeTitleBar(true);
		window.setCentrePosition(600, 400);
		window.setVisible(true);
		window.setResizable(false, false);
		window.setContentOwned(new OtherMainComponent(), true);

```

What happens when I run the project and click the button is that a new window opens with the correct UI, but now both `MainComponent` and `OtherMainComponent` are both displayed in their own window and when I close a window I get an exception because I have not overidden the `DocumentWIndow::closeButtonPressed` method.

I have a strong suspicion that I am going about this all wrong - but I can’t seem to find any tutorials about switching between windows.

Grateful if anyone can point me in the right direction 😉

PS - I tried the following using `ResizableWindow` instead of `DocumentWIndow` at stage 3:

```auto
		ScopedPointer<ResizableWindow> window = new ResizableWindow("Other Window", true);
		window->setUsingNativeTitleBar(true);
		window->setCentrePosition(600, 400);
		window->setVisible(true);
		window->setResizable(false, false);
		window->setContentOwned(new OtherMainComponent(), true);

```

But then I see another window quickly flash and dissapear whilst the original window remains displayed.
