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

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

Introduction

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

Prototype

public Point getSize() 

Source Link

Document

Returns a point describing the receiver's size.

Usage

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

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

    final Composite comp = new Composite(shell, SWT.NONE);
    final Table table = new Table(comp, SWT.BORDER | SWT.V_SCROLL);
    table.setHeaderVisible(true);// ww w .  j a v a2s  .c om
    table.setLinesVisible(true);
    final TableColumn column1 = new TableColumn(table, SWT.NONE);
    column1.setText("Column 1");
    final TableColumn column2 = new TableColumn(table, SWT.NONE);
    column2.setText("Column 2");
    for (int i = 0; i < 10; i++) {
        TableItem item = new TableItem(table, SWT.NONE);
        item.setText(new String[] { "item 0" + i, "item 1" + i });
    }
    comp.addControlListener(ControlListener.controlResizedAdapter(e -> {
        Rectangle area = comp.getClientArea();
        Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        ScrollBar vBar = table.getVerticalBar();
        int width = area.width - table.computeTrim(0, 0, 0, 0).width - vBar.getSize().x;
        if (size.y > area.height + table.getHeaderHeight()) {
            // Subtract the scrollbar width from the total column width
            // if a vertical scrollbar will be required
            Point vBarSize = vBar.getSize();
            width -= vBarSize.x;
        }
        Point oldSize = table.getSize();
        if (oldSize.x > area.width) {
            // table is getting smaller so make the columns
            // smaller first and then resize the table to
            // match the client area width
            column1.setWidth(width / 3);
            column2.setWidth(width - column1.getWidth());
            table.setSize(area.width, area.height);
        } else {
            // table is getting bigger so make the table
            // bigger first and then make the columns wider
            // to match the client area width
            table.setSize(area.width, area.height);
            column1.setWidth(width / 3);
            column2.setWidth(width - column1.getWidth());
        }
    }));

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

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

public static void main(String[] args) {
    int rowCount = 40;
    int columnCount = 15;
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 234");
    shell.setLayout(new FillLayout());

    Composite parent = new Composite(shell, SWT.BORDER);
    GridLayout layout = new GridLayout(2, false);
    layout.marginWidth = layout.marginHeight = layout.horizontalSpacing = 0;
    parent.setLayout(layout);/*w ww .j  a  v  a2s. c o m*/
    final Table leftTable = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION);
    leftTable.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true));
    leftTable.setHeaderVisible(true);
    final Table rightTable = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
    rightTable.setHeaderVisible(true);
    GridData table2Data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2);
    rightTable.setLayoutData(table2Data);
    // Create columns
    TableColumn column1 = new TableColumn(leftTable, SWT.NONE);
    column1.setText("Name");
    column1.setWidth(150);
    for (int i = 0; i < columnCount; i++) {
        TableColumn column = new TableColumn(rightTable, SWT.NONE);
        column.setText("Value " + i);
        column.setWidth(200);
    }
    // Create rows
    for (int i = 0; i < rowCount; i++) {
        TableItem item = new TableItem(leftTable, SWT.NONE);
        item.setText("item " + i);
        item = new TableItem(rightTable, SWT.NONE);
        for (int j = 0; j < columnCount; j++) {
            item.setText(j, "Item " + i + " value @ " + j);
        }
    }
    // Make selection the same in both tables
    leftTable.addListener(SWT.Selection, event -> rightTable.setSelection(leftTable.getSelectionIndices()));
    rightTable.addListener(SWT.Selection, event -> leftTable.setSelection(rightTable.getSelectionIndices()));
    // On Windows, the selection is gray if the table does not have focus.
    // To make both tables appear in focus, draw the selection background here.
    // This part only works on version 3.2 or later.
    Listener eraseListener = event -> {
        event.detail &= ~SWT.HOT;
        if ((event.detail & SWT.SELECTED) != 0) {
            GC gc = event.gc;
            Rectangle rect = event.getBounds();
            gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
            gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
            gc.fillRectangle(rect);
            event.detail &= ~SWT.SELECTED;
        }
    };

    leftTable.addListener(SWT.EraseItem, eraseListener);
    rightTable.addListener(SWT.EraseItem, eraseListener);
    // Make vertical scrollbars scroll together
    ScrollBar vBarLeft = leftTable.getVerticalBar();
    vBarLeft.addListener(SWT.Selection, event -> rightTable.setTopIndex(leftTable.getTopIndex()));
    ScrollBar vBarRight = rightTable.getVerticalBar();
    vBarRight.addListener(SWT.Selection, event -> leftTable.setTopIndex(rightTable.getTopIndex()));
    // Horizontal bar on second table takes up a little extra space.
    // To keep vertical scroll bars in sink, force table1 to end above
    // horizontal scrollbar
    ScrollBar hBarRight = rightTable.getHorizontalBar();
    Label spacer = new Label(parent, SWT.NONE);
    GridData spacerData = new GridData();
    spacerData.heightHint = hBarRight.getSize().y;
    spacer.setVisible(false);
    parent.setBackground(leftTable.getBackground());

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

From source file:TableColumnFirstFix.java

public static void main(String[] args) {
    int rowCount = 40;
    int columnCount = 15;
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Composite parent = new Composite(shell, SWT.BORDER);
    GridLayout layout = new GridLayout(2, false);
    layout.marginWidth = layout.marginHeight = layout.horizontalSpacing = 0;
    parent.setLayout(layout);//from  w  w  w. j a v a 2s .  co m
    final Table leftTable = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION);
    leftTable.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true));
    leftTable.setHeaderVisible(true);
    final Table rightTable = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
    rightTable.setHeaderVisible(true);
    GridData table2Data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2);
    rightTable.setLayoutData(table2Data);
    // Create columns
    TableColumn column1 = new TableColumn(leftTable, SWT.NONE);
    column1.setText("Name");
    column1.setWidth(150);
    for (int i = 0; i < columnCount; i++) {
        TableColumn column = new TableColumn(rightTable, SWT.NONE);
        column.setText("Value " + i);
        column.setWidth(200);
    }
    // Create rows
    for (int i = 0; i < rowCount; i++) {
        TableItem item = new TableItem(leftTable, SWT.NONE);
        item.setText("item " + i);
        item = new TableItem(rightTable, SWT.NONE);
        for (int j = 0; j < columnCount; j++) {
            item.setText(j, "Item " + i + " value @ " + j);
        }
    }
    // Make selection the same in both tables
    leftTable.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            rightTable.setSelection(leftTable.getSelectionIndices());
        }
    });
    rightTable.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            leftTable.setSelection(rightTable.getSelectionIndices());
        }
    });
    // On Windows, the selection is gray if the table does not have focus.
    // To make both tables appear in focus, draw teh selection background here.
    // This part only works on version 3.2 or later.
    Listener eraseListener = new Listener() {
        public void handleEvent(Event event) {
            if ((event.detail & SWT.SELECTED) != 0) {
                GC gc = event.gc;
                Rectangle rect = event.getBounds();
                gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
                gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
                gc.fillRectangle(rect);
                event.detail &= ~SWT.SELECTED;
            }
        }
    };

    leftTable.addListener(SWT.EraseItem, eraseListener);
    rightTable.addListener(SWT.EraseItem, eraseListener);
    // Make vertical scrollbars scroll together
    ScrollBar vBarLeft = leftTable.getVerticalBar();
    vBarLeft.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            rightTable.setTopIndex(leftTable.getTopIndex());
        }
    });
    ScrollBar vBarRight = rightTable.getVerticalBar();
    vBarRight.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            leftTable.setTopIndex(rightTable.getTopIndex());
        }
    });
    // Horizontal bar on second table takes up a little extra space.
    // To keep vertical scroll bars in sink, force table1 to end above
    // horizontal scrollbar
    ScrollBar hBarRight = rightTable.getHorizontalBar();
    Label spacer = new Label(parent, SWT.NONE);
    GridData spacerData = new GridData();
    spacerData.heightHint = hBarRight.getSize().y;
    spacer.setVisible(false);
    parent.setBackground(leftTable.getBackground());

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