What I tried so far:
In createPartControl:
ScrolledComposite sc = new ScrolledComposite(parent, SWT.V_SCROLL | SWT.H_SCROLL);
sc.setLayoutData(new GridData(GridData.FILL_BOTH));
sc.setExpandVertical(true);
sc.setExpandHorizontal(true);
sc.setSize(ApplicationWorkbenchWindowAdvisor.WIDTH, ApplicationWorkbenchWindowAdvisor.HEIGHT);
final TabFolder tabFolder = new TabFolder(sc, SWT.TOP);
but this does not work. My problem is that if I resize my program window the scrollbars does not appear in my view. Any ideas?
Typically in Eclipse views, I want my controls to grab all available space, and only show scrollbars, if otherwise a control would shrink below a usable size.
The other answers are perfectly valid, but I wanted to add a full example of a createPartControl method (Eclipse e4).
#PostConstruct
public void createPartControl(Composite parent) {
ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
Composite composite = new Composite(sc, SWT.NONE);
sc.setContent(composite);
composite.setLayout(new GridLayout(2, false));
Label label = new Label(composite, SWT.NONE);
label.setText("Foo");
Text text = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
GridDataFactory.fillDefaults().grab(true, true).hint(400, 400).applyTo(text);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
Note that .fillDefaults() implies .align(SWT.FILL, SWT.FILL).
I commonly use this pattern, so I created the following little helper method:
public static ScrolledComposite createScrollable(Composite parent, Consumer<Composite> scrollableContentCreator) {
ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
Composite composite = new Composite(sc, SWT.NONE);
sc.setContent(composite);
scrollableContentCreator.accept(composite);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
return sc;
}
Thanks to Java 8 lambdas, you can now implement new scrollable composites in a very compact way:
createScrollable(container, composite -> {
composite.setLayout(new FillLayout());
// fill composite with controls
});
The Javadoc of ScrolledComposite describes the two ways to use it, including example code. To sum them up:
You either set the size of the control/composite contained in your ScrolledComposite on the control/composite itself
Or you tell your ScrolledComposite the minimum size to use for its content.
Currently, you're doing neither. You're setting the size on the ScrolledComposite, but unless you don't use a layout manager, that doesn't make much sense. In any case, see the above link for some official example code.
This is a small piece of code which worked for me:
ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
Composite composite = new Composite(sc, SWT.NONE);
sc.setContent(composite);
Label lblRelation = new Label(composite, SWT.NONE);
lblRelation.setBounds(10, 13, 74, 15);
lblRelation.setText("Label name:");
composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Related
I have a TabFolder in which the TabItems can be created dynamically. I was expecting the tabs to compress like they would in a browser when you have more than will fit on the screen, but they just keep expanding to the right off the screen unless I add a horizontal scroll. Is there a way to make them automatically compress like that?
TabFolder tabsComposite = new TabFolder(builder, SWT.NONE);
tabsComposite.setLayout(new GridLayout(1, false));
GridData tabsLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
tabsComposite.setLayoutData(tabsLayoutData);
These are created via button click in another method and any number of them can be created
TabItem item = new TabItem(tabsComposite, SWT.NONE);
tableComposite = new Composite(parent, SWT.BORDER);
GridData tableLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
tableComposite.setLayoutData(tableLayoutData);
GridLayout tableLayout = new GridLayout(1, false);
tableComposite.setLayout(tableLayout);
item.setText(nameText.getText());
As #greg-449 mentioned, you can accomplish this with a CTabFolder. By default, the CTabFolder will display no fewer than 20 characters when compressed, so you may not have noticed this default behavior if the tab title were not sufficiently long.
You can call CTabFolder.setMinimumCharacters(int) if you want to change this value.
Default behavior:
With a new minimum set:
I have created a Shell and added a ScrolledComposite to it that contains a Text as its content. But I want the shell to change the size dynamically based on the content size. My implementation is as follows
shell.setLayout(new GridLayout(1,true));
ScrolledComposite sc = new ScrolledComposite(shell, SWT.V_SCROLL|SWT.H_SCROLL);
sc.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).hint(200, 200).create());
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
Composite top = new Composite(sc,SWT.NONE);
top.setLayout(GridLayoutFactory.swtDefaults().numColumns(1).create());
StyledText styledText = new StyledText(top, SWT.NONE);
styledText.setText(text);
StyleRange style = new StyleRange();
style.start = 0;
style.length = text.indexOf(":"); //$NON-NLS-1$
style.fontStyle = SWT.BOLD;
styledText.setStyleRange(style);
sc.setContent(top);
// shell.setSize(sc.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.setMinSize(top.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.pack(true);
shell.setVisible(true);
When I uncomment the commented line in above code, shell is resizing based on content, but unable to achieve scrollbar in this case.
I want to get scrollbar as well if content is beyond certain limit. If the content is within the limit, I do not want the shell to have extra blank space.
Can someone help me here ??
StyledText supports scrolling itself there is no need to using ScrolledComposite:
Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new GridLayout());
StyledText text = new StyledText(shell, SWT.V_SCROLL | SWT.H_SCROLL);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
text.setText(....);
shell.layout();
Point size = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
shell.setSize(Math.min(size.x, 100), Math.min(size.y, 100));
shell.open();
Instead of using shell.pack() just call shell.layout and then shell.computeSize to see what the unscrolled size would be. Adjust the size if it is too large and call shell.setSize.
I am developing an application and my employer demands that it should have an absolute size (1024 x 768). Is it possible to insert this absolute composite into another composite with fill layout (or any other) and set the absolute layout to be always centralized?
I am fairly new to developing screens, so I'm confused with this concept.
Thanks in advance.
You can use GridLayout to center the Composite in the Shell.
Something like:
Display display = new Display();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setText("Stack Overflow");
shell.setFullScreen(true);
shell.setLayout(new GridLayout());
// Composite, just using a border here to show where it is
Composite composite = new Composite(shell, SWT.BORDER);
// Center composite in the shell using all available space
composite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
composite.setLayout(new GridLayout());
// Something to put in the composite
Label label = new Label(composite, SWT.BEGINNING);
label.setText("Text");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
In an eclipse plugin, I try to show the user a dialog that just contains a long text. This text should be scrollable.
I tried:
protected Control createDialogArea(Composite parent)
{
Composite container = (Composite) super.createDialogArea(parent);
Text text = new Text(container, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL| SWT.MULTI);
text.setText(" " + command + "\n\r\n\r" + result);
return container;
}
The text is then shown with a disabled scrollbar (although it is larger than the size of the window). How do I enable scrolling?
The issue seems to be, that your layoutdata on the text is not limited. So SWT appears to have no idea when to enable scrolling.
Setting griddata to fill both did not work for me with your code (just tried).
However, this will:
public static void main(String[] args) {
Display display = Display.getDefault();
Shell s = new Shell(display);
s.setSize(300, 300);
s.setLayout(new GridLayout(1, true));
Composite c = new Composite(s, SWT.NONE);
c.setLayout(new GridLayout(1, false));
Text text = new Text(c, SWT.H_SCROLL | SWT.V_SCROLL | SWT.READ_ONLY);
GridData gridData = new GridData(SWT.NONE, SWT.NONE, false, false);
gridData.heightHint = 200;
gridData.widthHint = 200;
text.setLayoutData(gridData);
text.setBackground(s.getDisplay().getSystemColor(SWT.COLOR_WHITE));
text.setSize(250, 250);
Font stdFont = new Font(text.getDisplay(), new FontData("Consolas", 11, SWT.NORMAL));
text.setFont(stdFont);
StringBuffer buffer = new StringBuffer();
for (int row = 0; row < 40; row++) {
for (int column = 0; column < 20; column++) {
buffer.append("Word ");
}
buffer.append("\n");
}
text.setText(buffer.toString());
s.open();
while (!s.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
By restricting the size of your Text properly (with layoutdata, not setting the size), SWT now knows when the text is bigger than the area and enables scrolling.
Mind you, your solution does work, if you type something after creating (i know not possible for your case).
I works if you also set
text.setLayoutData(new GridData(GridData.FILL_BOTH));
I have a simple swt GUI in my Eclipse application, which looks like the following:
It is implemented very simply:
// creating the label
Label label = new Label(composite, SWT.NONE);
label.setText("Label");
// creating the input field
Text text = new Text(composite, SWT.BORDER);
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
text.setLayoutData(gridData);
I would like to add an button between the label and the input element, so that the user can get additional help on what to add inide the field.
It can either be a help button or just a icon which shows information in mouse hover.
How do I implement that? I would appreciate any help!
One of the many ways to do this is to use an information field decoration.
Something like:
Text text = new Text(composite, SWT.BORDER);
FieldDecorationRegistry decRegistry = FieldDecorationRegistry.getDefault();
FieldDecoration infoField = decRegistry.getFieldDecoration(FieldDecorationRegistry.DEC_INFORMATION);
ControlDecoration decoration = new ControlDecoration(text, SWT.TOP | SWT.LEFT);
decoration.setImage(infoField.getImage());
decoration.setDescriptionText("Info decoration text");
GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
// Space for decoration image
gridData.horizontalIndent = decRegistry.getMaximumDecorationWidth();
text.setLayoutData(gridData);