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);
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.
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 created composite control that has check box and two radio buttons.This is something like controlling radio buttons with a check box, i.e. radios will be enabled only after check box selected
I want to set the focus on the check box when the page comes up(using CTRl+TAB for composite traversal), it works fine in the case of none of radio buttons are enabled.
If any of the radio button is selected then focus is going to that radio button but not to the checked box.Is this windows default behavior, focus should be on selected radio button ??
How can i make focus should always be on check box ??
I tried to use setFocus on checkbox, but that didnt help.
All these three buttons are in a group.
Here is the sample code
Composite composite = this;
GridData LData = new GridData();
LData.horizontalSpan = 1;
LData.horizontalAlignment = GridData.FILL;
LData.grabExcessHorizontalSpace = true;
LData.verticalAlignment = GridData.FILL;
LData.grabExcessVerticalSpace = true;
composite.setLayoutData(LData);
composite.setLayout(new GridLayout(1, false));
Label infoLabel = new Label(composite, SWT.NONE);
infoLabel.setText("Test");
rGroup = new Group(composite, SWT.NONE);
GridData rGroupLData = new GridData();
rGroupLData.grabExcessHorizontalSpace = true;
rGroupLData.horizontalAlignment = GridData.FILL;
rGroup.setLayoutData(rGroupLData);
GridLayout rGroupLayout = new GridLayout(5, false);
rGroup.setLayout(rGroupLayout);
checkBox1 = new Button(rGroup, SWT.CHECK);
checkBox1.setText("CheckBox");
GridData rcheckBox1LData = new GridData();
rcheckBox1LData.horizontalSpan = 5;
rcheckBox1LData.verticalAlignment = GridData.CENTER;
rcheckBox1LData.grabExcessHorizontalSpace = true;
rcheckBox1LData.horizontalAlignment = GridData.FILL;
checkBox1.setLayoutData(rcheckBox1LData);
GridData r1LData = new GridData();
r1LData.horizontalAlignment = GridData.CENTER;
r1LData.grabExcessHorizontalSpace = true;
r1LData.verticalAlignment = GridData.CENTER;
r1Numeric = new Button(rGroup, SWT.RADIO);
r1Numeric.setText("Radio1");
r1Numeric.setSelection(true);
r1Numeric.setLayoutData(r1LData);
r1Numeric.setEnabled(false);
GridData selR1LData = new GridData();
selR1LData.horizontalAlignment = GridData.CENTER;
selR1LData.grabExcessHorizontalSpace = true;
selR1LData.verticalAlignment = GridData.CENTER;
selR1Value = new Button(rGroup, SWT.NONE);
selR1Value
.setText("Select R1");
selR1Value.setLayoutData(selR1LData);
selR1Value.setEnabled(false);
GridData r2LData = new GridData();
r2LData.horizontalAlignment = GridData.CENTER;
r2LData.grabExcessHorizontalSpace = true;
r2LData.verticalAlignment = GridData.CENTER;
r2Numeric = new Button(rGroup, SWT.RADIO);
r2Numeric.setText("Radio2");
r2Numeric.setLayoutData(r2LData);
r2Numeric.setSelection(false);
r2Numeric.setEnabled(false);
GridData selR2LData = new GridData();
selR2LData.horizontalAlignment = GridData.CENTER;
selR2LData.grabExcessHorizontalSpace = true;
selR2LData.verticalAlignment = GridData.CENTER;
selR2Value = new Button(rGroup, SWT.NONE);
selR2Value
.setText("Select R2");
selR2Value.setLayoutData(selR2LData);
selR2Value.setEnabled(false);
Given the code provided is not exactly what you have on your machine, I could not reproduce your problem, but here I could traverse through all components.
Anyway, you can set the component's tab order. Based on your example, try adding this code after creating everything:
rGroup.setTabList( new Control[] { checkBox1, r1Numeric, selR1Value, r2Numeric, selR2Value } );
Besides that, you could also implement, if it exists, the page's setFocus method, making it set focus on the checkbox.
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));