Java by API/org.eclipse.swt.widgets/Combo

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

Combo: addSelectionListener(SelectionListener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell 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[] = { "A", "B", "C" };
          c2.setItems(newItems);
          c2.setEnabled(true);
        } else if (c1.getText().equals("Item Two")) {
          String newItems[] = { "A", "B", "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();
  }
}





Combo: add(String sel)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell 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[] = { "A", "B", "C" };
          c2.setItems(newItems);
          c2.setEnabled(true);
        } else if (c1.getText().equals("Item Two")) {
          String newItems[] = { "A", "B", "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();
  }
}





Combo: getItems()

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
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 MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250, 200);
    s.setText("A KeyListener Example");
    s.setLayout(new RowLayout());
    final Combo c = new Combo(s, SWT.DROP_DOWN | SWT.BORDER);
    c.add("A");
    c.add("B");
    c.add("C");
    c.add("D");
    c.addKeyListener(new KeyListener() {
      String selectedItem = "";
      public void keyPressed(KeyEvent e) {
        if (c.getText().length() > 0) {
          return;
        }
        String key = Character.toString(e.character);
        String[] items = c.getItems();
        for (int i = 0; i < items.length; i++) {
          if (items[i].toLowerCase().startsWith(key.toLowerCase())) {
            c.select(i);
            selectedItem = items[i];
            return;
          }
        }
      }
      public void keyReleased(KeyEvent e) {
        if (selectedItem.length() > 0)
          c.setText(selectedItem);
        selectedItem = "";
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Combo: getText()

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell 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[] = { "A", "B", "C" };
          c2.setItems(newItems);
          c2.setEnabled(true);
        } else if (c1.getText().equals("Item Two")) {
          String newItems[] = { "A", "B", "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();
  }
}





Combo: select(int index)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
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 MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250, 200);
    s.setText("A KeyListener Example");
    s.setLayout(new RowLayout());
    final Combo c = new Combo(s, SWT.DROP_DOWN | SWT.BORDER);
    c.add("A");
    c.add("B");
    c.add("C");
    c.add("D");
    c.addKeyListener(new KeyListener() {
      String selectedItem = "";
      public void keyPressed(KeyEvent e) {
        if (c.getText().length() > 0) {
          return;
        }
        String key = Character.toString(e.character);
        String[] items = c.getItems();
        for (int i = 0; i < items.length; i++) {
          if (items[i].toLowerCase().startsWith(key.toLowerCase())) {
            c.select(i);
            selectedItem = items[i];
            return;
          }
        }
      }
      public void keyReleased(KeyEvent e) {
        if (selectedItem.length() > 0)
          c.setText(selectedItem);
        selectedItem = "";
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





Combo: setEnabled(boolean b)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell 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[] = { "A", "B", "C" };
          c2.setItems(newItems);
          c2.setEnabled(true);
        } else if (c1.getText().equals("Item Two")) {
          String newItems[] = { "A", "B", "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();
  }
}





Combo: setItems(String[] values)

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 MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell 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();
  }
}





new Combo(Composite parent, int style)(SWT.DROP_DOWN | SWT.BORDER)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
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 MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(250, 200);
    s.setText("A KeyListener Example");
    s.setLayout(new RowLayout());
    final Combo c = new Combo(s, SWT.DROP_DOWN | SWT.BORDER);
    c.add("A");
    c.add("B");
    c.add("C");
    c.add("D");
    c.addKeyListener(new KeyListener() {
      String selectedItem = "";
      public void keyPressed(KeyEvent e) {
        if (c.getText().length() > 0) {
          return;
        }
        String key = Character.toString(e.character);
        String[] items = c.getItems();
        for (int i = 0; i < items.length; i++) {
          if (items[i].toLowerCase().startsWith(key.toLowerCase())) {
            c.select(i);
            selectedItem = items[i];
            return;
          }
        }
      }
      public void keyReleased(KeyEvent e) {
        if (selectedItem.length() > 0)
          c.setText(selectedItem);
        selectedItem = "";
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





new Combo(Shell shell, SWT.DROP_DOWN)

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 MainClass {
  public static void main(String[] a) {
    final String[] ITEMS = { "A", "B", "C", "D", "E"};
    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();
  }
}





new Combo(Shell shell, SWT.DROP_DOWN | SWT.READ_ONLY)

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 MainClass {
  public static void main(String[] a) {
    final String[] ITEMS = { "A", "B", "C", "D", "E"};
    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();
  }
}





new Combo(Shell shell, SWT.SIMPLE)

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 MainClass {
  public static void main(String[] a) {
    final String[] ITEMS = { "A", "B", "C", "D", "E"};
    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();
  }
}





new Combo(Shell s, SWT.READ_ONLY)

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 MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell 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();
  }
}