How to load a part of composite keeping others static?

半腔热情 提交于 2019-12-23 04:26:50

问题


I have a form in SWT where I have five different composites based on one parent composite.Each of the composite contains different widgets like a single textbox / a combo box / a combination of text and combo etc.

Now the problem is when I click on the button I want to change my third composite to carry a different widget keeping others static.Now I can't reload from the beginning as I want current values of the other widgets to be displayed.How can I fetch only that composite,dispose it and create a new widget in place of that.

Creating and hiding the widget is difficult to consider as it is dynamic to at what place we want to redraw.

Here is the snippet.

    formComposite=new Composite(parentComposite,SWT.BORDER_SOLID);
    formLayout=new GridLayout(5,false);
    fromComposite.setLayout(formLayout)
    item.create(formComposite)  //Here item is the widget (combo/textbox/combination text/combo)

     formComposite1=new Composite(parentComposite,SWT.BORDER_SOLID);
    formLayout1=new GridLayout(5,false);
    fromComposite1.setLayout(formLayout)
    item1.create(formComposite1))

  formComposite2=new Composite(parentComposite,SWT.BORDER_SOLID);
    formLayout2=new GridLayout(5,false);
    fromComposite2.setLayout(formLayout)
    item2.create(formComposite2))

 formComposite3=new Composite(parentComposite,SWT.BORDER_SOLID);
    formLayout3=new GridLayout(5,false);
    fromComposite3.setLayout(formLayout)
    item3.create(formComposite3))

 formComposite4=new Composite(parentComposite,SWT.BORDER_SOLID);
    formLayout4=new GridLayout(5,false);
    fromComposite4.setLayout(formLayout)
    item4.create(formComposite4))

Now how can I replace item3 with a different item to be created keeping others static in their place?


回答1:


Assuming you only have one child for each Composite you can just dispose of the existing control and add the new one and then redo the layout.

item.dispose();

item = new Combo(fromComposite, ... style);

formComposite.layout(true);

You may also have to call layout on the parentComposite.




回答2:


you could do this by using Control.moveAbove() and Control.moveBelow() methods

// create item3replacement
item3replacement.moveAbove(item3);
item3.dispose();
// call layout on parent when all is done

otherwise you will need to write your own Layout to do so. as you are using GridLayout this will be your starting point. you need to add a value for an position to GridData and process this in the GridLayout



来源:https://stackoverflow.com/questions/41953375/how-to-load-a-part-of-composite-keeping-others-static

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!