Miglayout by dynamically inserting panels

I am creating a Java Swing application using MiG layout. The GUI consists of a series of lines which are custom JPanels, I create an initial layout:

Panel x

Panel y

But later on I want to be able to insert the panel in the middle and pop the rest,

Panel x

Panel z

Panel y

I am currently doing this by iterating over all layout components and re-adding them:

    Map<Component, Object> constraintMap = ((MigLayout)this.getLayout()).getConstraintMap();
    Component[] allComps = this.getComponents();
    this.removeAll();
    for(Component c : allComps){
        if('met some insert condition'){
            this.add('new component to insert', new CC().wrap());
        this.add(c, new CC().wrap());
    }
    this.revalidate();

      

While there aren't many components that feel very befitting, is there a better way to do this? Any help would be greatly appreciated.

+3


source to share





All Articles