Still Problem with removeComponent(location)

I am asking rather same question asked before and presumed fixed.

I have been facing same problem of my application not removing some location items especialy when the command comes from the button that resides withing the location to where the componets has to be removed.

But still the wird part is, some other places it works fine, but whan I click a button from layout A and set it to remove layout A and add Layout B to the same slot, nothing happens, but when I click a button from Layout A and set it to remove Layout B and add Layout C in B location it works fine.

Please help me if there is something that i have to do to make it work.

Hi,

Do you have some small example (test case) for reproducing the problem?
…although this sounds like it’s easy to reproduce, having a test case usually speeds up the process a lot.

Best Regards,
Marc

ok here is the code, not exactly the one giving me problem but a small version of same function.

*The application should be replacing panels as you click the buttons to change panels, but the third panel disapears.

import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.CustomComponent;
import com.itmill.toolkit.ui.CustomLayout;
import com.itmill.toolkit.ui.Panel;
import com.itmill.toolkit.ui.Window;
import com.itmill.toolkit.ui.Button.ClickEvent;
import com.itmill.toolkit.ui.Button.ClickListener;

public class MainClass extends com.itmill.toolkit.Application implements
		ClickListener
{
	public Window mainWindow = new Window("Test Project");
	public CustomLayout mainLayout = new CustomLayout("mainLayout");
	

	/**
	 * Initialize Application. Demo components are added to main window.
	 */
	public void init()
	{
		setMainWindow(mainWindow);		
		setTheme("example");
		
		mainLayout.addComponent(new FirstPanel(mainWindow, mainLayout),"panel");
		mainWindow.addComponent(mainLayout);
		

	}

	public void buttonClick(ClickEvent event)
	{

	}
 /**
  * First Panel Class 
  */
	public class FirstPanel extends CustomComponent implements ClickListener
	{
		public Window mainWindow2 = new Window();
		public CustomLayout customLayout;
		public Panel stPanel = new Panel("First Panel");
		public Button stButton = new Button("Show second Panel", this);

		public FirstPanel(Window mainWindow, CustomLayout mainLayout)
		{
			customLayout = mainLayout;
			mainWindow2 = mainWindow;
			stPanel.addComponent(stButton);			
			setCompositionRoot(stPanel);
			
		}

		@Override
		public void buttonClick(ClickEvent event)
		{
			if (event.getSource() == stButton)
			{
				customLayout.removeComponent("panel");				
				customLayout.addComponent(new SecPanel(mainWindow2, customLayout), "panel");
				mainWindow2.addComponent(customLayout);
			}

		}

	}
/** 
MainClass.java

 *
 */
	public class SecPanel extends CustomComponent implements ClickListener
	{
		public CustomLayout customLayout;
		public Window mainWindow2 = new Window();
		public Panel secPanel = new Panel("Second Panel");
		public Button secButton = new Button("Show Third Panel", this);

		public SecPanel(Window mainWindow, CustomLayout mainLayout)
		{
			customLayout = mainLayout;
			secPanel.addComponent(secButton);
			setCompositionRoot(secPanel);
			mainWindow2 = mainWindow;
		}

		@Override
		public void buttonClick(ClickEvent event)
		{
			if (event.getSource() == secButton)
			{
				customLayout.removeComponent("panel");				
				customLayout.addComponent(new ThrdPanel(mainWindow2, customLayout),"panel");				
				mainWindow2.addComponent(customLayout);
			}

		}

	}
/**
 * Third Panel Class 
 */
	public class ThrdPanel extends CustomComponent implements ClickListener
	{
		public CustomLayout customLayout;
		public Window mainWindow2 = new Window();
		public Panel thrdPanel = new Panel("Third Panel");
		public Button thrdButton = new Button("Show First Panel", this);

		public ThrdPanel(Window mainWindow, CustomLayout mainLayout)
		{
			customLayout = mainLayout;
			mainWindow2 = mainWindow;
			thrdPanel.addComponent(thrdButton);
			setCompositionRoot(thrdPanel);
			
		}

		@Override
		public void buttonClick(ClickEvent event)
		{
			if (event.getSource() == thrdButton)
			{
				customLayout.removeComponent("panel");				
				customLayout.addComponent(new FirstPanel(mainWindow2, customLayout),"Panel");
				mainWindow2.addComponent(customLayout);
			}

		}

	}
}

and here is a layout file code

<table>
	<tr>
		<td>
		<div location="panel"></div>
		</td>
	</tr>
</table>

I have combined all class in one class, but the program i have here, has all classes separate.

Attention!! there is a typo in location name on third class “Panel” insteda of “panel” notice capital P.

Hi!

I tried your example, but could not replicate the problem. The example works just ok. What version of IT Mill Toolkit are you using?

Some comments:


mainWindow.addComponent(mainLayout);

It’s a layout, so you should normally set it as the root layout of the Window with
setLayout()
.

In all buttonClick handlers:


public void buttonClick(ClickEvent event) {
    if (event.getSource() == stButton) {
        customLayout.removeComponent("panel");              
        customLayout.addComponent(new SecPanel(mainWindow2, customLayout), "panel");
        mainWindow2.addComponent(customLayout);
    }
}

Adding the customLayout to the main window every time you click a button is obviously an error. Luckily
addComponent()
doesn’t (apparently) add the component multiple times to the window if it’s already there. Please check that you have a newish version of IT Mill Toolkit, some early versions such as 5.0.0 probably had a lot of problems with this sort of things.

I’m not quite sure what your goal is, but if you want to switch to another “screen” or “view” when clicking a button, you might want to replace the root layout of the main window, or a component immediately inside it, and have the custom layout inside that for each screen separately. (Switching the root layout of windows with
setLayout()
just changed a bit in 5.2.4, so you might want to upgrade to the newest version.)

The object relationships in your example also look a bit complex, which might easily lead to containment loops or something else bad. I recommend doing layout switching in a centralized way either in the application class or in a separate screen/view manager class.