Java/SWT JFace Eclipse/Combo

Материал из Java эксперт
Версия от 05:57, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Combo (ComboBox) Example

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTComboExample {
  Display d;
  Shell s;
  SWTComboExample() {
    d = new Display();
    s = new Shell(d);
    s.setSize(250, 250);
    s.setText("A Combo Example");
    final Combo c = new Combo(s, SWT.READ_ONLY);
    c.setBounds(50, 50, 150, 65);
    String items[] = { "Item One", "Item Two", "Item Three", "Item Four",
        "Item Five" };
    c.setItems(items);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
  public static void main(String[] argv) {
    new SWTComboExample();
  }
}





Combo (ComboBox) Example 2

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ComboExample2 {
  Display d;
  Shell s;
  ComboExample2() {
    d = new Display();
    s = new Shell(d);
    s.setSize(250, 250);
    
    s.setText("A Combo Example");
    final Combo c1 = new Combo(s, SWT.READ_ONLY);
    c1.setBounds(50, 50, 150, 65);
    final Combo c2 = new Combo(s, SWT.READ_ONLY);
    c2.setBounds(50, 85, 150, 65);
    c2.setEnabled(false);
    String items[] = { "Item One", "Item Two", "Item Three", "Item Four",
        "Item Five" };
    c1.setItems(items);
    c1.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        if (c1.getText().equals("Item One")) {
          String newItems[] = { "Item One A", "Item One B",
              "Item One C" };
          c2.setItems(newItems);
          c2.setEnabled(true);
        } else if (c1.getText().equals("Item Two")) {
          String newItems[] = { "Item Two A", "Item Two B",
              "Item Two C" };
          c2.setItems(newItems);
          c2.setEnabled(true);
        } else {
          c2.add("Not Applicable");
          c2.setText("Not Applicable");
        }
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
  public static void main(String[] argv) {
    new ComboExample2();
  }
}





Create a combo box (non-editable)

/*
 * Combo example snippet: create a combo box (non-editable)
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Snippet26 {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Combo combo = new Combo(shell, SWT.READ_ONLY);
    combo.setItems(new String[] { "A", "B", "C" });
    combo.setSize(200, 200);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





demonstrates combos (combobox)

//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
 * This class demonstrates combos
 */
public class ComboExample {
  // Strings to use as list items
  private static final String[] ITEMS = { "Alpha", "Bravo", "Charlie", "Delta",
    "Echo", "Foxtrot", "Golf", "Hotel", "India", "Juliet", "Kilo", "Lima", "Mike",
    "November", "Oscar", "Papa", "Quebec", "Romeo", "Sierra", "Tango", "Uniform",
    "Victor", "Whiskey", "X-Ray", "Yankee", "Zulu"
  };
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout());
    
    // Create a drop-down combo
    Combo combo = new Combo(shell, SWT.DROP_DOWN);
    combo.setItems(ITEMS);
    
    // Create a read only combo
    Combo readOnly = new Combo(shell, SWT.DROP_DOWN | SWT.READ_ONLY);
    readOnly.setItems(ITEMS);
    // Create a "simple" combo
    Combo simple = new Combo(shell, SWT.SIMPLE);
    simple.setItems(ITEMS);
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





DropDown Demo

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Feb 8, 2004 3:43:08 PM by JACK
 * $Id$
 * 
 * visit: http://www.asprise.ru/swt
 *****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class DropDownAndSimple {
  Display display = new Display();
  Shell shell = new Shell(display);
  public DropDownAndSimple() {
    init();
    
    RowLayout rowLayout = new RowLayout();
    rowLayout.spacing = 15;
    rowLayout.marginWidth = 15;
    rowLayout.marginHeight = 15;
    
    shell.setLayout(rowLayout);
    
    Combo comboDropDown = new Combo(shell, SWT.DROP_DOWN | SWT.BORDER);
    
    Combo comboSimple = new Combo(shell, SWT.SIMPLE | SWT.BORDER);
    
    for(int i=0; i<3; i++) {
      comboDropDown.add("item " + i);
      comboSimple.add("item " + i);
    }
    shell.pack();
    shell.open();
    //textUser.forceFocus();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
  private void init() {
  }
  public static void main(String[] args) {
    new DropDownAndSimple();
  }
}





Sample Combo

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Feb 8, 2004 9:25:39 AM by JACK
 * $Id$
 * 
 * visit: http://www.asprise.ru/swt
 *****************************************************************************/

import java.util.Arrays;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class SampleCombo {
  Display display = new Display();
  Shell shell = new Shell(display);
  public SampleCombo() {
    init();
    
    shell.setLayout(new GridLayout(2, false));
    
    (new Label(shell, SWT.NULL)).setText("Select your favorite programming language: ");
    
    //final CCombo combo = new CCombo(shell, SWT.FLAT);
    final Combo combo = new Combo(shell, SWT.NULL);
    
    String[] languages = new String[]{"Java", "C", "C++", "SmallTalk"};
    
    Arrays.sort(languages);
    
    for(int i=0; i<languages.length; i++)
      combo.add(languages[i]);
    
    //combo.add("Perl", 5);
    //combo.setItem(5, "Perl");
    
    combo.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        System.out.println("Selected index: " + combo.getSelectionIndex() + ", selected item: " + combo.getItem(combo.getSelectionIndex()) + ", text content in the text field: " + combo.getText());
      }
      public void widgetDefaultSelected(SelectionEvent e) {
        System.out.println("Default selected index: " + combo.getSelectionIndex() + ", selected item: " + (combo.getSelectionIndex() == -1 ? "<null>" : combo.getItem(combo.getSelectionIndex())) + ", text content in the text field: " + combo.getText());
        String text = combo.getText();
        if(combo.indexOf(text) < 0) { // Not in the list yet. 
          combo.add(text);
          // Re-sort
          String[] items = combo.getItems();
          Arrays.sort(items);
          combo.setItems(items);
        }
      }
    });
    shell.pack();
    shell.open();
    //textUser.forceFocus();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
  
  private void init() {
  }
  public static void main(String[] args) {
    new SampleCombo();
  }
}





SWT Combo

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.ruposite;
public class Ch5ComboComposite extends Composite {
  public Ch5ComboComposite(Composite parent) {
    super(parent, SWT.NONE);
    buildControls();
  }
  protected void buildControls() {
    setLayout(new RowLayout());
    int[] comboStyles = { SWT.SIMPLE, SWT.DROP_DOWN, SWT.READ_ONLY };
    for (int idxComboStyle = 0; idxComboStyle < comboStyles.length; ++idxComboStyle) {
      Combo combo = new Combo(this, comboStyles[idxComboStyle]);
      combo.add("Option #1");
      combo.add("Option #2");
      combo.add("Option #3");
    }
  }
}