SWT TreeViewer resize issue - java

I have 3 treeviewer objects in one view in a column like disposition and I want their size to increase equally if I maximize/minimize the view/eclipse window. Here is my code:
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
treeViewer1 = new TreeViewer(composite, SWT.BORDER);
tree1 = treeViewer1.getTree();
tree1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
tree1.pack();
treeViewer2 = new TreeViewer(composite, SWT.BORDER);
tree2 = treeViewer2.getTree();
tree2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
tree2.pack();
treeViewer3 = new TreeViewer(composite, SWT.BORDER);
tree3 = treeViewer3.getTree();
tree3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
tree3.pack();
However, when the contents of a certain tree are surpass the current viewing space and I maximize/minimize the view, that tree viewing space gets bigger than the others.
Is there a way to prevent this resize behaviour due to content size?
Many thanks,
ND

If you read the documentation of GridLayout, you will find your answer:
Constructs a new instance of this class given the number of columns, and whether or not the columns should be forced to have the same width. If numColumns has a value less than 1, the layout will not set the size and position of any controls.
The solution to your problem is replacing this (since you are talking about columns):
composite.setLayout(new GridLayout(1, false));
with:
composite.setLayout(new GridLayout(3, true));

Related

Resizing label to fit content

I am creating a widget, which will display a text followed by a progress bar. For this I create a Composite
container = new Composite(parent, SWT.BORDER);
container.setLayoutData(layoutData);
container.setLayout(new GridLayout(1, true));
To this I add a Label
messageText = new Label(container, SWT.WRAP | SWT.BORDER | SWT.CENTER);
messageText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
followed by a composite holding the progress bar:
final Composite progressContainer = new Composite(container, SWT.BORDER);
progressContainer
.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
This is the result:
What I would expect is the label to grow as to be able to contain the full text. I have been trying to follow the instructions from this post however, I must be missing something as I am not able to achieve the desired behavior.
Thanks for the input.
The GridData you have specified uses all the available horizontal space but the vertical space only uses the initial size calculated for the control.
To use all available vertical space use:
messageText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Note that you are also specifying that the progressContainer should also grab all available space - this is probably not what you want so you may need to change that as well. Possibly:
progressContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
If you want the message text to resize when you change the text you need to call
container.layout(true);
after setting the new text to force the sizes to be recalculated. Use your original GridData values.

SWT - Not using first and second Column with GridLayout

I'm using SWT, on a desktop application, where I'm using a GridLayout splited by 5 columns and one line, there is a tabble using the top of the space and at the bottom I have 3 buttons, I want to put these 3 botton to the right. Basically jump the two first spaces. I tried using:
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment= GridData.END;
CompanyGroup.setLayoutData(gridData);
But I realized that this method only align inside the column.
Composite rightComp = DialogCompositeFactory.create(parent);
rightComp.setLayout(new GridLayout(1, false));
Group CompanyGroup = DialogGroupFactory.create(rightComp,Message));
CompanyGroup.setLayout(new GridLayout(5, true));
Like the example below
Just put empty label controls in the first two columns.
// Empty first column
new Label(CompanyGroup, SWT.LEAD);
// Empty second column
new Label(CompanyGroup, SWT.LEAD);
... your buttons in the last three columns
I assume you have a composite for buttons, if not create a composite for buttons and set a GridData with HORIZONTAL_ALIGN_END
Composite buttonsComposite = new Composite( groupComp, SWT.NONE );
GridData gd = new GridData( GridData.HORIZONTAL_ALIGN_END );
GridLayout lo = new GridLayout(3,true);
buttonsComp.setLayoutData( gd );
buttonsComp.setLayout(lo);

What does the moveBelow method of Control class do?

I am using a Gridlayout with 2 column. I have Labels and corresponding Text control with it. I wanted the Text control of first label to slip down the label instead of right next to it (since its a gridlayout). For this I thought the moveBelow method would work but doesn't seem to be. Am i interpreting the use of the method wrongly?
Label label = Components.createLabel(myContainer, SWT.LEFT
| SWT.WRAP);
abel.setText("WC Plan Name");
textName = createTextControl(myContainer, SWT.LEFT);
textName.moveBelow(label);
private Text createTextControl(Composite parent, int horizontalAlignment)
{
final Text textControl = Components.createText(parent, SWT.SINGLE | SWT.BORDER);
final GridData layoutData = new GridData(horizontalAlignment, SWT.FILL, false, false);
layoutData.widthHint = 200;
textControl.setLayoutData(layoutData);
return textControl;
}
moveBelow() does exactly what it says in the documentation:
Moves the receiver below the specified control in the drawing order. If the argument is null, then the receiver is moved to the bottom of the drawing order. The control at the bottom of the drawing order will be covered by all other controls which occupy intersecting areas.
This means that it can be used to reorder children (if the layout of the parent allows it). For example, if you have a RowLayout and call moveBelow(null) on the last child, it will be moved to the top.
Now to solve your problem: You have a GridLayout with 2 columns. A GridLayout is filled from top left to bottom right. If you want two elements to appear below each other rather than next to each other, there are two options:
Add an empty Label in between, so that it can occupy the space to the right of your first element
Add a GridData to your first element and set GridData#horizontalSpan to 2. This way it will span two columns.
UPDATE
Here is an example of solution 2:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(4, false));
Text text = new Text(shell, SWT.BORDER);
text.setLayoutData(new GridData(SWT.BEGINNING, SWT.TOP, false, true, 4, 1));
text = new Text(shell, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 4, 1));
for (int i = 0; i < 8; i++)
{
new Button(shell, SWT.PUSH).setText("Button " + i);
}
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Looks like this:

SWT Button image limit

When an image is set on a SWT Button and the image is bigger (in this case the width), the limits of the button are not honored, and the image from one button can "touch" the image from another.
Strangely, if SWT.LEFT or SWT.RIGHT is used, the problem does no occur on the respective side. Tried with SWT.CENTER, without success.
Is there any "workaround" to bypass this problem? Or is just something that any SWT user must get used to?
Composite Code:
GridLayout gridLayout = new GridLayout(1, true);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.verticalSpacing = 0;
setLayout(gridLayout);
setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button Code:
Button button=new Button(this, SWT.PUSH | SWT.WRAP);
GridData gridData=new GridData(SWT.FILL, SWT.FILL, true, false);
gridData.widthHint=size.width;
gridData.heightHint=size.height;
button.setLayoutData(gridData);
I also have a listener associated with the Composite Layout, to adjust the Number of Columns, but i assume that's irrelevant.
Using a Button for this task doesn't seem to be the best choice (because of the borders).
You might be better off using a Label to host the Image. Your code will only change very little, since you only need to replace all occurrences of Button with Label.

SWT - How do you re-size/set size of an a Composite residing inside another composite?

//I have a composite within a scrolled composite.
final Composite c = new Composite(scrolledComposite, SWT.NONE); //1
c.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
c.setLayout(new GridLayout(1, false));
//I have a for loop that adds composites to composite c
for(int i = 0; i<100; i++){
Composite b = new Composite(c, SWT.BORDER);
b.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION));
b.setLayout(new GridLayout(2, false));
b.setBounds(112, 70, 268, 69);
//and then some controllers added to that inner composite
Label lblNewLabel_2 = new Label(b, SWT.NONE);
lblNewLabel_2.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 4));
Image img = new Image(Display.getCurrent(), result.get(i).getPicturePath());
//.... more controllers.
// finally I end the loop and add the outer most composite to an scrolled composite.
} // end loop
scrolledComposite.setContent(c); //setter content!
scrolledComposite.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
This all works great, almost. My problem is that the inner composite wont respond to the setBounds method. No matter what I'm writing in it I cant get it to expand. I suspect this have something to do with the layouts.
Anyone got a clue?
Thanks in advance.
Petter
Layouts are the objects that set size and location of composite's children so you wouldn't have to call setBounds() for every one of them. Check out Understanding Layouts in SWT.
What is the purpose of Layout in SWT applications?
Consider a Resize listener in your outer panel to have it adjust your inner panel.
_outerPanel.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event e) {
computePanelSize(); // computes the inner panel size
}
});

Categories

Resources