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));
Related
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 had an Eclipse plug-in app (ten-year-old code, no documentation, etc.) dropped in my lap and while adding new features to it, I noticed that when a panel is resized, the text boxes change size continuously while the separator is being dragged.
As you can see in the second picture, the text boxes are kind of randomly sized. Is there a setting in SWT that will prevent this from happening?
Here's how I'm creating one of the text boxes. The others are basically clones of this:
Font font = parent.getFont();
setLayout(new FillLayout());
SashForm sashForm = new SashForm(this, SWT.VERTICAL);
FormToolkit toolkit = new FormToolkit(getParent().getDisplay());
Section section = toolkit.createSection(sashForm,
Section.DESCRIPTION | ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED);
section.setLayoutData(new GridData(GridData.FILL_BOTH));
section.setLayout(new GridLayout());
section.setText("Section Title");
Composite controlComposite = toolkit.createComposite(section);
GridLayout controlLayout = new GridLayout();
controlLayout.numColumns = 2;
controlLayout.verticalSpacing = 20;
controlComposite.setLayout(controlLayout);
section.setClient(controlComposite);
Font bold = ResourceManager.getBoldFont(font);
Label textLabel = toolkit.createLabel(controlComposite, "Title:", SWT.BOLD);
GridData gd = new GridData();
gd.horizontalSpan = 1;
textLabel.setLayoutData(gd);
textLabel.setFont(bold);
textBox = new ExtendedText(controlComposite, SWT.BORDER | SWT.SINGLE, false);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 1;
gd.verticalSpan = 2;
textBox.setLayoutData(gd);
The ExtendedText class is an extension of StyledText. The important bits of it are this:
GridData gd_bg = new GridData(GridData.FILL_BOTH);
setLayoutData(gd_bg);
final GridLayout gridLayout = new GridLayout();
gridLayout.verticalSpacing = 0;
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.horizontalSpacing = 0;
gridLayout.numColumns = 1;
gridLayout.makeColumnsEqualWidth = true;
sashForm.setWeights(new int[] { 1, 1 });
Okay, after digging in a little deeper, I got it working as expected.
First, the controlComposite and controlLayout objects are now created using
Composite controlComposite = new Composite(section, SWT.NONE)
controlComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
controlComposite.setBackground(section.getBackground());
GridLayout controlLayout = new GridLayout(2, true);
controlLayout.marginHeight = 20;
controlLayout.marginWidth = 0;
controlLayout.verticalSpacing = 10;
controlLayout.horizontalSpacing = 0;
controlComposite.setLayout(controlLayout);
section.setClient(controlComposite);
Once I did that, things started to stabilize. I also ended up tweaking the weights to this:
sashForm.setWeights(new int[] { 2, 3 });
It's not perfect, but it'll do for now.
Thanks to #greg-449 and #Baz for taking a look
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:
I am creating an SWT application with two TreeViewers and a TableViewer in between them.
The TableViewer contains images in each row that indicate something about the date in that row of the TreeViewer.
However the problem is that the images in the TableViewer are not properly aligning with the rows in the TreeViewer. Is there any way for me to ensure that their rows stay exactly leveld?
Thanks
ADDED
You can change the way the height of a row is measured by overriding the behaviour of the "measuring method", i.e. adding a Listener to SWT.MeasureItem. There is a good example here. Just use the height of the icon you use in the TreeViewer plus maybe a couple of border pixels.
Here is the important code part:
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(10,10,200,250);
final Table table = new Table(shell, SWT.NONE);
table.setBounds(10,10,150,200);
table.setLinesVisible(true);
for (int i = 0; i < 5; i++) {
new TableItem(table, SWT.NONE).setText("item " + i);
}
table.addListener(SWT.MeasureItem, new Listener() {
public void handleEvent(Event event) {
int clientWidth = table.getClientArea().width;
event.height = event.gc.getFontMetrics().getHeight() * 2;
event.width = clientWidth * 2;
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();