how to move the text to the left? - java

I created a toolbar with text field and two toolItem
The problem that the Text is exist in the right scrren and not in the left screen.
How I can put the text in the right screen ?
It should look :
Text item1 item2
ToolBar treeToolBar = new ToolBar(treeComposite, SWT.RIGHT_TO_LEFT);
Text text = new Text(treeToolBar, SWT.NONE);
text.setLayoutData(new GridData(SWT.FILL, SWT.LEFT, false, false));
text.setText("Text");
text.pack();
item1 = new ToolItem(treeToolBar, SWT.PUSH | SWT.RIGHT);
item2 = new ToolItem(treeToolBar, SWT.PUSH | SWT.RIGHT);
treeToolBar.pack();

After you create the text, you also have to create a tab item for it. Remove the text.pack() call and insert this:
ToolItem textItem = new ToolItem(treeToolBar, SWT.SEPARATOR);
textItem.setControl(text);
textItem.setWidth(text.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
This is how ControlContribution does in method fill(ToolBar parent, int index).

Related

Resize toolbar SWT

I am trying to set the height of ToolBar to match other items in the grid layout. Here is the code I am using:
public TestSwt() {
Display display = new Display();
shell = new Shell(display, SWT.SHELL_TRIM);
shell.setSize(800, 800);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
shell.setLayout(gridLayout);
Composite composite = new Composite(shell, SWT.NONE);
composite.setSize(100, 100);
GridLayout childGridlayout = new GridLayout();
childGridlayout.numColumns = 10;
composite.setLayout(childGridlayout);
composite.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
final Link link = new Link(composite, SWT.NULL);
link.setText("Link1");
Link link1 = new Link(composite, SWT.NULL);
link1.setText("Link2");
ToolBar toolBar = new ToolBar(composite, SWT.BORDER | SWT.VERTICAL);
ToolItem item = new ToolItem(toolBar, SWT.PUSH);
item.setText("toolbar");
// Trying to resize toolbar
Point size = toolBar.computeSize(SWT.DEFAULT, link.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
toolBar.setSize(size);
toolBar.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
The output I get is:
The output I want is:
I am sure I am missing something very basic. Any help will be really appreciated.
Edit:
As Andrew suggested I set the GridData as LayoutData. It did resize the toolBar but the text is not visible.
Using the code:
GridData toolBarlayout = new GridData(SWT.LEFT, SWT.TOP, true, true);
toolBarlayout.heightHint = link.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
ToolBar toolBar = new ToolBar(composite, SWT.BORDER | SWT.VERTICAL);
toolBar.setLayoutData(toolBarlayout);
ToolItem item = new ToolItem(toolBar, SWT.PUSH|SWT.TOP);
item.setText("toolbar");
The output is something like:
The GridLayout will allow the link widgets to grow to the height of the row, and will size the height of the row to accommodate the toolbar, and the toolbar wants to be at least as big as it is in your output. For that reason, you might have to explicitly set the height you want for the ToolBar (in pixels) rather than taking it from the Link. But you can put some logging in to see what computeSize() is actually returning.
I would create a GridData for the ToolBar, set the heightHint on the GridData to the height you want, and call toolBar.setLayoutData.
GridData toolBarLayoutData = new GridData();
toolBarLayoutData.heightHint = ...;
toolBar.setLayoutData(toolBaryLayoutData);

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 to reorder Tables

I am learning to work with SWT.
I added two Tables to one Composite using:
TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
tabFolder.setLayoutData(BorderLayout.CENTER);
TabItem tbtmData = new TabItem(tabFolder, SWT.NONE);
tbtmData.setText("data");
scrolledComposite = new ScrolledComposite(tabFolder, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
tbtmData.setControl(scrolledComposite);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
tableComposite = new Composite(scrolledComposite, SWT.NONE);
RowLayout rl_tableComposite = new RowLayout(SWT.HORIZONTAL);
tableComposite.setLayout(rl_tableComposite);
table_2 = new Table(tableComposite, SWT.BORDER | SWT.FULL_SELECTION);
table_2.setHeaderVisible(true);
table_2.setLinesVisible(true);
table_3 = new Table(tableComposite, SWT.BORDER | SWT.FULL_SELECTION);
table_3.setLayoutData(new RowData(261, 45));
table_3.setTopIndex(1);
table_3.setHeaderVisible(true);
table_3.setLinesVisible(true);
scrolledComposite.setContent(tableComposite);
scrolledComposite.setMinSize(tableComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
TabItem tbtmGraph = new TabItem(tabFolder, SWT.NONE);
tbtmGraph.setText("graph");
Canvas canvas = new Canvas(tabFolder, SWT.NONE);
tbtmGraph.setControl(canvas);
Now, in the window I see table_2 in the left and table_3 in the right. How to change the order in a runtime? Adding table_3 first is not an option in my case.
One more question. if i call table.setSize(0, 0); I don't see any changes in table appearance. I tried to call table.redraw() after that, but still, size of the table is not changing. where is my mistake?
You can call:
tableThree.moveAbove(tableTwo);
See the javadoc for more detail.
As for your second question:
Why do you want to set the size to 0? Doesn't Control#setVisible(false) do the trick?

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