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 |
Christian Hauser 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 |
Veronika Irvine 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();
> }
> }
|
|
| | |
Goto Forum:
Current Time: Fri Apr 26 15:24:40 GMT 2024
Powered by FUDForum. Page generated in 0.03423 seconds
|