Example usage for org.eclipse.swt.widgets ScrollBar getSelection

List of usage examples for org.eclipse.swt.widgets ScrollBar getSelection

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets ScrollBar getSelection.

Prototype

public int getSelection() 

Source Link

Document

Returns the single 'selection' that is the receiver's value.

Usage

From source file:ScrollBarSelectionValue.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    // Create a List with a vertical ScrollBar
    List list = new List(shell, SWT.V_SCROLL);

    // Add a bunch of items to it
    for (int i = 0; i < 500; i++) {
        list.add("A list item");
    }// w  ww .j  a  v a  2s.  c o m

    // Get the ScrollBar
    ScrollBar sb = list.getVerticalBar();
    // Show the selection value
    System.out.println("Selection: " + sb.getSelection());

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:MainClass.java

public static void main(String[] a) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    // Create a List with a vertical scroll bar
    List list = new List(shell, SWT.V_SCROLL);

    // Add a bunch of items to it
    for (int i = 0; i < 500; i++) {
        list.add("A list item");
    }//ww w  .  j  av a 2s  .c o  m

    // Scroll to the bottom
    list.select(list.getItemCount() - 1);
    list.showSelection();

    // Get the scroll bar
    ScrollBar sb = list.getVerticalBar();

    // Add one more item that shows the selection value
    list.add("Selection: " + sb.getSelection());

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:ScrollBarExample.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    // Create a List with a vertical scroll bar
    List list = new List(shell, SWT.V_SCROLL);

    // Add a bunch of items to it
    for (int i = 0; i < 500; i++) {
        list.add("A list item");
    }/*from w  w  w . ja v  a2  s. c o  m*/

    // Scroll to the bottom
    list.select(list.getItemCount() - 1);
    list.showSelection();

    // Get the scroll bar
    ScrollBar sb = list.getVerticalBar();

    // Add one more item that shows the selection value
    list.add("Selection: " + sb.getSelection());

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet9.java

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);
    shell.setText("Snippet 9");
    final Composite composite = new Composite(shell, SWT.BORDER);
    composite.setSize(700, 600);/*from   w  w w . j  a va 2 s. c o  m*/
    final Color red = display.getSystemColor(SWT.COLOR_RED);
    composite.addPaintListener(e -> {
        e.gc.setBackground(red);
        e.gc.fillOval(5, 5, 690, 590);
    });
    final ScrollBar hBar = shell.getHorizontalBar();
    hBar.addListener(SWT.Selection, e -> {
        Point location = composite.getLocation();
        location.x = -hBar.getSelection();
        composite.setLocation(location);
    });
    final ScrollBar vBar = shell.getVerticalBar();
    vBar.addListener(SWT.Selection, e -> {
        Point location = composite.getLocation();
        location.y = -vBar.getSelection();
        composite.setLocation(location);
    });
    shell.addListener(SWT.Resize, 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.setSize(600, 500);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet167.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 167");
    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);//from   w ww. ja  va  2s . c o  m
    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 = widgetSelectedAdapter(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 = widgetSelectedAdapter(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();
}

From source file:org.eclipse.swt.snippets.Snippet48.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 48");
    shell.setLayout(new FillLayout());
    Image originalImage = null;/*from   www. j ava  2s .  c  o m*/
    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 Canvas canvas = new Canvas(shell,
            SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);
    final ScrollBar hBar = canvas.getHorizontalBar();
    hBar.addListener(SWT.Selection, e -> {
        int hSelection = hBar.getSelection();
        int destX = -hSelection - origin.x;
        Rectangle rect = image.getBounds();
        canvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
        origin.x = -hSelection;
    });
    final ScrollBar vBar = canvas.getVerticalBar();
    vBar.addListener(SWT.Selection, e -> {
        int vSelection = vBar.getSelection();
        int destY = -vSelection - origin.y;
        Rectangle rect = image.getBounds();
        canvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);
        origin.y = -vSelection;
    });
    canvas.addListener(SWT.Resize, e -> {
        Rectangle rect = image.getBounds();
        Rectangle client = canvas.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;
        }
        canvas.redraw();
    });
    canvas.addListener(SWT.Paint, e -> {
        GC gc = e.gc;
        gc.drawImage(image, origin.x, origin.y);
        Rectangle rect = image.getBounds();
        Rectangle client = canvas.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);
        }
    });
    Rectangle rect = image.getBounds();
    shell.setSize(Math.max(200, rect.width - 100), Math.max(150, rect.height - 100));
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    originalImage.dispose();
    display.dispose();
}

From source file:Snippet9.java

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);// ww w . jav a 2s.c  o m
    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();
}

From source file:ScrollBarSelectionListener.java

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);/*  w  w w .  j  a v a2s  .c  om*/
    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();
}

From source file:Snippet48.java

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;/*from   w  w w.  ja  va 2  s. c o m*/
    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();
}

From source file:ImageScrollFlickerFree.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Image originalImage = null;//from  w w  w  .  j ava2 s.c  o m
    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 Canvas canvas = new Canvas(shell,
            SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);
    final ScrollBar hBar = canvas.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();
            canvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
            origin.x = -hSelection;
        }
    });
    final ScrollBar vBar = canvas.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();
            canvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);
            origin.y = -vSelection;
        }
    });
    canvas.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event e) {
            Rectangle rect = image.getBounds();
            Rectangle client = canvas.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;
            }
            canvas.redraw();
        }
    });
    canvas.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 = canvas.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();
    }
    originalImage.dispose();
    display.dispose();
}