How to load a part of composite keeping others static? - java

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?

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.

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

Related

Design a ContextMenu with a TreeView functionality

I want to design a ContextMenu with a TreeView functionality. I didnt found anything how can be done. I want to design a custom TableMenu (that button at the end of table header which can show hide the columns), using a treeView structure in ContextMenu, so the user can show/hide a group of columns, like 1-3, 4-6, and so on, or any kind of grouping, but still keeping the one by one show/hiding. The best way would be a tree structure, so if the user hides the parent all of its children are hidden and vice-versa.
Is there any possibility to adapt somehow the TreeView into ContextMenu?
I would like something like this with CheckBoxes for instance:
Each parent node would be an item that represents a group of columns, and each child would be a column.
P.S.
I know how to create and add a custom ContextMenu as tableMenu (I have got it from here : https://gist.github.com/Roland09/d92829cdf5e5fee6fee9) , I'm interested in how to set a tree structure to context menu instead of the list structure.
I also know that I can add the parent like a "simple" item to the context menu, then handle as a parent, but then I Have to implement every functionality of a TreeItem, but I prefer a much simpler way if it exists.

Vaadin - adding components as elements of another components

I've been using a ComboBox to store some values and make a selection from those values, but the problem is, ComboBox, as it is, only allows one selection at the time and I need multiple selections, ie checkboxes, but that cannot be done via Vaadin. I figured if I could present checkboxes as the elements of the ComboBox, that would solve the issue, except adding components to a component that is not a layout doesn't seem to be possible.
I've done this tutorial https://vaadin.com/docs/-/part/framework/components/components-customcomponent.html
Basically it combines two Vaadin components into one panel and displays them together, but that's not what I need, as I need certain components to be placed inside a parent component.
So what are my options if I'm to do this?
This is not an answer to the question that you are asking (component within a component), but rather the underlying problem that you present. In other words, I believe your question is an example of an XY problem.
I think you want to use a Grid with multi-select turned on. In this mode, check boxes are automatically added to each row and there is a checkbox in the header to allow toggling all on/off, ability to filter, ability to sort columns, etc. See the documentation for more details.

How to refresh the composite while filtering through text field?

I have a section and formtoolkit in one composite. When I enter some text in Search box (Text field) it should filter based on user input and show the results below. I am facing one problem while filtering the data. How to refresh the data in composite when user enters text in Text field.
I want a solution which is same as how it works in
Preferences -> compiler -> Errors/Warnings?
Example:
If I enter "null" in search box it will display all the related "null" values below.
How to achieve this implementation for filtering/refreshing the composite data?
The preference page you reference does this with a Composite containing all the controls to be filtered. The Composite uses the GridLayout layout.
Each control has a GridData layout data. To set a control visible or invisible it uses:
control.setVisible(visible);
((GridData)control.getLayoutData()).exclude = !visible;
Once this has been done it calls:
composite.layout(true, true);
to redo the layout of the Composite completely.
Most of this code is in org.eclipse.jdt.internal.ui.preference.OptionsConfigurationBlock
To do the filtering you create a Text field for the filter and add a ModifyListener to listen to changes in the text.
Each time the text changes you match the text of each control with the filter and set the control visible / invisible as described above. At the end of filtering you do the layout call.

How to create custom list widget?

I want to create custom widget, resembling MS File Explorer thumbnail file view or similar.
The questions are:
1) Should I use/extend Item class? It is not prohibited to extend this class, but simultaneously it is said, that custom SWT widgets are made either of Composite or Canvas. If I want to put image above it's caption, then I probably am to use Composite with table layout. This way I will be unable to extend Item. If I extend Item somehow, then when to decide, how to draw it?
2) Should I implement all input handlers? I.e. so that selection can be moved by keyboard and mouse, multiple selection with Ctrl etc. There is too many code. Can I reuse some premade code for this?

Keeping a collection of SWT Widget objects

I trying to use XML codes to create SWT Widgets.
I want to store these widgets first before adding it into the GUI component, so I created a List for storage of these widgets using
List<Widget> widgets = new ArrayList<Widget>();
However, how do I create this widget without specifying it's parent composite?
Widget newWidget = new Button(null,SWT.RADIO); // Argument cannot be null
In here, I do not want to add it to a parent composite yet, so I specify null, but I am not able to get through.
How can I create this Widget without adding to the parent composite (as I do not have a composite now)?
Well, as javadoc states, IllegalArgumentException is thrown when parent argument is null. What you can do is place your widgets at some invisible composite, and then use org.eclipse.swt.widgets.Control.setParent(Composite parent) to add them to different Composite.

Categories