Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

java - How to move the ScrollBar of a ScrolledComposite to bottom when adding elements?

Having a ScrolledComposite, with a button that adds more buttons in it, the idea is to move the vertical scrollbar always to bottom when a new item is added.

How can that be achieved? this is a sample code of adding buttons to a ScrolledComposite:

public boolean open() {     
    shell = new Shell(getParent(), getStyle());
    shell.setText(getText());
    
    GridLayout gl_shell = new GridLayout(1, false);
    gl_shell.marginWidth = 20;
    shell.setLayout(gl_shell);
    
    final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    final Composite c1 = new Composite(sc1, SWT.NONE);
    sc1.setContent(c1);
    final GridLayout layout = new GridLayout();
    layout.numColumns = 1;
    c1.setLayout(layout);
    final Button b1 = new Button (c1, SWT.PUSH);
    b1.setText("first button");
    c1.setSize(200,200);

    final Button add = new Button(shell, SWT.PUSH);
    add.setText("add children");
    final int[] index = new int[]{0};
    add.addListener(SWT.Selection, new Listener() {
        public void handleEvent(final Event e) {
            index[0]++;
            final Button button = new Button(c1, SWT.PUSH);
            button.setText("button "+index[0]);
            // reset size of content so children can be seen - method 1
            c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
            c1.layout();
        }
    });
    
    
    shell.layout();
    shell.pack(); //for wrap the dialog size to it's content width and height.
    
    //the dialog must be centered after doing shell.pack();
    Rectangle parentSize = getParent().getBounds();
    Rectangle shellSize = shell.getBounds();
    int x = parentSize.x + (parentSize.width - shellSize.width) / 2;
    int y = (int) (parentSize.y + (parentSize.height - shellSize.height) / 3.5);
    shell.setLocation(new Point(x, y));
    shell.open(); //we put this after setLocation() and pack() because we don't want to see the shell on the original position for some milliseconds
    
    Display display = getParent().getDisplay();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    
    
    return false;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ScrolledComposite has a

public void showControl(Control control)

method to make sure a control is visible.

There is also

public void setShowFocusedControl(boolean show)

which makes sure that the control with focus is always shown (internally this uses showControl).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...