How to load dynamic data on a eclipse shell? - eclipse

I want to load details of a user's when clicking their names . So I create a Grid Layout with two columns left hand side is standalone and right hand side data will be loaded based on clicking their names .
JAVA CODE
Display display = PlatformUI.createDisplay();
Shell shell = new Shell(display);
shell.setSize(400, 400);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
shell.setLayout(gridLayout);
shell.setBackground(new Color(display, 255, 255, 255));
final Table table = new Table(shell, SWT.VIRTUAL);
table.setLayout(new GridLayout());
table.setSize(400, 400);
final TableColumn column = new TableColumn(table, SWT.LEFT);
column.setWidth(150);
column.setText("Runnable Services");
table.setItemCount(5);
final String[] services = new String[] { "Franklin", "Christian",
"Richard Levi",
"Dale Steyn", "Chris Gayle" };
table.addListener(SWT.SetData, new Listener() {
public void handleEvent(Event event) {
TableItem item = (TableItem) event.item;
int index = table.indexOf(item);
item.setText(services[index]);
System.out.println(item.getText());
}
});
table.addSelectionListener(new OnClickListener());
alignCenter(shell, display);
shell.open();
I added a selection listener on a table . When click the players I add their details on SelectionListener's widget Selected method .
JAVA CODE
Composite parent = new Composite(table.getParent(), SWT.NONE);
parent.setLayout(new GridLayout(2, false));
Label label = new Label(parent, SWT.NULL);
label.setText("Name :");
System.out.println(table.getSelection()[0].getText());
Text text = new Text(parent, SWT.BORDER);
text.setText(table.getSelection()[0].getText());
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
| GridData.GRAB_HORIZONTAL));
ViewActionDelegate.resize(200, true, 20, table.getShell());
If i did like this on click action each time data will be appended to existing shell . How can I clear the righ hand side details ? . I posted a image below .
How can i do this ? .

Finally I found the answer .
Issue :
I append new elements into already created composite .
Solution:
Clear elements from existing composite before drawing new elements .
Java Code :
if ((composite != null) && (!composite.isDisposed())) {
Control[] children = composite2.getChildren();
for(Control ch : children){
ch.dispose();
}
}

Related

Eclipse SWT Maintaining Same layout in 2 separate composites

Hello Everyone,
I have 2 Groups and each group has 3 Label and Text combinations, as you can see from the above Image.
I would like the text field to grab as much as space possible.hence, I have used grab 'true' only for the text field.
These label and text fields are well aligned inside their own group/composite.
But, when these 2 Groups are considered as a whole, the label and text fields are out of alignment.
Is there a way to align these label and text fields across multiple Groups?
is there a way to align these labels and text fields, without declaring each groups having equal column width, and specifying the number of columns label and text fields occupy?
Below is the code I have used for creating the below groups.
private void createFirstGroup(Composite parent) {
Group firstGroup = new Group(parent, SWT.NONE);
firstGroup.setText("first group");
GridDataFactory.fillDefaults().grab(true, false).applyTo(firstGroup);
GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).applyTo(firstGroup);
String[] labels = new String[]{"Kwd","Variable","Value"};
for(int index=0;index<labels.length; index++){
Label label = new Label(firstGroup, SWT.NONE);
label.setText(labels[index]);
GridDataFactory.fillDefaults().applyTo(label);
Text textField = new Text(firstGroup, SWT.BORDER);
GridDataFactory.fillDefaults().grab(true, false).applyTo(textField);
}
}
private void createSecondGroup(Composite parent) {
Group secondGroup = new Group(parent, SWT.NONE);
secondGroup.setText("second group");
GridDataFactory.fillDefaults().grab(true, false).applyTo(secondGroup);
GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).applyTo(secondGroup);
String[] labels = new String[]{"Count","Upper Comment","Lower Comment"};
for(int index=0;index<labels.length; index++){
Label label = new Label(secondGroup, SWT.NONE);
label.setText(labels[index]);
GridDataFactory.fillDefaults().applyTo(label);
Text textField = new Text(secondGroup, SWT.BORDER);
GridDataFactory.fillDefaults().grab(true, false).applyTo(textField);
}
}

Why composite is expanded by text field?

I created a text field which used GridLayout and I set the 3rd parameter of the GridData (grabExcessHorizontalSpace) to true. The Text field is not getting wrap into multiple line when I set a very long sequence of non-whitespace characters won't be split into lines. The Composite is expanded by text filed.
Below is my code:
Composite composite = new Composite(m_shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
composite.setLayout(new GridLayout(2, false));
Label label = new Label(composite, SWT.LEAD);
label.setText("Label");
label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
Text text = new Text(composite,SWT.MULTI|SWT.BORDER|SWT.WRAP|SWT.V_SCROLL);
GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
gd.heightHint = 2 * text.getLineHeight();
text.setLayoutData(gd);
text.setText("trweghjfbfdshjfghjreuyhjfghkjhjhjsdghjreewhjdfghjhjgfdhjsdghjtreuytrehjdfghjhjdfgh8irtrhjghjhjdfghjdfghjfghjdfg");
I assume you are calling pack on the Shell to complete the layout. This causes controls to be sized at their preferred size - the preferred size for a Text control is the size with no wrapping.
If you want the shell to be a specific size you can layout on the shell without calling pack. The text should then wrap.
Alternatively specify a widthHint for the Text control.
Setting widthHint to 0 together with the horizontalAlignment to SWT.FILL makes the text fill all availible space in its column, but not grow more that that. Instead it wraps as desired:
I tried this on Windows 10 with SWT 3.103.2. Greg seems to have got another result on Mac.
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
composite.setLayout(new GridLayout(2, false));
Label label = new Label(composite, SWT.LEAD);
label.setText("Label");
label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
Text text = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
gd.heightHint = 2 * text.getLineHeight();
// Added widthHint
gd.widthHint = 0;
text.setLayoutData(gd);
text.setText("trweghjfbfdshjfghjreuyhjfghkjhjhjsdghjreewhjdfghjhjgfdhjsdghjtreuytrehjdfghjhjdfgh8irtrhjghjhjdfghjdfghjfghjdfg");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

Use FillLayout with Browser in SWT

I have a view that consists of a heading and a browser. I want the browser to fill my view. This is how it should look:
I have tried out different combinations.
c = new Composite(parent, SWT.NONE);
c.setLayout(new RowLayout(SWT.VERTICAL));
Label l = new Label(c, SWT.NONE);
l.setBackground(color);
l.setText("----------------------Heading--------------------------------");
l.setFont(new Font(null, new FontData(Constants.FONT, 12, SWT.BOLD)));
Composite wrapper = new Composite(c, SWT.NONE);
wrapper.setBackground(new Color(null, 0, 0, 0));
wrapper.setLayout(new FillLayout(SWT.VERTICAL));
browser = new Browser(wrapper, SWT.WRAP);
browser.setBackground(color);
produces:
Setting a FillLayout to c does not work either. By doing so both, heading and browser, share the view's size and have an equal size.
c = new Composite(parent, SWT.NONE);
c.setLayout(new FillLayout(SWT.VERTICAL));
Label l = new Label(c, SWT.NONE);
l.setBackground(color);
l.setText("----------------------Heading--------------------------------");
l.setFont(new Font(null, new FontData(Constants.FONT, 12, SWT.BOLD)));
browser = new Browser(c, SWT.WRAP);
browser.setBackground(color);
produces:
I have also tried the following:
composite with filllayout
-> composite with gridlayout
->composite with rowLayout (fixed height, grab right)
->composite with rowLayout
-> composite with fillLayout
-> composite with browser
Assuming that the header and browser have the same parent, you could use a GridLayout like this:
parent.setLayout( new GridLayout( 1, false ) );
label.setLayoutData( new GridData( SWT.FILL, SWT.TOP, true, false ) );
browser.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
Even though a one-cell-grid isn't really what a GridLayout is meant for, it should do the trick. The layout data advise the label to align at the top and use all horizontal space and the browser to use the horizontal and vertical space

How to put label item on rowLayout in SWT?

I want to put some items on row layout. But I take an error. My error image is here:
my example code is below:
GridLayout parentLayout = new GridLayout(1, true);
parent.setLayout(parentLayout);
// design filter composite layout
Composite filterComposite = new Composite(parent, SWT.NONE);
RowLayout filterCompositeLayout = new RowLayout();
filterCompositeLayout.wrap = true;
filterCompositeLayout.pack = false;
filterCompositeLayout.justify = true;
filterCompositeLayout.type = SWT.HORIZONTAL;
filterComposite.setLayout(filterCompositeLayout);
// design filter composite
Label lbl_type = new Label(filterComposite, SWT.BORDER);
lbl_type.setText("Type :");
Combo cmb_type = new Combo(filterComposite, SWT.BORDER);
cmb_type.setText("-- choose --");
Label lbl_severity = new Label(filterComposite, SWT.BORDER);
lbl_severity.setText("Severity :");
Combo cmb_severity = new Combo(filterComposite, SWT.BORDER);
cmb_severity.setText("-- choose --");
Label lbl_startDate = new Label(filterComposite, SWT.BORDER);
lbl_startDate.setText("Start Date : ");
DateTime dateTimeStart = new DateTime(filterComposite, SWT.DROP_DOWN | SWT.LONG);
dateTimeStart.setLayoutData(new GridData(SWT.NONE, SWT.NONE, true, true));
Error is on DataTime row. Can anybody give some advice? Thank you.
You cant set GridData to component placed in RowLayout. You should use RowData instead:
DateTime dateTimeStart = new DateTime(filterComposite, SWT.DROP_DOWN | SWT.LONG);
RowData rd = new RowData();
rd.width = 123;
rd.height = 23;
dateTimeStart.setLayoutData(rd);
You are attempting to set GridData layout data for dateTimeStart. However Composite filterComposite has RowLayout and this Layout expects RowData layout data.
example:
DateTime dateTimeStart = new DateTime(filterComposite, SWT.DROP_DOWN | SWT.LONG);
dateTimeStart.setLayoutData(new RowData (width, height)); //set proper height and width according to your UI expectations

horizontalSpan in SWT GridLayout doesn't span

I'm trying to configure a dialog window like so:
#Override
protected Control createDialogArea(Composite parent) {
GridLayout dialogAreaLayout = new GridLayout();
dialogAreaLayout.numColumns = 2;
parent.setLayout(dialogAreaLayout);
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
gridData.minimumWidth = 300;
// the label in the first row should span across both columns
gridData.horizontalSpan = 2;
Label headlineLabel = new Label(parent, SWT.NONE);
headlineLabel.setText("example test");
headlineLabel.setLayoutData(gridData);
// the rest should be ordered in two columns
gridData.horizontalSpan = 1;
companyLabel = new Label(parent, SWT.NONE);
companyLabel("company");
companyTextfield = new Text(parent, SWT.BORDER);
companyTextfield(gridData);
...
What I'm trying to accomplish is a label in the first line, that spans across both columns and have the following fields be ordered in pairs per line. What I get with this is, that the lable (that should be left in second line) is right in first line, just as the label wouldn't span across two columns. Can anybody see, what I'm doing wrong?
Thanx again!
After setting the layoutData to headlineLabel, you are setting the horizontalSpan to 1. You need to create a new LayoutData object for every SWT widget. It is not possible to reuse them after setting them.