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();
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 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);
I am using RowLayout for thumbnails of images. All the thumbnails are being displayed in one row only. How can I make them to display efficiently in multiple rows upon resizing?
You can easily find it in the documentation of RowLayout.
What you're looking for is RowLayout#wrap:
wrap specifies whether a control will be wrapped to the next row if there is insufficient space on the current row. The default value is true.
Since the default is true, it should already wrap...
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell();
shell.setText("StackOverflow");
RowLayout layout = new RowLayout();
layout.wrap = true;
shell.setLayout(layout);
Image image = new Image(display, "star.png");
for(int i = 0; i < 10; i++)
new Label(shell, SWT.NONE).setImage(image);
shell.pack();
shell.setSize(200, 100);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
image.dispose();
}
Looks like this before resizing:
And after resizing: