Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » [SWT_AWT] ScrollableSwingComposite v1.1
[SWT_AWT] ScrollableSwingComposite v1.1 [message #436620] Thu, 20 May 2004 11:19 Go to next message
Henning Rogge is currently offline Henning RoggeFriend
Messages: 148
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------030507080300000803010402
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

I have finished a new version of my ScrollableSwingComposite class, this
one based on a Canvas ( no simulated scrollbars ) and supporting mouse
wheel movement.
The only problem is a little flickering during a resize operation, I was
unable to track it down. Maybe this class needs a specialized paint
listener...

You can use the class this way:

ScrollableSwingComposite scroll =
new ScrollableSwingComposite(form, SWT.V_SCROLL);
text = new JTextArea();
scroll.setViewportView(text);


Henning Rogge


--------------030507080300000803010402
Content-Type: text/plain;
name="ScrollableSwingComposite.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="ScrollableSwingComposite.java"

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ScrollBar;

public class ScrollableSwingComposite extends Canvas {
protected Composite embeddedComposite;
protected java.awt.Frame embeddedFrame;

protected ScrollBar hBar = null, vBar = null;
protected javax.swing.JScrollPane scrollpane;

static int checkStyle(int style) {
style = style & (~(SWT.EMBEDDED));
return style;
}

public ScrollableSwingComposite(Composite parent, int style) {
super(parent, checkStyle(style));

setLayout(new FillLayout());

hBar = getHorizontalBar();
vBar = getVerticalBar();

embeddedComposite = new Composite(this, SWT.EMBEDDED);

scrollpane = new javax.swing.JScrollPane(javax.swing.JScrollPane.VERTICAL_SCR OLLBAR_NEVER,
javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollpane.setWheelScrollingEnabled(true);

embeddedFrame = SWT_AWT.new_Frame(embeddedComposite);
embeddedFrame.setLayout(new java.awt.BorderLayout());
embeddedFrame.add(scrollpane, java.awt.BorderLayout.CENTER);

if (vBar != null) {
vBar.addListener(SWT.Selection, new VBarListener());
scrollpane.getVerticalScrollBar().addAdjustmentListener(new ScrollAdjustmentListener());
}
if (hBar != null) {
hBar.addListener(SWT.Selection, new HBarListener());
scrollpane.getHorizontalScrollBar().addAdjustmentListener(ne w ScrollAdjustmentListener());
}

addListener(SWT.Resize, new ResizeListener());
scrollpane.addMouseWheelListener(new MouseWheelListener());
}

public void setViewportView(java.awt.Component component) {
scrollpane.setViewportView(component);
}

private void updateSWTScrollbar() {
if (scrollpane.getViewport() == null || scrollpane.getViewport().getView() == null)
return;

java.awt.Rectangle rect = scrollpane.getViewport().getView().getBounds();

Rectangle client = getClientArea();
if (hBar != null) {
hBar.setMinimum(0);
hBar.setMaximum(rect.width);
hBar.setThumb(Math.min(rect.width, client.width));
hBar.setIncrement(hBar.getThumb() / 4);
hBar.setPageIncrement(hBar.getThumb());
hBar.setSelection(scrollpane.getHorizontalScrollBar().getVal ue());
}
if (vBar != null) {
vBar.setMinimum(0);
vBar.setMaximum(rect.height);
vBar.setThumb(Math.min(rect.height, client.height));
vBar.setIncrement(vBar.getThumb() / 4);
vBar.setPageIncrement(vBar.getThumb());
vBar.setSelection(scrollpane.getVerticalScrollBar().getValue ());
}
}

private class HBarListener implements Listener {
public void handleEvent(Event e) {
final int hSelection = hBar.getSelection();

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
scrollpane.getHorizontalScrollBar().setValue(hSelection);
}
});

}
}

private class MouseWheelListener implements java.awt.event.MouseWheelListener {
public void mouseWheelMoved(final java.awt.event.MouseWheelEvent e) {
if (e.getScrollType() != java.awt.event.MouseWheelEvent.WHEEL_UNIT_SCROLL)
return;

getDisplay().asyncExec(new Runnable() {
public void run() {
if (vBar != null) {
int move = e.getWheelRotation() * vBar.getIncrement();
vBar.setSelection(vBar.getSelection() + move);
vBar.notifyListeners(SWT.Selection, new Event());
}
else if (hBar != null) {
int move = e.getWheelRotation() * hBar.getIncrement();
hBar.setSelection(hBar.getSelection() + move);
hBar.notifyListeners(SWT.Selection, new Event());
}
}
});
}
}

private class ResizeListener implements Listener {
public void handleEvent(Event e) {
updateSWTScrollbar();
}
}
private class ScrollAdjustmentListener implements java.awt.event.AdjustmentListener {
public void adjustmentValueChanged(java.awt.event.AdjustmentEvent e) {
getDisplay().asyncExec(new Runnable() {
public void run() {
updateSWTScrollbar();
}
});
}
}

private class VBarListener implements Listener {
public void handleEvent(Event e) {
final int vSelection = vBar.getSelection();

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
scrollpane.getVerticalScrollBar().setValue(vSelection);
}
});

}
}
}
--------------030507080300000803010402--
Re: [SWT_AWT] ScrollableSwingComposite v1.1 [message #443982 is a reply to message #436620] Tue, 05 October 2004 11:26 Go to previous messageGo to next message
Nils Meyer is currently offline Nils MeyerFriend
Messages: 19
Registered: July 2009
Junior Member
Thanks for this very helpful class. Works great for me.

But when I close my Workspace down with still an
ScrollableSwingComposite open I get a Class is disposed error. Therefor
I just extended the first if in the method updateSWTScrollbar() with a
isDisposed():

if (scrollpane.getViewport() == null ||
scrollpane.getViewport().getView() == null || isDisposed())
return;

Nils Meyer

Henning Rogge wrote:
> I have finished a new version of my ScrollableSwingComposite class, this
> one based on a Canvas ( no simulated scrollbars ) and supporting mouse
> wheel movement.
> The only problem is a little flickering during a resize operation, I was
> unable to track it down. Maybe this class needs a specialized paint
> listener...
>
> You can use the class this way:
>
> ScrollableSwingComposite scroll =
> new ScrollableSwingComposite(form, SWT.V_SCROLL);
> text = new JTextArea();
> scroll.setViewportView(text);
>
>
> Henning Rogge
>
>
> ------------------------------------------------------------ ------------
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.awt.SWT_AWT;
> import org.eclipse.swt.graphics.Rectangle;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.*;
> import org.eclipse.swt.widgets.Canvas;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.ScrollBar;
>
> public class ScrollableSwingComposite extends Canvas {
> protected Composite embeddedComposite;
> protected java.awt.Frame embeddedFrame;
>
> protected ScrollBar hBar = null, vBar = null;
> protected javax.swing.JScrollPane scrollpane;
>
> static int checkStyle(int style) {
> style = style & (~(SWT.EMBEDDED));
> return style;
> }
>
> public ScrollableSwingComposite(Composite parent, int style) {
> super(parent, checkStyle(style));
>
> setLayout(new FillLayout());
>
> hBar = getHorizontalBar();
> vBar = getVerticalBar();
>
> embeddedComposite = new Composite(this, SWT.EMBEDDED);
>
> scrollpane = new javax.swing.JScrollPane(javax.swing.JScrollPane.VERTICAL_SCR OLLBAR_NEVER,
> javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
> scrollpane.setWheelScrollingEnabled(true);
>
> embeddedFrame = SWT_AWT.new_Frame(embeddedComposite);
> embeddedFrame.setLayout(new java.awt.BorderLayout());
> embeddedFrame.add(scrollpane, java.awt.BorderLayout.CENTER);
>
> if (vBar != null) {
> vBar.addListener(SWT.Selection, new VBarListener());
> scrollpane.getVerticalScrollBar().addAdjustmentListener(new ScrollAdjustmentListener());
> }
> if (hBar != null) {
> hBar.addListener(SWT.Selection, new HBarListener());
> scrollpane.getHorizontalScrollBar().addAdjustmentListener(ne w ScrollAdjustmentListener());
> }
>
> addListener(SWT.Resize, new ResizeListener());
> scrollpane.addMouseWheelListener(new MouseWheelListener());
> }
>
> public void setViewportView(java.awt.Component component) {
> scrollpane.setViewportView(component);
> }
>
> private void updateSWTScrollbar() {
> if (scrollpane.getViewport() == null || scrollpane.getViewport().getView() == null)
> return;
>
> java.awt.Rectangle rect = scrollpane.getViewport().getView().getBounds();
>
> Rectangle client = getClientArea();
> if (hBar != null) {
> hBar.setMinimum(0);
> hBar.setMaximum(rect.width);
> hBar.setThumb(Math.min(rect.width, client.width));
> hBar.setIncrement(hBar.getThumb() / 4);
> hBar.setPageIncrement(hBar.getThumb());
> hBar.setSelection(scrollpane.getHorizontalScrollBar().getVal ue());
> }
> if (vBar != null) {
> vBar.setMinimum(0);
> vBar.setMaximum(rect.height);
> vBar.setThumb(Math.min(rect.height, client.height));
> vBar.setIncrement(vBar.getThumb() / 4);
> vBar.setPageIncrement(vBar.getThumb());
> vBar.setSelection(scrollpane.getVerticalScrollBar().getValue ());
> }
> }
>
> private class HBarListener implements Listener {
> public void handleEvent(Event e) {
> final int hSelection = hBar.getSelection();
>
> javax.swing.SwingUtilities.invokeLater(new Runnable() {
> public void run() {
> scrollpane.getHorizontalScrollBar().setValue(hSelection);
> }
> });
>
> }
> }
>
> private class MouseWheelListener implements java.awt.event.MouseWheelListener {
> public void mouseWheelMoved(final java.awt.event.MouseWheelEvent e) {
> if (e.getScrollType() != java.awt.event.MouseWheelEvent.WHEEL_UNIT_SCROLL)
> return;
>
> getDisplay().asyncExec(new Runnable() {
> public void run() {
> if (vBar != null) {
> int move = e.getWheelRotation() * vBar.getIncrement();
> vBar.setSelection(vBar.getSelection() + move);
> vBar.notifyListeners(SWT.Selection, new Event());
> }
> else if (hBar != null) {
> int move = e.getWheelRotation() * hBar.getIncrement();
> hBar.setSelection(hBar.getSelection() + move);
> hBar.notifyListeners(SWT.Selection, new Event());
> }
> }
> });
> }
> }
>
> private class ResizeListener implements Listener {
> public void handleEvent(Event e) {
> updateSWTScrollbar();
> }
> }
> private class ScrollAdjustmentListener implements java.awt.event.AdjustmentListener {
> public void adjustmentValueChanged(java.awt.event.AdjustmentEvent e) {
> getDisplay().asyncExec(new Runnable() {
> public void run() {
> updateSWTScrollbar();
> }
> });
> }
> }
>
> private class VBarListener implements Listener {
> public void handleEvent(Event e) {
> final int vSelection = vBar.getSelection();
>
> javax.swing.SwingUtilities.invokeLater(new Runnable() {
> public void run() {
> scrollpane.getVerticalScrollBar().setValue(vSelection);
> }
> });
>
> }
> }
> }
Re: [SWT_AWT] ScrollableSwingComposite v1.1 [message #444055 is a reply to message #443982] Tue, 05 October 2004 23:06 Go to previous message
Henning Rogge is currently offline Henning RoggeFriend
Messages: 148
Registered: July 2009
Senior Member
Nils Meyer wrote:
> Thanks for this very helpful class. Works great for me.
>
> But when I close my Workspace down with still an
> ScrollableSwingComposite open I get a Class is disposed error. Therefor
> I just extended the first if in the method updateSWTScrollbar() with a
> isDisposed():
>
> if (scrollpane.getViewport() == null ||
> scrollpane.getViewport().getView() == null || isDisposed())
> return;

Good, I changed this in my version too... I'm not really happy with the
performance of the update, but it seems to work well at the moment.

I you have any suggestion how to do a better repaint/update, please tell me.

Henning Rogge
Previous Topic:System-wide events (was Detecting system idle time)
Next Topic:swt component in java plugin
Goto Forum:
  


Current Time: Wed Oct 21 08:08:04 GMT 2020

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

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

Back to the top