Example usage for org.eclipse.swt.widgets Composite setLayout

List of usage examples for org.eclipse.swt.widgets Composite setLayout

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Composite setLayout.

Prototype

public void setLayout(Layout layout) 

Source Link

Document

Sets the layout which is associated with the receiver to be the argument which may be null.

Usage

From source file:Snippet135.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("SWT and Swing/AWT Example");

    Listener exitListener = new Listener() {
        public void handleEvent(Event e) {
            MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
            dialog.setText("Question");
            dialog.setMessage("Exit?");
            if (e.type == SWT.Close)
                e.doit = false;/*  w  ww.java2  s .  c om*/
            if (dialog.open() != SWT.OK)
                return;
            shell.dispose();
        }
    };
    Listener aboutListener = new Listener() {
        public void handleEvent(Event e) {
            final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
            s.setText("About");
            GridLayout layout = new GridLayout(1, false);
            layout.verticalSpacing = 20;
            layout.marginHeight = layout.marginWidth = 10;
            s.setLayout(layout);
            Label label = new Label(s, SWT.NONE);
            label.setText("SWT and AWT Example.");
            Button button = new Button(s, SWT.PUSH);
            button.setText("OK");
            GridData data = new GridData();
            data.horizontalAlignment = GridData.CENTER;
            button.setLayoutData(data);
            button.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event event) {
                    s.dispose();
                }
            });
            s.pack();
            Rectangle parentBounds = shell.getBounds();
            Rectangle bounds = s.getBounds();
            int x = parentBounds.x + (parentBounds.width - bounds.width) / 2;
            int y = parentBounds.y + (parentBounds.height - bounds.height) / 2;
            s.setLocation(x, y);
            s.open();
            while (!s.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
        }
    };
    shell.addListener(SWT.Close, exitListener);
    Menu mb = new Menu(shell, SWT.BAR);
    MenuItem fileItem = new MenuItem(mb, SWT.CASCADE);
    fileItem.setText("&File");
    Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(fileMenu);
    MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
    exitItem.setText("&Exit\tCtrl+X");
    exitItem.setAccelerator(SWT.CONTROL + 'X');
    exitItem.addListener(SWT.Selection, exitListener);
    MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH);
    aboutItem.setText("&About\tCtrl+A");
    aboutItem.setAccelerator(SWT.CONTROL + 'A');
    aboutItem.addListener(SWT.Selection, aboutListener);
    shell.setMenuBar(mb);

    RGB color = shell.getBackground().getRGB();
    Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Label locationLb = new Label(shell, SWT.NONE);
    locationLb.setText("Location:");
    Composite locationComp = new Composite(shell, SWT.EMBEDDED);
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
    ToolItem exitToolItem = new ToolItem(toolBar, SWT.PUSH);
    exitToolItem.setText("&Exit");
    exitToolItem.addListener(SWT.Selection, exitListener);
    ToolItem aboutToolItem = new ToolItem(toolBar, SWT.PUSH);
    aboutToolItem.setText("&About");
    aboutToolItem.addListener(SWT.Selection, aboutListener);
    Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    final Composite comp = new Composite(shell, SWT.NONE);
    final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER);
    Sash sash = new Sash(comp, SWT.VERTICAL);
    Composite tableComp = new Composite(comp, SWT.EMBEDDED);
    Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Composite statusComp = new Composite(shell, SWT.EMBEDDED);

    java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp);
    final java.awt.TextField locationText = new java.awt.TextField();
    locationFrame.add(locationText);

    java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(tableComp);
    java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout());
    fileTableFrame.add(panel);
    final JTable fileTable = new JTable(new FileTableModel(null));
    fileTable.setDoubleBuffered(true);
    fileTable.setShowGrid(false);
    fileTable.createDefaultColumnsFromModel();
    JScrollPane scrollPane = new JScrollPane(fileTable);
    panel.add(scrollPane);

    java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp);
    statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue));
    final java.awt.Label statusLabel = new java.awt.Label();
    statusFrame.add(statusLabel);
    statusLabel.setText("Select a file");

    sash.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            if (e.detail == SWT.DRAG)
                return;
            GridData data = (GridData) fileTree.getLayoutData();
            Rectangle trim = fileTree.computeTrim(0, 0, 0, 0);
            data.widthHint = e.x - trim.width;
            comp.layout();
        }
    });

    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
        File file = roots[i];
        TreeItem treeItem = new TreeItem(fileTree, SWT.NONE);
        treeItem.setText(file.getAbsolutePath());
        treeItem.setData(file);
        new TreeItem(treeItem, SWT.NONE);
    }
    fileTree.addListener(SWT.Expand, new Listener() {
        public void handleEvent(Event e) {
            TreeItem item = (TreeItem) e.item;
            if (item == null)
                return;
            if (item.getItemCount() == 1) {
                TreeItem firstItem = item.getItems()[0];
                if (firstItem.getData() != null)
                    return;
                firstItem.dispose();
            } else {
                return;
            }
            File root = (File) item.getData();
            File[] files = root.listFiles();
            if (files == null)
                return;
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.isDirectory()) {
                    TreeItem treeItem = new TreeItem(item, SWT.NONE);
                    treeItem.setText(file.getName());
                    treeItem.setData(file);
                    new TreeItem(treeItem, SWT.NONE);
                }
            }
        }
    });
    fileTree.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            TreeItem item = (TreeItem) e.item;
            if (item == null)
                return;
            final File root = (File) item.getData();
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    statusLabel.setText(root.getAbsolutePath());
                    locationText.setText(root.getAbsolutePath());
                    fileTable.setModel(new FileTableModel(root.listFiles()));
                }
            });
        }
    });

    GridLayout layout = new GridLayout(4, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    shell.setLayout(layout);
    GridData data;
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator1.setLayoutData(data);
    data = new GridData();
    data.horizontalSpan = 1;
    data.horizontalIndent = 10;
    locationLb.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 2;
    data.heightHint = locationText.getPreferredSize().height;
    locationComp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 1;
    toolBar.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator2.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 4;
    comp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator3.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    data.heightHint = statusLabel.getPreferredSize().height;
    statusComp.setLayoutData(data);

    layout = new GridLayout(3, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    comp.setLayout(layout);
    data = new GridData(GridData.FILL_VERTICAL);
    data.widthHint = 200;
    fileTree.setLayoutData(data);
    data = new GridData(GridData.FILL_VERTICAL);
    sash.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    tableComp.setLayoutData(data);

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

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

static void createChildren(Composite parent) {
    parent.setLayout(new RowLayout());
    List list = new List(parent, SWT.BORDER | SWT.MULTI);
    list.add("List item 1");
    list.add("List item 2");
    Label label = new Label(parent, SWT.NONE);
    label.setText("Label");
    Button button = new Button(parent, SWT.RADIO);
    button.setText("Radio Button");
    button = new Button(parent, SWT.CHECK);
    button.setText("Check box Button");
    button = new Button(parent, SWT.PUSH);
    button.setText("Push Button");
    Text text = new Text(parent, SWT.BORDER);
    text.setText("Text");
}

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

private static void createForLocale(Shell shell, Locale locale) {
    System.setProperty("swt.datetime.locale", locale.toLanguageTag());
    Composite composite = new Composite(shell, SWT.BORDER);
    composite.setLayout(new RowLayout(SWT.HORIZONTAL));
    new Label(composite, SWT.NONE).setText(locale.toLanguageTag());
    new DateTime(composite, SWT.DROP_DOWN);
    new DateTime(composite, SWT.SHORT);
    new DateTime(composite, SWT.TIME);
}

From source file:org.talend.dataprofiler.chart.util.ChartUtils.java

/**
 * Create a AWT_SWT bridge composite for displaying the <CODE>ChartPanel</CODE>.
 * //from  w w w .j a  v  a2s  . com
 * @param composite
 * @param gd
 * @param chartPanel
 */
public static ChartPanel createAWTSWTComp(Composite composite, GridData gd, JFreeChart chart) {

    ChartPanel chartPanel = new ChartPanel(chart);
    Composite frameComp = new Composite(composite, SWT.EMBEDDED);
    frameComp.setLayout(new GridLayout());
    frameComp.setLayoutData(gd);
    frameComp.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_GRAY));

    Frame frame = SWT_AWT.new_Frame(frameComp);
    frame.setLayout(new java.awt.GridLayout());
    frame.add(chartPanel);
    frame.validate();

    return chartPanel;
}

From source file:org.qsos.radar.GenerateRadar.java

/**
 * This class creates a composite in which the radar char will be
 * implemented//from  ww w.j a  v  a 2 s  .c o  m
 * 
 * @param parent
 *             Composite where the chart will be seen
 * @param Categories
 *             String[] that contains the name of the elements [4 min and 7 max] the user has chosen to visualize
 * @param Scores
 *             Double[] that contains the average score of each category
 * @return Control
 * 
 */
public static Control createChart(Composite parent, String[] Categories, double[] Scores) {

    Composite Charcomposite = new Composite(parent, SWT.EMBEDDED);
    Charcomposite.setLayout(new FillLayout());
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    //
    for (int i = 0; i < CatNumber; i++) {
        dataset.addValue(Scores[i], getTitle(), Categories[i]);
    }

    String BackGroundMire = null;

    //Configuration of the spiderwebplot
    SpiderWebPlot plot = new SpiderWebPlot();
    plot.setDataset(dataset);
    plot.setMaxValue(JQConst.RADAR_MAX_VALUE);
    plot.setSeriesPaint(JQConst.RADAR_SERIES_PAINT);
    plot.setAxisLabelGap(JQConst.RADAR_AXIS_LABEL_GAP);
    plot.setHeadPercent(JQConst.RADAR_HEAD_PERCENT);
    plot.setInteriorGap(JQConst.RADAR_INTERIOR_GAP);
    plot.setWebFilled(true);

    //The backgroundpicture used as a spiderweb is chosen according to the 
    //number of categories selected
    switch (CatNumber) {
    case 4:
        BackGroundMire = JQConst.RADAR_SPIDERWEB_4;
        break;
    case 5:
        BackGroundMire = JQConst.RADAR_SPIDERWEB_5;
        break;
    case 6:
        BackGroundMire = JQConst.RADAR_SPIDERWEB_6;
        break;
    case 7:
        BackGroundMire = JQConst.RADAR_SPIDERWEB_7;
        break;
    }
    javax.swing.ImageIcon icon = new javax.swing.ImageIcon(BackGroundMire);
    plot.setBackgroundImage(icon.getImage());

    //chart creation
    JFreeChart chart = new JFreeChart(plot);

    //Here the background color from the shell is taken in order to match AWT and SWT backgrounds
    Color backgroundColor = parent.getBackground();

    //JFreechart doesn't support SWT so we create an AWT Frame that will depend on Charcomposite
    java.awt.Frame chartPanel = SWT_AWT.new_Frame(Charcomposite);
    chartPanel.setLayout(new java.awt.GridLayout());
    ChartPanel jfreeChartPanel = new ChartPanel(chart);

    chartPanel.setBackground(new java.awt.Color(backgroundColor.getRed(), backgroundColor.getGreen(),
            backgroundColor.getBlue()));

    chartPanel.add(jfreeChartPanel);
    chartPanel.pack();

    return parent;

}

From source file:SashExampleOne.java

/**
 * Creates the contents of the main window
 * /*ww w.  j a  v a 2 s  .  c o  m*/
 * @param composite the parent composite
 */
public void createContents(Composite composite) {
    composite.setLayout(new FillLayout());
    new Text(composite, SWT.BORDER);
    new Sash(composite, SWT.VERTICAL);
    new Text(composite, SWT.BORDER);
}

From source file:CLabelTest.java

/**
 * Creates the main window's contents// w  ww.  ja  va  2 s.com
 * 
 * @param parent the main window
 */
private void createContents(Composite parent) {
    parent.setLayout(new GridLayout(1, false));

    // Create the CLabels
    CLabel left = new CLabel(parent, SWT.LEFT | SWT.SHADOW_IN);
    left.setText("Left and Shadow In");
    left.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    CLabel center = new CLabel(parent, SWT.CENTER | SWT.SHADOW_OUT);
    center.setText("Center and Shadow Out");
    center.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    CLabel right = new CLabel(parent, SWT.RIGHT | SWT.SHADOW_NONE);
    right.setText("Right and Shadow None");
    right.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
}

From source file:CLabelShort.java

/**
 * Creates the main window's contents/*  w ww. j  a  va  2s . co  m*/
 * 
 * @param parent the main window
 */
private void createContents(Composite parent) {
    parent.setLayout(new FillLayout());

    // Create the CLabel
    CLabel label = new CLabel(parent, SWT.LEFT);
    label.setText("This is a CLabel with a lot of long-winded text");
    label.setImage(lookImage);
}

From source file:ScrolledCompositeTest.java

private void createContents(Composite parent) {
    parent.setLayout(new FillLayout());

    // Create the ScrolledComposite to scroll horizontally and vertically
    ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);

    // Create a child composite to hold the controls
    Composite child = new Composite(sc, SWT.NONE);
    child.setLayout(new FillLayout());

    // Create the buttons
    new Button(child, SWT.PUSH).setText("One");
    new Button(child, SWT.PUSH).setText("Two");
    /*// w  ww .jav a 2 s  .  co  m
     * // Set the absolute size of the child child.setSize(400, 400);
     */
    // Set the child as the scrolled content of the ScrolledComposite
    sc.setContent(child);

    // Set the minimum size
    sc.setMinSize(400, 400);

    // Expand both horizontally and vertically
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
}

From source file:MainClass.java

protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(1, false));

    final Text text = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
    text.setLayoutData(new GridData(GridData.FILL_BOTH));

    Button show = new Button(composite, SWT.PUSH);
    show.setText("Show Error");
    show.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent event) {
            Status status = new Status(IStatus.ERROR, "My Plug-in ID", 0, "Status Error Message", null);
            ErrorDialog.openError(Display.getCurrent().getActiveShell(), "JFace Error", text.getText(), status);
        }/* w  ww .j  a  v  a 2s .com*/
    });
    return composite;
}