Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Vertical Scrolling on an Unknown Height
Vertical Scrolling on an Unknown Height [message #440128] Fri, 23 July 2004 18:30 Go to next message
Eclipse UserFriend
Originally posted by: cowking.telus.NOSPAM.net

Hi,

I've searched for a decent solution on this and have attempted many
things. I've found several code snipets describing how to use scroll bars,
but none of them seem to account for everything I need to do. Perhaps
someone here will be able to assist me.

I am attempting to create a stand-alone SWT program. In it, I dynamically
generate screens (such as reports taken from data within a database).
Therefore, I never know how long the screen will be. It's quite possible
that even maximized the screen will need to carry on further. Naturally,
the solution to this problem is a vertical scrollbar. But such a simple
solution has proven quite difficult for me to implement.

Here is a snipet of the code I am using now:

public static void main(String[] args) {
//Create a display and a shell
Display display = new Display();
final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.V_SCROLL);

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

//TODO Uncomment when you've got the scroll bar working
// shell.setMaximized(true);

//TODO Create a database connection and get the current months data

//Create the Report Screen/Main Screen
createReportScreen(composite);

//Start the program
shell.open();

//Wait until the program finishes before disposing the display
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();

}


What happens, is that I get a composite on screen that has a scrollbar.
However, when I attempt to scroll down to data off the current page, it
shows me nothing. Just the background shell. Also, if I resize the shell,
the composite resizes properly... But again, if any data is off the screen
(or takes up more space than the composite has) it cannot be scrolled down.

I know I'm missing something small. So if anyone is kind enough to use
their time for this, I'd appreciate it very much.

Thanks!
Kevin

PS - Using FormLayout to place my widgets, if that makes any difference.
Re: Vertical Scrolling on an Unknown Height [message #440131 is a reply to message #440128] Fri, 23 July 2004 20:17 Go to previous messageGo to next message
Greg Roberts is currently offline Greg RobertsFriend
Messages: 88
Registered: July 2009
Member
Instead of 'adding' scrollbars to a Composite I use a ScrolledComposite.

ScrolledComposite scComp =
new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);

Composite comp = new Composite(scComp, SWT.NONE);

scComp.setContent(comp);

Then you can add widgets to comp and set the layout on comp, etc. Using
ScrolledComposite takes care of a lot of the work for it.
The scroll bars are only present when needed - vertical and horizontal in
the example above.

I hope this helps.

Greg



Kevbo wrote:

> Hi,

> I've searched for a decent solution on this and have attempted many
> things. I've found several code snipets describing how to use scroll bars,
> but none of them seem to account for everything I need to do. Perhaps
> someone here will be able to assist me.

> I am attempting to create a stand-alone SWT program. In it, I dynamically
> generate screens (such as reports taken from data within a database).
> Therefore, I never know how long the screen will be. It's quite possible
> that even maximized the screen will need to carry on further. Naturally,
> the solution to this problem is a vertical scrollbar. But such a simple
> solution has proven quite difficult for me to implement.

> Here is a snipet of the code I am using now:

> public static void main(String[] args) {
> //Create a display and a shell
> Display display = new Display();
> final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.V_SCROLL);

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

> //TODO Uncomment when you've got the scroll bar working
> // shell.setMaximized(true);

> //TODO Create a database connection and get the current months data

> //Create the Report Screen/Main Screen
> createReportScreen(composite);

> //Start the program
> shell.open();

> //Wait until the program finishes before disposing the display
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) display.sleep();
> }
> display.dispose();

> }


> What happens, is that I get a composite on screen that has a scrollbar.
> However, when I attempt to scroll down to data off the current page, it
> shows me nothing. Just the background shell. Also, if I resize the shell,
> the composite resizes properly... But again, if any data is off the screen
> (or takes up more space than the composite has) it cannot be scrolled down.

> I know I'm missing something small. So if anyone is kind enough to use
> their time for this, I'd appreciate it very much.

> Thanks!
> Kevin

> PS - Using FormLayout to place my widgets, if that makes any difference.
Re: Vertical Scrolling on an Unknown Height [message #440218 is a reply to message #440131] Mon, 26 July 2004 17:21 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Kevin.clark.accessbc.com.nospam

Thanks for your comments Greg. I have attempted to use scrolled composite,
but get the same problems. When I run my program I just get the blank
screen I was getting before... All that is displayed is an empty shell
with not scroll bars and no widgets. No scrolled composite or composite
seems to get drawn. It's got to be something small..

Here's the revised code with your suggestions:

public static void main(String[] args) {
//Create a display and a shell
Display display = new Display();
final Shell shell = new Shell(display);

ScrolledComposite sComp = new ScrolledComposite(shell, SWT.V_SCROLL);

Composite composite = new Composite(sComp, SWT.NONE);
sComp.setContent(composite);

//Create a new layout for this screen using FormLayout
FormLayout layout = new FormLayout();
layout.marginWidth = 5; //Amount of space from sides
layout.marginHeight = 5; //Amount of space from top and bottom
composite.setLayout(layout);

//I CREATE AND DRAW MY WIDGETS IN HERE USING composite AS THEIR PARENT

//Start the program
shell.open();

//Wait until the program finishes before disposing the display
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}


Thanks for your help,
Kevin




Greg wrote:

> Instead of 'adding' scrollbars to a Composite I use a ScrolledComposite.

> ScrolledComposite scComp =
> new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);

> Composite comp = new Composite(scComp, SWT.NONE);

> scComp.setContent(comp);

> Then you can add widgets to comp and set the layout on comp, etc. Using
> ScrolledComposite takes care of a lot of the work for it.
> The scroll bars are only present when needed - vertical and horizontal in
> the example above.

> I hope this helps.

> Greg



> Kevbo wrote:

> > Hi,

> > I've searched for a decent solution on this and have attempted many
> > things. I've found several code snipets describing how to use scroll bars,
> > but none of them seem to account for everything I need to do. Perhaps
> > someone here will be able to assist me.

> > I am attempting to create a stand-alone SWT program. In it, I dynamically
> > generate screens (such as reports taken from data within a database).
> > Therefore, I never know how long the screen will be. It's quite possible
> > that even maximized the screen will need to carry on further. Naturally,
> > the solution to this problem is a vertical scrollbar. But such a simple
> > solution has proven quite difficult for me to implement.

> > Here is a snipet of the code I am using now:

> > public static void main(String[] args) {
> > //Create a display and a shell
> > Display display = new Display();
> > final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.V_SCROLL);

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

> > //TODO Uncomment when you've got the scroll bar working
> > // shell.setMaximized(true);

> > //TODO Create a database connection and get the current months data

> > //Create the Report Screen/Main Screen
> > createReportScreen(composite);

> > //Start the program
> > shell.open();

> > //Wait until the program finishes before disposing the display
> > while (!shell.isDisposed()) {
> > if (!display.readAndDispatch()) display.sleep();
> > }
> > display.dispose();

> > }


> > What happens, is that I get a composite on screen that has a scrollbar.
> > However, when I attempt to scroll down to data off the current page, it
> > shows me nothing. Just the background shell. Also, if I resize the shell,
> > the composite resizes properly... But again, if any data is off the screen
> > (or takes up more space than the composite has) it cannot be scrolled down.

> > I know I'm missing something small. So if anyone is kind enough to use
> > their time for this, I'd appreciate it very much.

> > Thanks!
> > Kevin

> > PS - Using FormLayout to place my widgets, if that makes any difference.
Re: Vertical Scrolling on an Unknown Height [message #440277 is a reply to message #440218] Tue, 27 July 2004 20:53 Go to previous messageGo to next message
Greg Roberts is currently offline Greg RobertsFriend
Messages: 88
Registered: July 2009
Member
It looks correct.
I just wonder if the problem has to do with using the new Shell you get to
pass to the ScrolledComposite constructor...
Not sure if this Shell is what you need - seems like it should work, but I
haven't tried this method.
I have used this mechanism inside of a createPartControl method for a view
and I used the Composite parent that got passed in so I know that it is
the Composite that is the Composite for this particular view.

Sorry I don't have any more information about this...


Kevbo wrote:

> Thanks for your comments Greg. I have attempted to use scrolled composite,
> but get the same problems. When I run my program I just get the blank
> screen I was getting before... All that is displayed is an empty shell
> with not scroll bars and no widgets. No scrolled composite or composite
> seems to get drawn. It's got to be something small..

> Here's the revised code with your suggestions:

> public static void main(String[] args) {
> //Create a display and a shell
> Display display = new Display();
> final Shell shell = new Shell(display);

> ScrolledComposite sComp = new ScrolledComposite(shell, SWT.V_SCROLL);

> Composite composite = new Composite(sComp, SWT.NONE);
> sComp.setContent(composite);

> //Create a new layout for this screen using FormLayout
> FormLayout layout = new FormLayout();
> layout.marginWidth = 5; //Amount of space from sides
> layout.marginHeight = 5; //Amount of space from top and bottom
> composite.setLayout(layout);

> //I CREATE AND DRAW MY WIDGETS IN HERE USING composite AS THEIR PARENT

> //Start the program
> shell.open();

> //Wait until the program finishes before disposing the display
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) display.sleep();
> }
> display.dispose();
> }


> Thanks for your help,
> Kevin




> Greg wrote:

> > Instead of 'adding' scrollbars to a Composite I use a ScrolledComposite.

> > ScrolledComposite scComp =
> > new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);

> > Composite comp = new Composite(scComp, SWT.NONE);

> > scComp.setContent(comp);

> > Then you can add widgets to comp and set the layout on comp, etc. Using
> > ScrolledComposite takes care of a lot of the work for it.
> > The scroll bars are only present when needed - vertical and horizontal in
> > the example above.

> > I hope this helps.

> > Greg



> > Kevbo wrote:

> > > Hi,

> > > I've searched for a decent solution on this and have attempted many
> > > things. I've found several code snipets describing how to use scroll
bars,
> > > but none of them seem to account for everything I need to do. Perhaps
> > > someone here will be able to assist me.

> > > I am attempting to create a stand-alone SWT program. In it, I dynamically
> > > generate screens (such as reports taken from data within a database).
> > > Therefore, I never know how long the screen will be. It's quite possible
> > > that even maximized the screen will need to carry on further. Naturally,
> > > the solution to this problem is a vertical scrollbar. But such a simple
> > > solution has proven quite difficult for me to implement.

> > > Here is a snipet of the code I am using now:

> > > public static void main(String[] args) {
> > > //Create a display and a shell
> > > Display display = new Display();
> > > final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.V_SCROLL);

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

> > > //TODO Uncomment when you've got the scroll bar working
> > > // shell.setMaximized(true);

> > > //TODO Create a database connection and get the current months data

> > > //Create the Report Screen/Main Screen
> > > createReportScreen(composite);

> > > //Start the program
> > > shell.open();

> > > //Wait until the program finishes before disposing the display
> > > while (!shell.isDisposed()) {
> > > if (!display.readAndDispatch()) display.sleep();
> > > }
> > > display.dispose();

> > > }


> > > What happens, is that I get a composite on screen that has a scrollbar.
> > > However, when I attempt to scroll down to data off the current page, it
> > > shows me nothing. Just the background shell. Also, if I resize the shell,
> > > the composite resizes properly... But again, if any data is off the
screen
> > > (or takes up more space than the composite has) it cannot be scrolled
down.

> > > I know I'm missing something small. So if anyone is kind enough to use
> > > their time for this, I'd appreciate it very much.

> > > Thanks!
> > > Kevin

> > > PS - Using FormLayout to place my widgets, if that makes any difference.
Re: Vertical Scrolling on an Unknown Height [message #440290 is a reply to message #440277] Wed, 28 July 2004 16:10 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Kevin.clark.accessbc.com.nospam

Greg,

Thank you for your time and efforts. I suppose I'll just keep hacking at
it and continue searching for good code snippets.

You mentioned the Shell... Since this is a stand-alone program, I don't
get a createPartControl method to implement. The creation of a Display and
Shell is just the way you get the ball rolling for a stand-alone program.
The Shell has the same characteristics of a Composite object (Shell
extends Decorations extends Canvas extends Composite). Therefore, it
should be capable of being used as a Composite.

However with that said, I am certainly not ruling out the chance that the
Shell creation could be causing some of these problems.

Again, thanks for helping. I've learned a lot despite the fact that it
still doesn't work!

Kevin



Greg wrote:

> It looks correct.
> I just wonder if the problem has to do with using the new Shell you get to
> pass to the ScrolledComposite constructor...
> Not sure if this Shell is what you need - seems like it should work, but I
> haven't tried this method.
> I have used this mechanism inside of a createPartControl method for a view
> and I used the Composite parent that got passed in so I know that it is
> the Composite that is the Composite for this particular view.

> Sorry I don't have any more information about this...


> Kevbo wrote:

> > Thanks for your comments Greg. I have attempted to use scrolled composite,
> > but get the same problems. When I run my program I just get the blank
> > screen I was getting before... All that is displayed is an empty shell
> > with not scroll bars and no widgets. No scrolled composite or composite
> > seems to get drawn. It's got to be something small..

> > Here's the revised code with your suggestions:

> > public static void main(String[] args) {
> > //Create a display and a shell
> > Display display = new Display();
> > final Shell shell = new Shell(display);

> > ScrolledComposite sComp = new ScrolledComposite(shell, SWT.V_SCROLL);

> > Composite composite = new Composite(sComp, SWT.NONE);
> > sComp.setContent(composite);

> > //Create a new layout for this screen using FormLayout
> > FormLayout layout = new FormLayout();
> > layout.marginWidth = 5; //Amount of space from sides
> > layout.marginHeight = 5; //Amount of space from top and bottom
> > composite.setLayout(layout);

> > //I CREATE AND DRAW MY WIDGETS IN HERE USING composite AS THEIR PARENT

> > //Start the program
> > shell.open();

> > //Wait until the program finishes before disposing the display
> > while (!shell.isDisposed()) {
> > if (!display.readAndDispatch()) display.sleep();
> > }
> > display.dispose();
> > }


> > Thanks for your help,
> > Kevin




> > Greg wrote:

> > > Instead of 'adding' scrollbars to a Composite I use a ScrolledComposite.

> > > ScrolledComposite scComp =
> > > new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);

> > > Composite comp = new Composite(scComp, SWT.NONE);

> > > scComp.setContent(comp);

> > > Then you can add widgets to comp and set the layout on comp, etc. Using
> > > ScrolledComposite takes care of a lot of the work for it.
> > > The scroll bars are only present when needed - vertical and horizontal in
> > > the example above.

> > > I hope this helps.

> > > Greg



> > > Kevbo wrote:

> > > > Hi,

> > > > I've searched for a decent solution on this and have attempted many
> > > > things. I've found several code snipets describing how to use scroll
> bars,
> > > > but none of them seem to account for everything I need to do. Perhaps
> > > > someone here will be able to assist me.

> > > > I am attempting to create a stand-alone SWT program. In it, I
dynamically
> > > > generate screens (such as reports taken from data within a database).
> > > > Therefore, I never know how long the screen will be. It's quite
possible
> > > > that even maximized the screen will need to carry on further.
Naturally,
> > > > the solution to this problem is a vertical scrollbar. But such a simple
> > > > solution has proven quite difficult for me to implement.

> > > > Here is a snipet of the code I am using now:

> > > > public static void main(String[] args) {
> > > > //Create a display and a shell
> > > > Display display = new Display();
> > > > final Shell shell = new Shell(display, SWT.SHELL_TRIM |
SWT.V_SCROLL);

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

> > > > //TODO Uncomment when you've got the scroll bar working
> > > > // shell.setMaximized(true);

> > > > //TODO Create a database connection and get the current months data

> > > > //Create the Report Screen/Main Screen
> > > > createReportScreen(composite);

> > > > //Start the program
> > > > shell.open();

> > > > //Wait until the program finishes before disposing the display
> > > > while (!shell.isDisposed()) {
> > > > if (!display.readAndDispatch()) display.sleep();
> > > > }
> > > > display.dispose();

> > > > }


> > > > What happens, is that I get a composite on screen that has a scrollbar.
> > > > However, when I attempt to scroll down to data off the current page, it
> > > > shows me nothing. Just the background shell. Also, if I resize the
shell,
> > > > the composite resizes properly... But again, if any data is off the
> screen
> > > > (or takes up more space than the composite has) it cannot be scrolled
> down.

> > > > I know I'm missing something small. So if anyone is kind enough to use
> > > > their time for this, I'd appreciate it very much.

> > > > Thanks!
> > > > Kevin

> > > > PS - Using FormLayout to place my widgets, if that makes any
difference.
Re: Vertical Scrolling on an Unknown Height [message #440823 is a reply to message #440128] Fri, 06 August 2004 23:02 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Kevin.clark.accessbc.com.nospam

I have resolved this problem. I'm posting the solution here for archival
purposes, since I'm pretty sure the topic has died.

It's best to use ScrolledComposite, instead of what I was originally
trying to do (access the scroll bars on the shell). You need to set the
size of the ScrolledComposite to the size of the client area on the Shell.
But set the composite that you draw onto to it's default size. Then add a
resize listener to the ScrolledComposite so that it changes sizes with the
Shell. See the code snippet below for a better understanding.


Kevin



public static void main(String[] args) {
//Create a display and a shell
Display display = new Display();
final Shell shell = new Shell(display);

//Create scroll bars
final ScrolledComposite sComp = new ScrolledComposite(shell,
SWT.V_SCROLL);

//Create the composite to draw widgets onto and put the scroll bars on
//it
final Composite composite = new Composite(sComp, SWT.NONE);
sComp.setContent(composite);

//Create a new layout for this screen using FormLayout
FormLayout layout = new FormLayout();
layout.marginWidth = 5; //Amount of space from sides
layout.marginHeight = 5; //Amount of space from top and bottom
composite.setLayout(layout);

//**************DRAW WIDGETS****************

//Set the size and configure the scrollbars
Rectangle rect = shell.getClientArea();
Point size = composite.computeSize(rect.width, SWT.DEFAULT);
composite.setSize(size);
sComp.setSize(rect.width, rect.height);

shell.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event e) {
Rectangle rect = shell.getClientArea();
Point size = composite.computeSize(rect.width, SWT.DEFAULT);
composite.setSize(size);
sComp.setSize(rect.width, rect.height);
}
});

//Start the program
shell.open();

//Wait until the program finishes before disposing the display
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();

}







Kevbo wrote:

> Hi,

> I've searched for a decent solution on this and have attempted many
> things. I've found several code snipets describing how to use scroll bars,
> but none of them seem to account for everything I need to do. Perhaps
> someone here will be able to assist me.

> I am attempting to create a stand-alone SWT program. In it, I dynamically
> generate screens (such as reports taken from data within a database).
> Therefore, I never know how long the screen will be. It's quite possible
> that even maximized the screen will need to carry on further. Naturally,
> the solution to this problem is a vertical scrollbar. But such a simple
> solution has proven quite difficult for me to implement.

> Here is a snipet of the code I am using now:

> public static void main(String[] args) {
> //Create a display and a shell
> Display display = new Display();
> final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.V_SCROLL);

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

> //TODO Uncomment when you've got the scroll bar working
> // shell.setMaximized(true);

> //TODO Create a database connection and get the current months data

> //Create the Report Screen/Main Screen
> createReportScreen(composite);

> //Start the program
> shell.open();

> //Wait until the program finishes before disposing the display
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) display.sleep();
> }
> display.dispose();

> }


> What happens, is that I get a composite on screen that has a scrollbar.
> However, when I attempt to scroll down to data off the current page, it
> shows me nothing. Just the background shell. Also, if I resize the shell,
> the composite resizes properly... But again, if any data is off the screen
> (or takes up more space than the composite has) it cannot be scrolled down.

> I know I'm missing something small. So if anyone is kind enough to use
> their time for this, I'd appreciate it very much.

> Thanks!
> Kevin

> PS - Using FormLayout to place my widgets, if that makes any difference.
Re: Vertical Scrolling on an Unknown Height [message #440969 is a reply to message #440823] Mon, 09 August 2004 15:10 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Thanks for posting your solution.

"Kevbo" <Kevin.clark@accessbc.com.nospam> wrote in message
news:cf12mk$7op$1@eclipse.org...
> I have resolved this problem. I'm posting the solution here for archival
> purposes, since I'm pretty sure the topic has died.
>
> It's best to use ScrolledComposite, instead of what I was originally
> trying to do (access the scroll bars on the shell). You need to set the
> size of the ScrolledComposite to the size of the client area on the Shell.
> But set the composite that you draw onto to it's default size. Then add a
> resize listener to the ScrolledComposite so that it changes sizes with the
> Shell. See the code snippet below for a better understanding.
>
>
> Kevin
>
>
>
> public static void main(String[] args) {
> //Create a display and a shell
> Display display = new Display();
> final Shell shell = new Shell(display);
>
> //Create scroll bars
> final ScrolledComposite sComp = new ScrolledComposite(shell,
> SWT.V_SCROLL);
>
> //Create the composite to draw widgets onto and put the scroll bars on
> //it
> final Composite composite = new Composite(sComp, SWT.NONE);
> sComp.setContent(composite);
>
> //Create a new layout for this screen using FormLayout
> FormLayout layout = new FormLayout();
> layout.marginWidth = 5; //Amount of space from sides
> layout.marginHeight = 5; //Amount of space from top and bottom
> composite.setLayout(layout);
>
> //**************DRAW WIDGETS****************
>
> //Set the size and configure the scrollbars
> Rectangle rect = shell.getClientArea();
> Point size = composite.computeSize(rect.width, SWT.DEFAULT);
> composite.setSize(size);
> sComp.setSize(rect.width, rect.height);
>
> shell.addListener(SWT.Resize, new Listener() {
> public void handleEvent(Event e) {
> Rectangle rect = shell.getClientArea();
> Point size = composite.computeSize(rect.width, SWT.DEFAULT);
> composite.setSize(size);
> sComp.setSize(rect.width, rect.height);
> }
> });
>
> //Start the program
> shell.open();
>
> //Wait until the program finishes before disposing the display
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) display.sleep();
> }
> display.dispose();
>
> }
>
>
>
>
>
>
>
> Kevbo wrote:
>
> > Hi,
>
> > I've searched for a decent solution on this and have attempted many
> > things. I've found several code snipets describing how to use scroll
bars,
> > but none of them seem to account for everything I need to do. Perhaps
> > someone here will be able to assist me.
>
> > I am attempting to create a stand-alone SWT program. In it, I
dynamically
> > generate screens (such as reports taken from data within a database).
> > Therefore, I never know how long the screen will be. It's quite possible
> > that even maximized the screen will need to carry on further. Naturally,
> > the solution to this problem is a vertical scrollbar. But such a simple
> > solution has proven quite difficult for me to implement.
>
> > Here is a snipet of the code I am using now:
>
> > public static void main(String[] args) {
> > //Create a display and a shell
> > Display display = new Display();
> > final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.V_SCROLL);
>
> > final Composite composite = new Composite(shell, SWT.NORMAL);
> > Rectangle shellRect = shell.getClientArea();
> > composite.setSize(shellRect.width, shellRect.height);
> > final ScrollBar vBar = shell.getVerticalBar();
> > vBar.addListener(SWT.Selection, new Listener() {
> > public void handleEvent(Event e) {
> > Point location = composite.getLocation();
> > location.y = -vBar.getSelection();
> > composite.setLocation(location);
> > }
> > });
> > shell.addListener(SWT.Resize, new Listener() {
> > public void handleEvent(Event e) {
> > Rectangle shellRect = shell.getClientArea();
> > composite.setSize(shellRect.width, shellRect.height);
> > Point size = composite.getSize();
> > Rectangle rect = shell.getClientArea();
> > vBar.setMaximum(size.y);
> > vBar.setThumb(Math.min(size.y, rect.height));
> > int vPage = size.y - rect.height;
> > int vSelection = vBar.getSelection();
> > Point location = composite.getLocation();
> > if (vSelection >= vPage) {
> > if (vPage <= 0) vSelection = 0;
> > location.y = -vSelection;
> > }
> > composite.setLocation(location);
> > }
> > });
>
> > //TODO Uncomment when you've got the scroll bar working
> > // shell.setMaximized(true);
>
> > //TODO Create a database connection and get the current months data
>
> > //Create the Report Screen/Main Screen
> > createReportScreen(composite);
>
> > //Start the program
> > shell.open();
>
> > //Wait until the program finishes before disposing the display
> > while (!shell.isDisposed()) {
> > if (!display.readAndDispatch()) display.sleep();
> > }
> > display.dispose();
>
> > }
>
>
> > What happens, is that I get a composite on screen that has a scrollbar.
> > However, when I attempt to scroll down to data off the current page, it
> > shows me nothing. Just the background shell. Also, if I resize the
shell,
> > the composite resizes properly... But again, if any data is off the
screen
> > (or takes up more space than the composite has) it cannot be scrolled
down.
>
> > I know I'm missing something small. So if anyone is kind enough to use
> > their time for this, I'd appreciate it very much.
>
> > Thanks!
> > Kevin
>
> > PS - Using FormLayout to place my widgets, if that makes any difference.
>
>
Previous Topic:Listening to Shell closing before dispose
Next Topic:SWT pocketpc support
Goto Forum:
  


Current Time: Fri Apr 26 23:16:59 GMT 2024

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

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

Back to the top