How is it possible to position text in an SWT Shell?
My problem is that I don't know which Layout to use. I've tried Row and Fill-Layout, but this won't work if I do it like text.setLocation(x,y).
Can anyone help me?
Solved it.
If you use no Layout, put the elements you want in a SWT Group, you can position it free inside this group.
Group group = new Group(parent, SWT.NONE);
Text text = new Text(group, SWT.BORDER);
group.setSize(parent.getSize());
text.setLocation(parent.getSize().x/2, parent.getSize().y/2);
Will look like this:
Might be worth taking a look at FormLayout. Would go something like:
Group group = new Group(parent, SWT.NONE);
group.setLayout(new FormLayout());
Text text = new Text(group, SWT.BORDER);
text.setText("Text1");
FormData fd = new FormData();
fd.top = new FormAttachment(50,0);
fd.left = new FormAttachment(50,0);
text.setLayoutData(fd);
The FormAttachment class lets you specify both a percentage of the container width (50% in this case), and also a fixed offset(0).
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 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);
Am developing an eclipse plugin which has few wizard pages. I need the wizard window size to be constant, with "BOTH MAXIMISE and MINIMISE disabled", "window RESIZE disabled".
The point is I am not using SHELL. I am using COMPOSITE instead, which doesn't have any style bits.
How can I do that? I am just providing a part of my entire code:
public void createControl(Composite parent)
{
// TODO Auto-generated method stub
composite = new Composite(parent, SWT.NONE );
composite.setLayout(new GridLayout());
Composite selectAdapterComposite = new Composite(composite, SWT.NONE);
FormLayout reportOptionsCompositeLayout = new FormLayout();
reportOptionsCompositeLayout.marginHeight = 1;
reportOptionsCompositeLayout.marginWidth = 1;
selectAdapterComposite.setLayout(reportOptionsCompositeLayout);
buttonInterfaceSelection = new Button(selectAdapterComposite,SWT.RADIO);
//SWT.CHECK);
buttonInterfaceSelection.setText("Generate adapter using interface !");
buttonInterfaceSelection.setSelection(true);
buttonInterfaceSelection.addListener(SWT.Selection, this);
FormData exportInToExcelButtonData = new FormData();
exportInToExcelButtonData.left = new FormAttachment(null, 5);
buttonInterfaceSelection.setLayoutData(exportInToExcelButtonData);
// One Text Box
Label searchBoxLabel = new Label(selectAdapterComposite, SWT.None);
searchBoxLabel.setText("Search to select [Type to get the results below]");
FormData destinationLabelData = new FormData();
destinationLabelData.top = new FormAttachment(buttonInterfaceSelection, 10);
destinationLabelData.left = new FormAttachment(null, 5);
searchBoxLabel.setLayoutData(destinationLabelData);
searchTextBox = new Text(selectAdapterComposite, SWT.BORDER);
searchTextBox.setSize(20, 2);
FormData searchTextBoxData = new FormData();
searchTextBoxData.top = new FormAttachment(searchBoxLabel, 8);
searchTextBoxData.left = new FormAttachment(null, 5);
// destinationFolderPathData.left = new
// FormAttachment(destinationLabel,15);
searchTextBoxData.width = 400;
searchTextBox.addListener(SWT.Modify, this);
searchTextBox.setEnabled(true);
searchTextBox.setLayoutData(searchTextBoxData);
.
.
.
.
.
setControl(composite);
}
Please help me out.
Your code snippet is irrelevant to your question. The key word is wizard. When you create that wizard, it requires a Shell, so you can set its style bits there.
A WizardDialog's constructor:
public WizardDialog(Shell parentShell, IWizard newWizard)
Example of shell style bits:
parentShell.setShellStyle(parentShell.getShellStyle() | (~SWT.RESIZE));
Thanks for your reply... am a newbie to swt and your answer gave me an important info which I dint know before. Now then, I just took some time to go through widgets documentation and found something.
Composite : Instances of this class are controls which are capable of containing other controls.
Shell : Instances of this class represent the "windows" which the desktop or "window manager" is managing.
I realised that my understanding of SHELL and COMPOSITE was wrong.
Conclusion: So I have to depend upon SHELL to give window resizing controls and using a COMPOSITE does not give me any resizing option...
Correct me if am wrong please.. hope this will be useful to other noobs too...
Thanks.
P.S.: now i understoood, my code segment is irrelevant to my question cos, I am working on someone else's code and trying to make some changes to it. instead of making changes in SHELL (which is created in some other class) i am doing it in COMPOSITE.
I am new to Java and would like to know how to set the font and font color to be used for the next text to be added to a SWT StyledText box.
So for example I have an application that defines "command" and "data" text and each is to be displayed in a different font/color. So let's say I've just added some "command" text. Now how do I set things up so that the next text which will be "data" text is displayed in a different font and color?
I've done a lot of googling, but nothing seems to be helping me.
P.S.: This can't be the most efficient way to do it:
int a = st.getCharCount();
Font font = new Font(shlProtruleModifier.getDisplay(), "Courier", 10, SWT.NORMAL);
StyleRange[] sr = new StyleRange[1];
sr[0] = new StyleRange();
st.append("\r\nWhat the heck?");
sr[0].start = a;
sr[0].length = st.getCharCount() - a;
sr[0].font = font;
sr[0].foreground = SWTResourceManager.getColor(SWT.COLOR_BLACK);
st.replaceStyleRanges(sr[0].start, sr[0].length, sr);
So all I've been able to come up with the following technique that does work,
int a = st.getCharCount();
Font font = new Font(shlProtruleModifier.getDisplay(), "Courier", 10, SWT.NORMAL);
StyleRange[] sr = new StyleRange[1];
sr[0] = new StyleRange();
st.append("\r\nWhat the heck?");
sr[0].start = a;
sr[0].length = st.getCharCount() - a;
sr[0].font = font;
sr[0].foreground = SWTResourceManager.getColor(SWT.COLOR_BLACK);
st.replaceStyleRanges(sr[0].start, sr[0].length, sr);