SWT List SWT(JFace)体验之List演示汇总
人气:0想了解SWT(JFace)体验之List演示汇总的相关内容吗,在本文为您仔细讲解SWT List的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SWT,List,下面大家一起来学习吧。
代码如下:DropDownAndSimple.java
复制代码 代码如下:
package swt_jface.demo3;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Combo;
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() {
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();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new DropDownAndSimple();
}
}
SampleCombo.java
复制代码 代码如下:
package swt_jface.demo3;
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.Combo;
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() {
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();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new SampleCombo();
}
}
SampleList.java
复制代码 代码如下:
package swt_jface.demo3;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class SampleList {
Display display = new Display();
Shell shell = new Shell(display);
public SampleList() {
RowLayout rowLayout = new RowLayout();
shell.setLayout(rowLayout);
(new Label(shell, SWT.NULL)).setText("What programming languages are you proficient in? ");
final List list = new List(shell, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL);
String[] languages = new String[]{"Java", "C", "C++", "SmallTalk"};
for(int i=0; i<languages.length; i++)
list.add(languages[i]);
list.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
System.err.println(list.getSelectionIndex());
int[] indices = list.getSelectionIndices();
String[] items = list.getSelection();
StringBuffer sb = new StringBuffer("Selected indices: ");
for(int i=0; i < indices.length; i++) {
sb.append(indices[i]);
sb.append("(");
sb.append(items[i]);
sb.append(")");
if(i == indices.length-1)
sb.append('.');
else
sb.append(", ");
}
System.out.println(sb.toString());
}
public void widgetDefaultSelected(SelectionEvent e) {
int[] indices = list.getSelectionIndices();
String[] items = list.getSelection();
StringBuffer sb = new StringBuffer("Default selected indices: ");
for(int i=0; i < indices.length; i++) {
sb.append(indices[i]);
sb.append("(");
sb.append(items[i]);
sb.append(")");
if(i == indices.length-1)
sb.append('.');
else
sb.append(", ");
}
System.out.println(sb.toString());
}
});
list.selectAll();
//list.select(1);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new SampleList();
}
}
SingleMultiLists.java
复制代码 代码如下:
package swt_jface.demo3;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class SingleMultiLists {
Display display = new Display();
Shell shell = new Shell(display);
public SingleMultiLists() {
GridLayout gridLayout = new GridLayout(2, true);
shell.setLayout(gridLayout);
(new Label(shell, SWT.NULL)).setText("SINGLE");
(new Label(shell, SWT.NULL)).setText("MULTI");
List singleSelectList = new List(shell, SWT.BORDER);
List mutliSelectList = new List(shell, SWT.MULTI | SWT.BORDER);
String[] items = new String[]{"Item 1", "Item 2", "Item 3", "Item 4"};
for(int i=0; i<items.length; i++) {
singleSelectList.add(items[i]);
mutliSelectList.add(items[i]);
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new SingleMultiLists();
}
}
最后再看一个综合性强一点的:
SampleListViewer.java
复制代码 代码如下:
package swt_jface.demo3;
import java.util.Iterator;
import java.util.Vector;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.viewers.ViewerSorter;
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.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class SampleListViewer {
Display display = new Display();
Shell shell = new Shell(display);
ListViewer listViewer;
public static class Language {
public String genre;
public boolean isObjectOriented;
public Language() { }
public Language(String genre, boolean isObjectOriented) {
this.genre = genre;
this.isObjectOriented = isObjectOriented;
}
public String toString() {
return "Lang: " + genre + " [" + (isObjectOriented ? "Object-oriented" : "Procedural") + "]";
}
}
Vector languages = new Vector();
private void init() {
languages.add(new Language("Java", true));
languages.add(new Language("C", false));
languages.add(new Language("C++", true));
languages.add(new Language("SmallTalk", true));
listViewer = new ListViewer(shell);
listViewer.setContentProvider(new IStructuredContentProvider() {
public Object[] getElements(Object inputElement) {
Vector v = (Vector)inputElement;
return v.toArray();
}
public void dispose() {
System.out.println("Disposing ...");
}
public void inputChanged(
Viewer viewer,
Object oldInput,
Object newInput) {
System.out.println("Input changed: old=" + oldInput + ", new=" + newInput);
}
});
//listViewer.setContentProvider(new ArrayContentProvider());
listViewer.setInput(languages);
listViewer.setLabelProvider(new LabelProvider() {
public Image getImage(Object element) {
return null;
}
public String getText(Object element) {
return ((Language)element).genre;
}
});
listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = (IStructuredSelection)event.getSelection();
StringBuffer sb = new StringBuffer("Selection - ");
sb.append("tatal " + selection.size() + " items selected: ");
for(Iterator iterator = selection.iterator(); iterator.hasNext(); ) {
sb.append(iterator.next() + ", ");
}
System.out.println(sb);
}
});
listViewer.addFilter(new ViewerFilter() {
public boolean select(
Viewer viewer,
Object parentElement,
Object element) {
if(((Language)element).isObjectOriented)
return true;
else
return false;
}
});
listViewer.setSorter(new ViewerSorter(){
public int compare(Viewer viewer, Object e1, Object e2) {
return ((Language)e1).genre.compareTo(((Language)e2).genre);
}
});
// Object[] toBeSelectedItems = new Object[2];
// toBeSelectedItems[0] = languages.elementAt(languages.size()-1);
// toBeSelectedItems[1] = languages.elementAt(languages.size()-2);
// IStructuredSelection selection = new StructuredSelection(toBeSelectedItems);
//
// listViewer.setSelection(selection);
// Vector v = new Vector();
// v.addElement(new Language("VB", true));
//
// listViewer.setInput(v);
//
// listViewer.add(new Language("C#", true));
}
Button buttonAdd;
Button buttonRemove;
Button buttonModify;
private void addButtons() {
Composite composite = new Composite(shell, SWT.NULL);
FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
fillLayout.spacing = 2;
composite.setLayout(fillLayout);
buttonAdd = new Button(composite, SWT.PUSH);
buttonAdd.setText("Add");
buttonModify = new Button(composite, SWT.PUSH);
buttonModify.setText("Modify");
buttonRemove = new Button(composite, SWT.PUSH);
buttonRemove.setText("Remove");
buttonAdd.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String text = OptionPane.showInputDialog(shell, "New language genre: ", "Add new element", null);
if(text != null) {
languages.add(new Language(text, true));
}
listViewer.refresh(false);
}
});
buttonModify.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
IStructuredSelection selection = (IStructuredSelection)listViewer.getSelection();
Language language = (Language)selection.getFirstElement();
if(language == null) {
System.out.println("Please select a language first.");
return;
}
String text = OptionPane.showInputDialog(shell, "Rename: ", "Modify genre", language.genre);
if(text != null) {
language.genre = text;
}
listViewer.update(language, null);
}
});
buttonRemove.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
IStructuredSelection selection = (IStructuredSelection)listViewer.getSelection();
Language language = (Language)selection.getFirstElement();
if(language == null) {
System.out.println("Please select a language first.");
return;
}
languages.remove(language);
System.out.println("Removed: " + language);
listViewer.refresh(false);
}
});
}
public SampleListViewer() {
RowLayout rowLayout = new RowLayout();
shell.setLayout(rowLayout);
(new Label(shell, SWT.NULL)).setText("What programming languages are you proficient in? ");
init();
addButtons();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(String[] args) {
new SampleListViewer();
}
}
OptionPane.java
复制代码 代码如下:
package swt_jface.demo3;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
public class OptionPane {
public static String showInputDialog(Shell parent, String message, String title, String initialValue) {
final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.RESIZE);
//shell.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
if(title != null)
shell.setText(title);
GridLayout gridLayout = new GridLayout(1, false);
shell.setLayout(gridLayout);
Composite content = new Composite(shell, SWT.NULL);
content.setLayout(new RowLayout());
//content.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_CYAN));
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
content.setLayoutData(gridData);
(new Label(content, SWT.NULL)).setText(message);
final Text text = new Text(content, SWT.SINGLE | SWT.BORDER);
if(initialValue != null)
text.setText(initialValue);
Composite buttons =new Composite(shell, SWT.NULL);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
buttons.setLayoutData(gridData);
FillLayout fillLayout = new FillLayout();
fillLayout.spacing = 5;
buttons.setLayout(fillLayout);
final Button buttonOK = new Button(buttons, SWT.PUSH);
final Button buttonCancel = new Button(buttons, SWT.PUSH);
buttonOK.setText("OK");
buttonCancel.setText("Cancel");
final class DialogSelectionListener implements SelectionListener {
public String returnText;
public void widgetSelected(SelectionEvent e) {
onSelected(e.widget);
}
public void widgetDefaultSelected(SelectionEvent e) {
onSelected(e.widget);
}
private void onSelected(Widget widget) {
if(widget == buttonOK)
returnText = text.getText();
shell.dispose();
}
}
DialogSelectionListener listener = new DialogSelectionListener();
buttonCancel.addSelectionListener(listener);
buttonOK.addSelectionListener(listener);
shell.pack();
if(parent != null) {
Rectangle parentBounds = parent.getBounds();
Point shellSize = shell.getSize();
shell.setLocation( parentBounds.x+(parentBounds.width-shellSize.x)/2, parentBounds.y+(parentBounds.height-shellSize.y)/2 );
}
shell.open();
while (!shell.isDisposed()) {
if (!shell.getDisplay().readAndDispatch()) {
shell.getDisplay().sleep();
}
}
return listener.returnText;
}
}
加载全部内容