How to set width for GridData? - java

I was building an UI by using GridLayout/GridData. The layout supposes to have 3 section, one is the headingComposite, one is the leftEditorComposite, one is the rightEditorComposite. But my problem here is, I dont want my leftEditorComposite/rightEditorComposite's width layout be changed by the content. (like the name textbox, if the input get longer, then the leftEdtiorComposite's width will become longer too.) Is there a way to make them as a fix size width? thanks a lot!
GridLayout layout = new GridLayout(2, false);
mainComposite.setLayout(layout);
scrolledComposite.setContent(mainComposite);
GridData data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
data.horizontalSpan = 2;
headingComposite = new Composite(mainComposite, SWT.BORDER);
headingComposite.setBackground(getBackground());
headingComposite.setLayoutData(data);
data = new GridData(GridData.FILL_BOTH);
leftEditorComposite = new Composite(mainComposite, SWT.BORDER);
leftEditorComposite.setBackground(getBackground());
leftEditorComposite.setLayoutData(data);
data = new GridData(GridData.FILL_BOTH);
rightEditorComposite = new Composite(mainComposite, SWT.BORDER);
rightEditorComposite.setBackground(getBackground());
rightEditorComposite.setLayoutData(data);

If you know the size of each composite, you could provide a widthHint for both your Composite objects.
leftEditorComposite.widthHint = 200;
and
rightEditorComposite.widthHint = 200;
Alternatively, you can read this question, and hopefully you will get your answer:
How to align two composites in a parent composite without using widthHint and heightHint

Related

ScrolledCompsite in TabFolder sizes content to zero

I have the following problem:
I'm using SWT to create a GUI for my application. I have a TabFolder in which I add several TabItems and in each of those I create a ScrolledComposite that holds some content.
The TabFolder is displaying fine, however the ScrolledComposite in the TabFolder does only show it's content in the first TabItem. All other ScrolledComposites are visible themselves just fine but their content is invisible.
Here is a little code snippet that demonstrates what I am referring to:
Display display = new Display();
Shell topShell = new Shell(display);
topShell.setSize(800, 800);
topShell.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
topShell.setLayout(new FillLayout());
TabFolder folder = new TabFolder(topShell, SWT.NONE);
for (int i = 0; i < 5; i++) {
TabItem item = new TabItem(folder, SWT.NONE);
item.setText("Item " + i);
ScrolledComposite scroller = new ScrolledComposite(folder,
SWT.H_SCROLL | SWT.V_SCROLL );
scroller.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Composite content = new Composite(scroller, SWT.NONE);
content.setBackground(display.getSystemColor(SWT.COLOR_RED));
scroller.setContent(content);
scroller.setExpandHorizontal(true);
scroller.setExpandVertical(true);
item.setControl(scroller);
}
topShell.setVisible(true);
while (!topShell.isDisposed()) {
display.readAndDispatch();
}
You can tell that the content is being displayed if the area is painted red. If the content is invisible the area is blue (the background of the ScrolledComposite)
I'm not sure if that matters but this occurs on Linux Mint 18 and it appears to only happen within GTK 3 (in 2 it works just fine)
After quite some time I tracked the issue down to the following:
It turned out that the problem was that the "missing" content had a size of zero, because the layouting doesn't set the size of those.
In my case it could be fixed by removing the SWT.V_SCROLL and the SWT.H_SCROLL style constants from the ScrolledComposite. Therefore the above code written as following works as expected.
Display display = new Display();
Shell topShell = new Shell(display);
topShell.setSize(800, 800);
topShell.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
topShell.setLayout(new FillLayout());
TabFolder folder = new TabFolder(topShell, SWT.NONE);
for (int i = 0; i < 5; i++) {
TabItem item = new TabItem(folder, SWT.NONE);
item.setText("Item " + i);
ScrolledComposite scroller = new ScrolledComposite(folder,
SWT.NONE);
scroller.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Composite content = new Composite(scroller, SWT.NONE);
content.setBackground(display.getSystemColor(SWT.COLOR_RED));
scroller.setContent(content);
scroller.setExpandHorizontal(true);
scroller.setExpandVertical(true);
item.setControl(scroller);
}
topShell.setVisible(true);
while (!topShell.isDisposed()) {
display.readAndDispatch();
}
Although that causes all content to be properly sized it completely removes the ScrollBars of the ScrolledComposite which somehow is not what you want from a ScrolledComposite.
Does anyone know how to fix that or whether that is a bug (that might have been fixed in newer SWT versions)?
I've fixed bugs related to this a few months ago.
Can you try latest SWT master?
http://download.eclipse.org/eclipse/downloads/

eclipse plugin add description to table/tabfolder

I'm building a plugin for eclipse and have the following code to put a tabfolder in my view:
private void constructViewWindow(Composite parent){
final TabFolder tabFolder = new TabFolder(parent, SWT.NONE);
terminologyTab = new TabItem(tabFolder, SWT.NONE);
grammarTab = new TabItem(tabFolder, SWT.NONE);
styleTab = new TabItem(tabFolder, SWT.NONE);
xmlTab = new TabItem(tabFolder, SWT.NONE);
terminologyTable = new Table(tabFolder, SWT.BORDER);
grammarTable = new Table(tabFolder, SWT.BORDER);
styleTable = new Table(tabFolder, SWT.BORDER);
xmlTable = new Table(tabFolder, SWT.BORDER);
addTableColumns(terminologyTable);
addTableColumns(grammarTable);
addTableColumns(styleTable);
addTableColumns(xmlTable);
terminologyTab.setControl(terminologyTable);
grammarTab.setControl(grammarTable);
styleTab.setControl(styleTable);
xmlTab.setControl(xmlTable);
}
which results in the following window:
which is very nice. But I now want to add a description/some additional information to this, something that ends up in the same spot as the '0 items' text in the Tasks view:
Since I'm not sure what this would be called, googling is a bit difficult (e.g. I've tried "add header to table", but a header is obviously something else. "Description" didn't really do the trick either). I guess one could find this in the eclipse source code somewhere, but as I'm new to Java, and diving in the eclipse sources and finding what I need will take quite some time (at least for me), anyone any ideas on how to add this additional line of text above my TabFolder?
Just add a Label to the composite before the tab folder.
private void constructViewWindow(Composite parent){
label = new Label(parent, SWT.LEFT);
// TODO set layout on the layout
label.setText("0 items");
final TabFolder tabFolder = new TabFolder(parent, SWT.NONE);
You may need to set Layout Data on the label to make sure it is wide enough for the text.
Another way is to use the ViewForm class which lets you put controls at the corners of the page.

GUI in design page and GUI when RUN is not the same (Eclipse)

I want to manage the position of component in GUI by easily drag and drop them at design page of Eclipse. This is the GUI I see in design page.(Right click>Test/Preview)
I think after I finish rearrange the component in design page, it will look similar when I RUN the app. But, this GUI appear.
The different appearance make me very hard to adjust the component, for example the width of DAY 2, the height of Medication, DOB : and the green background.
Please let me know if there is any solution to this problem. Thanks.
You can use the GridLayout where each day is a column and the subjects(diagnosis, treatment and so on) are the rows. And you create a composite in each grid cell (like Day1 & diagnosis) which contains your buttons for this day and the subject.
[EDIT]
My suggested implementation is: (SWT.BORDER marks all cells of the toplevel grid)
GridLayout topLevelLayout = new GridLayout();
topLevelLayout.numColumns = 4;
parent.setLayout(topLevelLayout);
// head row
Label label = new Label(parent, SWT.BORDER);
label.setText("Activity");
label = new Label(parent, SWT.BORDER);
label.setText("Day 1");
label = new Label(parent, SWT.BORDER);
label.setText("Day 2");
label = new Label(parent, SWT.BORDER);
label.setText("Day 3");
// new row - first cell
label = new Label(parent, SWT.BORDER);
label.setText("Diagnosis");
// Day1 & Diagnosis
GridLayout cellLayout = new GridLayout();
cellLayout.numColumns = 2;
Composite composite = new Composite(parent, SWT.BORDER);
composite.setLayout(cellLayout);
Button button = new Button(composite, SWT.NONE);
button.setText("ECG");
button = new Button(composite, SWT.NONE);
button.setText("Blood Pressure");
button = new Button(composite, SWT.NONE);
button.setText("Vital signs");
// other subjects of diagnosis at day 1...
// Day2 & Diagnosis
composite = new Composite(parent, SWT.BORDER);
// same layout like for day1 & diagnosis
composite.setLayout(cellLayout);
button = new Button(composite, SWT.NONE);
button.setText("ECG");
button = new Button(composite, SWT.NONE);
button.setText("Labs");
button = new Button(composite, SWT.NONE);
button.setText("Blood pressure");
// other subjects of diagnosis at day 2...
// Day3 & Diagnosis
composite = new Composite(parent, SWT.BORDER);
// same layout like for day1 & diagnosis
composite.setLayout(cellLayout);
button = new Button(composite, SWT.NONE);
button.setText("Stress Tests");
button = new Button(composite, SWT.NONE);
button.setText("Labs");
button = new Button(composite, SWT.NONE);
button.setText("Cardiac rhythm");
// other subjects of diagnosis at day 1...
label = new Label(parent, SWT.BORDER);
label.setText("Treatment");
Things like this happen with any designer. Usually they are an indicator of a location change occuring at runtime as the result of a dynamic parameter of some kind. Make sure that you having the correct padding on each of the buttons, and whatever you are using to contain the buttons in should be using a consistent spacing for each one. Also make sure that alignment within the containers is correct, and that both your buttons and the containers they are in are properly anchored.

How I can add size to text in SWT

I create a label with default text of 'data preview'.
When I tried to change the text ( dnyamic ) to diffrerent text it show me the text only in the size of 'data preview'.
How i can control on the size of the label ?
final Composite previewHeader = new Composite(this.previewPanel, SWT.NULL);
final GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.marginHeight = 0;
layout.marginWidth = 0;
previewHeader.setLayout(layout);
final GridData headerData = new GridData();
headerData.horizontalAlignment = GridData.FILL;
headerData.grabExcessHorizontalSpace = true;
previewHeader.setLayoutData(headerData);
this.previewLabel = new RichTextLabel(previewHeader, SWT.LEFT);
this.previewLabel.setText("<b>Data Preview:</b>"); //$NON-NLS-1$
final GridData labeldata = new GridData();
labeldata.grabExcessHorizontalSpace = true;
labeldata.verticalAlignment = GridData.VERTICAL_ALIGN_END;
labeldata.verticalSpan = 0;
labeldata.verticalIndent = 0;
this.previewLabel.setLayoutData(labeldata);
You need to call layout() on the parent of the text. Here is the javadoc of Composite#layout():
If the receiver has a layout, asks the layout to lay out (that is, set the size and location of) the receiver's children. If the receiver does not have a layout, do nothing.
This solves the problem because of the following reason:
The size of each child is determined every time the parent has to lay out. When you first add your label, the parent will initiate the size computation for the child. When you later on change the text, this is not done automatically, so you have to tell the parent to lay out again.

SWT: scrollable area within a tab

I am trying to add a scrollable area to my tabbed window. So far I have a CTabFolder in a shell. I have added 5 CTabItems to it and everything works as expected.
On one of my CTabItems the contents are too big to fit on the screen so I would like to be able to scroll. The contents is a collection of Groups each containing various widgets.
So the CTabFolder is created as follows:
CTabFolder tabs = new CTabFolder(shell, SWT.BORDER);
tabs.setSimple(false);
tabs.setUnselectedImageVisible(false);
tabs.setUnselectedCloseVisible(false);
tabs.setMinimizeVisible(false);
tabs.setMaximizeVisible(false);
FormData tabsLayoutData = new FormData();
tabsLayoutData.top = new FormAttachment(0, 5);
tabsLayoutData.left = new FormAttachment(0, 5);
tabsLayoutData.bottom = new FormAttachment(92, 0);
tabsLayoutData.right = new FormAttachment(100, -5);
tabs.setLayoutData(tabsLayoutData);
Then the CTabItem:
CTabItem tab = new CTabItem(tabs, SWT.NONE);
tab.setText("Role");
Then the contents:
Composite tabArea = new Composite(tabs, SWT.V_SCROLL);
tabArea.setLayout(new FormLayout());
tab.setControl(tabArea);
So the groups contained within the tab are created with tabArea as the parent and everything appears as you would expect. The problem is though that the vertical scroll bar is always present but doesn't seem to do anything. The contents are chopped off at the bottom of the tabArea composite.
Is there anything else I need to do to get the scrolling to work properly?
You need to use a ScrolledComposite. (Scroll down and find links to JavaDoc & snippets)
Here is code that should work in your situation:
ScrolledComposite scroller = new ScrolledComposite(tabs, SWT.BORDER | SWT.V_SCROLL);
Composite tabArea = new Composite(scroller, SWT.NONE);
scroller.setContent(tabArea);
// create some controls in TabArea and assign a layout to TabArea
scroller.setExpandVertical(true);
scroller.setExpandHorizontal(true);
scroller.setMinSize(tabArea.computeSize(SWT.DEFAULT, SWT.DEFAULT));
tab.setControl(scroller);

Categories