Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Vertical scroll bar for Text control
Vertical scroll bar for Text control [message #446296] Tue, 23 November 2004 10:31 Go to next message
Christian Hauser is currently offline Christian HauserFriend
Messages: 189
Registered: July 2009
Senior Member
Hello everybody

I have a composite that contains a Text control. However, when the
composite is too small to display all the text I'd like to get a
vertical scroll bar.

I tried different things, but don't seem to get any closer to a solution.

I provided a test example below. I'd like to have the scroll bars
(actually a vertical scroll bar would be sufficient) next to the
composite and it should scroll through the text. Or can I have a text
control with scroll bars?

I put the composite into a shell, but in my application it will be
within another composite.

I'd be very happy if someone could enlighten me on how to scroll through
a text composite (that might even be disabled or at least read only) if
not all of the text can be read.

Thanks in advance for any help,
Christian


import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ScrollTextExample {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout(1, true);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
shell.setLayout(new GridLayout(1, true));

final Composite composite = new Composite(shell, SWT.V_SCROLL |
SWT.H_SCROLL);
gridLayout = new GridLayout(1, true);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
composite.setLayout(gridLayout);
GridData gridData = new GridData(GridData.FILL, GridData.FILL,
true, true);
composite.setLayoutData(gridData);

final Text text = new Text(composite, SWT.MULTI | SWT.BORDER |
SWT.WRAP | SWT.READ_ONLY);
text.setText("The quick brown fox jumps over the lazy dog and I
struggle with SWT.");
gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
text.setLayoutData(gridData);

final ScrollBar hBar = composite.getHorizontalBar();
hBar.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
Point location = text.getLocation();
location.x = -hBar.getSelection();
text.setLocation(location);
}
});
final ScrollBar vBar = composite.getVerticalBar();
vBar.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
Point location = text.getLocation();
location.y = -vBar.getSelection();
text.setLocation(location);
}
});
composite.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event e) {
Point size = text.getSize();
Rectangle rect = composite.getClientArea();
hBar.setMaximum(size.x);
vBar.setMaximum(size.y);
hBar.setThumb(Math.min(size.x, rect.width));
vBar.setThumb(Math.min(size.y, rect.height));
int hPage = size.x - rect.width;
int vPage = size.y - rect.height;
int hSelection = hBar.getSelection();
int vSelection = vBar.getSelection();
Point location = text.getLocation();
if (hSelection >= hPage) {
if (hPage <= 0)
hSelection = 0;
location.x = -hSelection;
}
if (vSelection >= vPage) {
if (vPage <= 0)
vSelection = 0;
location.y = -vSelection;
}
text.setLocation(location);
}
});

shell.setBounds(100, 100, 100, 80);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Re: Vertical scroll bar for Text control [message #446305 is a reply to message #446296] Tue, 23 November 2004 13:58 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
To get a vertical scrollbar on the Text widget, just create the Text widget
with the SWT.V_SCROLL style. The widget manages the proper scrolling
behaviour for you.

public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout(1, true);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
shell.setLayout(gridLayout);

final Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP
| SWT.READ_ONLY | SWT.V_SCROLL);
text.setText("The quick brown fox jumps over the lazy dog and I
struggle with SWT.");
GridData gridData = new GridData(GridData.FILL, GridData.FILL, true,
true);
text.setLayoutData(gridData);

shell.setBounds(100, 100, 100, 80);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

"Christian Hauser" <christian.hauser@dvbern.ch> wrote in message
news:cnv3ht$84s$1@www.eclipse.org...
> Hello everybody
>
> I have a composite that contains a Text control. However, when the
> composite is too small to display all the text I'd like to get a vertical
> scroll bar.
>
> I tried different things, but don't seem to get any closer to a solution.
>
> I provided a test example below. I'd like to have the scroll bars
> (actually a vertical scroll bar would be sufficient) next to the composite
> and it should scroll through the text. Or can I have a text control with
> scroll bars?
>
> I put the composite into a shell, but in my application it will be within
> another composite.
>
> I'd be very happy if someone could enlighten me on how to scroll through a
> text composite (that might even be disabled or at least read only) if not
> all of the text can be read.
>
> Thanks in advance for any help,
> Christian
>
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.graphics.Point;
> import org.eclipse.swt.graphics.Rectangle;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Event;
> import org.eclipse.swt.widgets.Listener;
> import org.eclipse.swt.widgets.ScrollBar;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Text;
>
> public class ScrollTextExample {
> public static void main(String[] args) {
> Display display = new Display();
> final Shell shell = new Shell(display);
> GridLayout gridLayout = new GridLayout(1, true);
> gridLayout.marginWidth = 0;
> gridLayout.marginHeight = 0;
> shell.setLayout(new GridLayout(1, true));
>
> final Composite composite = new Composite(shell, SWT.V_SCROLL |
> SWT.H_SCROLL);
> gridLayout = new GridLayout(1, true);
> gridLayout.marginWidth = 0;
> gridLayout.marginHeight = 0;
> composite.setLayout(gridLayout);
> GridData gridData = new GridData(GridData.FILL, GridData.FILL, true,
> true);
> composite.setLayoutData(gridData);
>
> final Text text = new Text(composite, SWT.MULTI | SWT.BORDER |
> SWT.WRAP | SWT.READ_ONLY);
> text.setText("The quick brown fox jumps over the lazy dog and I
> struggle with SWT.");
> gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
> text.setLayoutData(gridData);
>
> final ScrollBar hBar = composite.getHorizontalBar();
> hBar.addListener(SWT.Selection, new Listener() {
> public void handleEvent(Event e) {
> Point location = text.getLocation();
> location.x = -hBar.getSelection();
> text.setLocation(location);
> }
> });
> final ScrollBar vBar = composite.getVerticalBar();
> vBar.addListener(SWT.Selection, new Listener() {
> public void handleEvent(Event e) {
> Point location = text.getLocation();
> location.y = -vBar.getSelection();
> text.setLocation(location);
> }
> });
> composite.addListener(SWT.Resize, new Listener() {
> public void handleEvent(Event e) {
> Point size = text.getSize();
> Rectangle rect = composite.getClientArea();
> hBar.setMaximum(size.x);
> vBar.setMaximum(size.y);
> hBar.setThumb(Math.min(size.x, rect.width));
> vBar.setThumb(Math.min(size.y, rect.height));
> int hPage = size.x - rect.width;
> int vPage = size.y - rect.height;
> int hSelection = hBar.getSelection();
> int vSelection = vBar.getSelection();
> Point location = text.getLocation();
> if (hSelection >= hPage) {
> if (hPage <= 0)
> hSelection = 0;
> location.x = -hSelection;
> }
> if (vSelection >= vPage) {
> if (vPage <= 0)
> vSelection = 0;
> location.y = -vSelection;
> }
> text.setLocation(location);
> }
> });
>
> shell.setBounds(100, 100, 100, 80);
> shell.open();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
> }
Re: Vertical scroll bar for Text control [message #446363 is a reply to message #446305] Tue, 23 November 2004 20:13 Go to previous messageGo to next message
Christian Hauser is currently offline Christian HauserFriend
Messages: 189
Registered: July 2009
Senior Member
> final Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP
> | SWT.READ_ONLY | SWT.V_SCROLL);

Thanks Veronika for your help. I didn't know that this works as JavaDoc
of the Text control only mentions the following styles:
CENTER, LEFT, MULTI, PASSWORD, SINGLE, RIGHT, READ_ONLY, WRAP

Thanks a lot.
Christian
Re: Vertical scroll bar for Text control [message #446373 is a reply to message #446363] Tue, 23 November 2004 22:55 Go to previous message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
The styles and events defined in the javadoc for all super classes in the
class hierarchy also apply. Therefore all subclasses of Scrollable support
V_SCROLL and H_SCROLL. Text subclasses Scrollable.

"Christian Hauser" <c.hauser@active.ch> wrote in message
news:41A399FD.2040906@active.ch...
>> final Text text = new Text(shell, SWT.MULTI | SWT.BORDER |
>> SWT.WRAP
>> | SWT.READ_ONLY | SWT.V_SCROLL);
>
> Thanks Veronika for your help. I didn't know that this works as JavaDoc of
> the Text control only mentions the following styles:
> CENTER, LEFT, MULTI, PASSWORD, SINGLE, RIGHT, READ_ONLY, WRAP
>
> Thanks a lot.
> Christian
Previous Topic:Table doesn't stretch to fit window
Next Topic:Toolbars
Goto Forum:
  


Current Time: Thu Apr 25 10:47:39 GMT 2024

Powered by FUDForum. Page generated in 0.03248 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top