Java/SWT JFace Eclipse/Scroll
Содержание
- 1 Create a ScrolledComposite with wrapping content
- 2 Create two ScrolledComposites that scroll in tandem
- 3 Demonstrates ScrolledComposite
- 4 Scroll a child control automatically
- 5 Scroll a control in a scrolled composite
- 6 scroll an image (flicker free, no double buffering)
- 7 Scroll SWT widgets into view when they get focus
Create a ScrolledComposite with wrapping content
/*
* Create a ScrolledComposite with wrapping content.
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class Snippet166 {
public static void main(String[] args) {
Display display = new Display();
Image image1 = display.getSystemImage(SWT.ICON_WORKING);
Image image2 = display.getSystemImage(SWT.ICON_QUESTION);
Image image3 = display.getSystemImage(SWT.ICON_ERROR);
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final ScrolledComposite scrollComposite = new ScrolledComposite(shell,
SWT.V_SCROLL | SWT.BORDER);
final Composite parent = new Composite(scrollComposite, SWT.NONE);
for (int i = 0; i <= 50; i++) {
Label label = new Label(parent, SWT.NONE);
if (i % 3 == 0)
label.setImage(image1);
if (i % 3 == 1)
label.setImage(image2);
if (i % 3 == 2)
label.setImage(image3);
}
RowLayout layout = new RowLayout(SWT.HORIZONTAL);
layout.wrap = true;
parent.setLayout(layout);
scrollComposite.setContent(parent);
scrollComposite.setExpandVertical(true);
scrollComposite.setExpandHorizontal(true);
scrollComposite.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
Rectangle r = scrollComposite.getClientArea();
scrollComposite.setMinSize(parent.ruputeSize(r.width,
SWT.DEFAULT));
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Create two ScrolledComposites that scroll in tandem
/*
* Create two ScrolledComposites that scroll in tandem.
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
public class Snippet167 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.BORDER
| SWT.H_SCROLL | SWT.V_SCROLL);
Button button1 = new Button(sc1, SWT.PUSH);
button1.setText("Button 1");
button1.setSize(400, 300);
sc1.setContent(button1);
final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.BORDER
| SWT.H_SCROLL | SWT.V_SCROLL);
Button button2 = new Button(sc2, SWT.PUSH);
button2.setText("Button 2");
button2.setSize(300, 400);
sc2.setContent(button2);
final ScrollBar vBar1 = sc1.getVerticalBar();
final ScrollBar vBar2 = sc2.getVerticalBar();
final ScrollBar hBar1 = sc1.getHorizontalBar();
final ScrollBar hBar2 = sc2.getHorizontalBar();
SelectionListener listener1 = new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
int x = hBar1.getSelection()
* (hBar2.getMaximum() - hBar2.getThumb())
/ Math.max(1, hBar1.getMaximum() - hBar1.getThumb());
int y = vBar1.getSelection()
* (vBar2.getMaximum() - vBar2.getThumb())
/ Math.max(1, vBar1.getMaximum() - vBar1.getThumb());
sc2.setOrigin(x, y);
}
};
SelectionListener listener2 = new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
int x = hBar2.getSelection()
* (hBar1.getMaximum() - hBar1.getThumb())
/ Math.max(1, hBar2.getMaximum() - hBar2.getThumb());
int y = vBar2.getSelection()
* (vBar1.getMaximum() - vBar1.getThumb())
/ Math.max(1, vBar2.getMaximum() - vBar2.getThumb());
sc1.setOrigin(x, y);
}
};
vBar1.addSelectionListener(listener1);
hBar1.addSelectionListener(listener1);
vBar2.addSelectionListener(listener2);
hBar2.addSelectionListener(listener2);
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Demonstrates ScrolledComposite
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* This class demonstrates ScrolledComposite
*/
public class ScrolledCompositeTest {
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private void createContents(Composite parent) {
parent.setLayout(new FillLayout());
// Create the ScrolledComposite to scroll horizontally and vertically
ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL
| SWT.V_SCROLL);
// Create a child composite to hold the controls
Composite child = new Composite(sc, SWT.NONE);
child.setLayout(new FillLayout());
// Create the buttons
new Button(child, SWT.PUSH).setText("One");
new Button(child, SWT.PUSH).setText("Two");
/*
* // Set the absolute size of the child child.setSize(400, 400);
*/
// Set the child as the scrolled content of the ScrolledComposite
sc.setContent(child);
// Set the minimum size
sc.setMinSize(400, 400);
// Expand both horizontally and vertically
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
}
public static void main(String[] args) {
new ScrolledCompositeTest().run();
}
}
Scroll a child control automatically
/*
* Composite example snippet: scroll a child control automatically
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.ruposite;
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;
public class Snippet9 {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.H_SCROLL
| SWT.V_SCROLL);
final Composite composite = new Composite(shell, SWT.BORDER);
composite.setSize(200, 400);
final ScrollBar hBar = shell.getHorizontalBar();
hBar.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
Point location = composite.getLocation();
location.x = -hBar.getSelection();
composite.setLocation(location);
}
});
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) {
Point size = composite.getSize();
Rectangle rect = shell.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 = composite.getLocation();
if (hSelection >= hPage) {
if (hPage <= 0)
hSelection = 0;
location.x = -hSelection;
}
if (vSelection >= vPage) {
if (vPage <= 0)
vSelection = 0;
location.y = -vSelection;
}
composite.setLocation(location);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Scroll a control in a scrolled composite
/*
* ScrolledComposite example snippet: scroll a control in a scrolled composite
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Snippet5 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
// this button is always 400 x 400. Scrollbars appear if the window is
// resized to be
// too small to show part of the button
ScrolledComposite c1 = new ScrolledComposite(shell, SWT.BORDER
| SWT.H_SCROLL | SWT.V_SCROLL);
Button b1 = new Button(c1, SWT.PUSH);
b1.setText("fixed size button");
b1.setSize(400, 400);
c1.setContent(b1);
// this button has a minimum size of 400 x 400. If the window is resized
// to be big
// enough to show more than 400 x 400, the button will grow in size. If
// the window
// is made too small to show 400 x 400, scrollbars will appear.
ScrolledComposite c2 = new ScrolledComposite(shell, SWT.BORDER
| SWT.H_SCROLL | SWT.V_SCROLL);
Button b2 = new Button(c2, SWT.PUSH);
b2.setText("expanding button");
c2.setContent(b2);
c2.setExpandHorizontal(true);
c2.setExpandVertical(true);
c2.setMinWidth(400);
c2.setMinHeight(400);
shell.setSize(600, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
scroll an image (flicker free, no double buffering)
/*
* Canvas example snippet: scroll an image (flicker free, no double buffering)
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
public class Snippet48 {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display, SWT.SHELL_TRIM
| SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL
| SWT.H_SCROLL);
Image originalImage = null;
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setText("Open an image file or cancel");
String string = dialog.open();
if (string != null) {
originalImage = new Image(display, string);
}
if (originalImage == null) {
int width = 150, height = 200;
originalImage = new Image(display, width, height);
GC gc = new GC(originalImage);
gc.fillRectangle(0, 0, width, height);
gc.drawLine(0, 0, width, height);
gc.drawLine(0, height, width, 0);
gc.drawText("Default Image", 10, 10);
gc.dispose();
}
final Image image = originalImage;
final Point origin = new Point(0, 0);
final ScrollBar hBar = shell.getHorizontalBar();
hBar.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
int hSelection = hBar.getSelection();
int destX = -hSelection - origin.x;
Rectangle rect = image.getBounds();
shell.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
origin.x = -hSelection;
}
});
final ScrollBar vBar = shell.getVerticalBar();
vBar.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
int vSelection = vBar.getSelection();
int destY = -vSelection - origin.y;
Rectangle rect = image.getBounds();
shell.scroll(0, destY, 0, 0, rect.width, rect.height, false);
origin.y = -vSelection;
}
});
shell.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event e) {
Rectangle rect = image.getBounds();
Rectangle client = shell.getClientArea();
hBar.setMaximum(rect.width);
vBar.setMaximum(rect.height);
hBar.setThumb(Math.min(rect.width, client.width));
vBar.setThumb(Math.min(rect.height, client.height));
int hPage = rect.width - client.width;
int vPage = rect.height - client.height;
int hSelection = hBar.getSelection();
int vSelection = vBar.getSelection();
if (hSelection >= hPage) {
if (hPage <= 0)
hSelection = 0;
origin.x = -hSelection;
}
if (vSelection >= vPage) {
if (vPage <= 0)
vSelection = 0;
origin.y = -vSelection;
}
shell.redraw();
}
});
shell.addListener(SWT.Paint, new Listener() {
public void handleEvent(Event e) {
GC gc = e.gc;
gc.drawImage(image, origin.x, origin.y);
Rectangle rect = image.getBounds();
Rectangle client = shell.getClientArea();
int marginWidth = client.width - rect.width;
if (marginWidth > 0) {
gc.fillRectangle(rect.width, 0, marginWidth, client.height);
}
int marginHeight = client.height - rect.height;
if (marginHeight > 0) {
gc
.fillRectangle(0, rect.height, client.width,
marginHeight);
}
}
});
shell.setSize(200, 150);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Scroll SWT widgets into view when they get focus
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
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.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
/*
* Scroll widgets into view when they get focus
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
public class Snippet188 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Button b1 = new Button(shell, SWT.PUSH);
b1.setText("top");
b1
.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
1, 1));
final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER
| SWT.H_SCROLL | SWT.V_SCROLL);
sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Composite c = new Composite(sc, SWT.NONE);
c.setLayout(new GridLayout(10, true));
for (int i = 0; i < 300; i++) {
Button b = new Button(c, SWT.PUSH);
b.setText("Button " + i);
}
Button b2 = new Button(shell, SWT.PUSH);
b2.setText("bottom");
b2
.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
1, 1));
sc.setContent(c);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(c.ruputeSize(SWT.DEFAULT, SWT.DEFAULT));
Listener listener = new Listener() {
public void handleEvent(Event e) {
Control child = (Control) e.widget;
Rectangle bounds = child.getBounds();
Rectangle area = sc.getClientArea();
Point origin = sc.getOrigin();
if (origin.x > bounds.x)
origin.x = Math.max(0, bounds.x);
if (origin.y > bounds.y)
origin.y = Math.max(0, bounds.y);
if (origin.x + area.width < bounds.x + bounds.width)
origin.x = Math
.max(0, bounds.x + bounds.width - area.width);
if (origin.y + area.height < bounds.y + bounds.height)
origin.y = Math.max(0, bounds.y + bounds.height
- area.height);
sc.setOrigin(origin);
}
};
Control[] controls = c.getChildren();
for (int i = 0; i < controls.length; i++) {
controls[i].addListener(SWT.Activate, listener);
}
shell.setSize(300, 500);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}