Java Tutorial/SWT/ScrolledComposite
Содержание
- 1 Create a ScrolledComposite with wrapping content
- 2 Create the ScrolledComposite to scroll horizontally and vertically
- 3 Create two ScrolledComposites that scroll in tandem
- 4 If the window is too small, scrollbars will appear.
- 5 Scroll a widget into view on focus in
- 6 Scrollbars appear if the window is resized to be too small to show part of the button
- 7 Setting Minimum Size for a Child Control
- 8 Setting the Child Control"s Size
Create a ScrolledComposite with wrapping content
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/
//package org.eclipse.swt.snippets; /*
* Create a ScrolledComposite with wrapping content. * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ * * @since 3.0 */
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 ScrolledCompositeCreate {
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(); }
}</source>
Create the ScrolledComposite to scroll horizontally and vertically
- SWT.H_SCROLL: horizontal scrolling.
- SWT.V_SCROLL: vertical scrolling.
- SWT.H_SCROLL | SWT.V_SCROLL: enable both.
<source lang="java">
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.ruposite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ScrollCompisiteHoriVertical {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("SashForm Test"); shell.setLayout(new FillLayout());
// Create the ScrolledComposite to scroll horizontally and vertically
ScrolledComposite sc = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL); Composite child = new Composite(sc, SWT.NONE); child.setLayout(new FillLayout()); new Button(child, SWT.PUSH).setText("One"); new Button(child, SWT.PUSH).setText("Two"); child.setSize(400, 400); sc.setContent(child); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>
Create two ScrolledComposites that scroll in tandem
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/
//package org.eclipse.swt.snippets; /*
* Create two ScrolledComposites that scroll in tandem. * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/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 ScrolledCompositeInTandem {
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(); }
}</source>
If the window is too small, scrollbars will appear.
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/
//package org.eclipse.swt.snippets; /*
* ScrolledComposite example snippet: scroll a control in a scrolled composite * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/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 ScrollBarAuto {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout()); // 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(); }
}</source>
Scroll a widget into view on focus in
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2005 IBM Corporation and others. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Contributors: IBM Corporation - initial API and implementation ******************************************************************************/
// package org.eclipse.swt.snippets; 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 a widget into view on focus in * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ * * @since 3.0 */
public class ScrollWidgetViewFocusIn {
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(); }
}</source>
Scrollbars appear if the window is resized to be too small to show part of the button
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/
//package org.eclipse.swt.snippets; /*
* ScrolledComposite example snippet: scroll a control in a scrolled composite * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/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 ScollCompositeControl {
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); shell.setSize(600, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Setting Minimum Size for a Child Control
<source lang="java">
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.ruposite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ScrolledCompositeMiniSize {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("SashForm Test"); shell.setLayout(new FillLayout()); // Create the ScrolledComposite to scroll horizontally and vertically ScrolledComposite sc = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL); Composite child = new Composite(sc, SWT.NONE); child.setLayout(new FillLayout()); new Button(child, SWT.PUSH).setText("One"); new Button(child, SWT.PUSH).setText("Two"); sc.setMinSize(400, 400); // Expand both horizontally and vertically sc.setExpandHorizontal(true); sc.setExpandVertical(true); sc.setContent(child); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>
Setting the Child Control"s Size
<source lang="java">
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.ruposite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ScrolledCompositeChildSize {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("SashForm Test"); shell.setLayout(new FillLayout());
// Create the ScrolledComposite to scroll horizontally and vertically
ScrolledComposite sc = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL); Composite child = new Composite(sc, SWT.NONE); child.setLayout(new FillLayout()); new Button(child, SWT.PUSH).setText("One"); new Button(child, SWT.PUSH).setText("Two"); child.setSize(400, 400); sc.setContent(child); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>