Making a Sash Stick : Sash « SWT « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » SWT » Sash 
17.83.3.Making a Sash StickPrevious/Next
  1. The default behavior of the sash allows dragging, but you must write code to make the sash stay.
  2. You implement an event handler to adjust the FormAttachment object associated with the sash's movable direction.
  3. For a vertical sash, adjust the left FormAttachment.
  4. For a horizontal sash, adjust the top FormAttachment.
Making a Sash Stick
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SashFormSticker {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Sash One");

    shell.setLayout(new FormLayout());

    final Sash sash = new Sash(shell, SWT.VERTICAL);
    FormData data = new FormData();
    data.top = new FormAttachment(00)// Attach to top
    data.bottom = new FormAttachment(1000)// Attach to bottom
    data.left = new FormAttachment(500)// Attach halfway across
    sash.setLayoutData(data);

    Text one = new Text(shell, SWT.BORDER);
    data = new FormData();
    data.top = new FormAttachment(00);
    data.bottom = new FormAttachment(1000);
    data.left = new FormAttachment(00);
    data.right = new FormAttachment(sash, 0);
    one.setLayoutData(data);

    Text two = new Text(shell, SWT.BORDER);
    data = new FormData();
    data.top = new FormAttachment(00);
    data.bottom = new FormAttachment(1000);
    data.left = new FormAttachment(sash, 0);
    data.right = new FormAttachment(1000);
    two.setLayoutData(data);

    
    sash.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        // Reattach to the left edge, and use the x value of the event to
        // determine the offset from the left
        ((FormDatasash.getLayoutData()).left = new FormAttachment(0, event.x);

        // Until the parent window does a layout, the sash will not be redrawn in
        // its new location. So, force a layout.
        sash.getParent().layout();
      }
    });
    
    
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();

  }
}
17.83.Sash
17.83.1.SashesSashes
17.83.2.Make Split Dragger slimMake Split Dragger slim
17.83.3.Making a Sash StickMaking a Sash Stick
17.83.4.Sash: implement a simple splitter (with a 20 pixel limit)Sash: implement a simple splitter (with a 20 pixel limit)
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.