Java/SWT JFace Eclipse/List
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
Demonstrates Lists
//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 Lists
*/
public class ListExample {
// 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 FillLayout());
// Create a single-selection list
List single = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
// Add the items, one by one
for (int i = 0, n = ITEMS.length; i < n; i++) {
single.add(ITEMS[i]);
}
// Select the 5th items
single.select(4);
// Create a multiple-selection list
List multi = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
// Add the items all at once
multi.setItems(ITEMS);
// Select the 10th through 12th items
multi.select(9, 11);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
List Demo
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class SWTListDemo {
public static void main (String [] args) {
Display display = new Display ();
final Shell shell = new Shell (display);
Label label = new Label (shell, SWT.WRAP);
label.setText ("This is a long text string that will wrap when the dialog is resized.");
List list = new List (shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
list.setItems (new String [] {"Item 1", "Item2"});
Button button1 = new Button (shell, SWT.PUSH);
button1.setText ("Ok");
Button button2 = new Button (shell, SWT.PUSH);
button2.setText ("Cancel");
final int insetX = 4, insetY = 4;
FormLayout formLayout = new FormLayout ();
formLayout.marginWidth = insetX;
formLayout.marginHeight = insetY;
shell.setLayout (formLayout);
Point size = label.ruputeSize (SWT.DEFAULT, SWT.DEFAULT);
final FormData labelData = new FormData (size.x, SWT.DEFAULT);
labelData.left = new FormAttachment (0, 0);
labelData.right = new FormAttachment (100, 0);
label.setLayoutData (labelData);
shell.addListener (SWT.Resize, new Listener () {
public void handleEvent (Event e) {
Rectangle rect = shell.getClientArea ();
labelData.width = rect.width - insetX * 2;
shell.layout ();
}
});
FormData button2Data = new FormData ();
button2Data.right = new FormAttachment (100, -insetX);
button2Data.bottom = new FormAttachment (100, 0);
button2.setLayoutData (button2Data);
FormData button1Data = new FormData ();
button1Data.right = new FormAttachment (button2, -insetX);
button1Data.bottom = new FormAttachment (100, 0);
button1.setLayoutData (button1Data);
FormData listData = new FormData ();
listData.left = new FormAttachment (0, 0);
listData.right = new FormAttachment (100, 0);
listData.top = new FormAttachment (label, insetY);
listData.bottom = new FormAttachment (button2, -insetY);
list.setLayoutData (listData);
shell.pack ();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
List Example
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
public class SWTListExample {
Display d;
Shell s;
SWTListExample() {
d = new Display();
s = new Shell(d);
s.setSize(250,250);
s.setText("A List Example");
final List l = new List(s, SWT.SINGLE | SWT.BORDER);
l.setBounds(50, 50, 75, 75);
l.add("Item One");
l.add("Item Two");
l.add("Item Three");
l.add("Item Four");
l.add("Item Five");
s.open();
while(!s.isDisposed()){
if(!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv){
new SWTListExample();
}
}
List Example 2
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class ListExample2 {
Display d;
Shell s;
ListExample2() {
d = new Display();
s = new Shell(d);
s.setSize(250, 250);
s.setText("A List Example");
String items[] = { "Item One", "Item Two", "Item Three", "Item Four",
"Item Five" };
final List l = new List(s, SWT.SINGLE | SWT.BORDER);
l.setBounds(50, 50, 75, 75);
l.setItems(items);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new ListExample2();
}
}
List Example 3
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.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class ListExample3 {
Display d;
Shell s;
ListExample3() {
d = new Display();
s = new Shell(d);
s.setSize(250, 250);
s.setText("A List Example");
final List l = new List(s, SWT.MULTI | SWT.BORDER);
l.setBounds(50, 50, 75, 75);
l.add("Item One");
l.add("Item Two");
l.add("Item Three");
l.add("Item Four");
l.add("Item Five");
final Button b1 = new Button(s, SWT.PUSH | SWT.BORDER);
b1.setBounds(150, 150, 50, 25);
b1.setText("Click Me");
b1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String selected[] = l.getSelection();
for (int i = 0; i < selected.length; i++) {
System.out.println(selected[i]);
}
}
});
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new ListExample3();
}
}
Print selected items in a list
/*
* List example snippet: print selected items in a list
*
* 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.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class Snippet59 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
final List list = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
for (int i = 0; i < 128; i++)
list.add("Item " + i);
list.setBounds(0, 0, 100, 100);
list.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
String string = "";
int[] selection = list.getSelectionIndices();
for (int i = 0; i < selection.length; i++)
string += selection[i] + " ";
System.out.println("Selection={" + string + "}");
}
});
list.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event e) {
String string = "";
int[] selection = list.getSelectionIndices();
for (int i = 0; i < selection.length; i++)
string += selection[i] + " ";
System.out.println("DefaultSelection={" + string + "}");
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Sample List
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Feb 8, 2004 8:21:31 PM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
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() {
init();
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();
//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 SampleList();
}
}
Single Multi Lists
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Feb 8, 2004 8:00:38 PM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
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() {
init();
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();
//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 SingleMultiLists();
}
}
SWT List Composite
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
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.layout.FillLayout;
import org.eclipse.swt.widgets.ruposite;
public class Ch8ListComposite extends Composite {
public Ch8ListComposite(Composite parent) {
super(parent, SWT.NULL);
populateControl();
}
protected void populateControl() {
FillLayout compositeLayout = new FillLayout();
setLayout(compositeLayout);
int[] styles = { SWT.SINGLE, SWT.MULTI };
for (int style = 0; style < styles.length; style++) {
createListViewer(styles[style]);
}
}
private void createListViewer(int style) {
ListViewer viewer = new ListViewer(this, style);
viewer.setLabelProvider(new LabelProvider() {
public String getText(Object element) {
return ((ListItem) element).name;
}
});
viewer.addFilter(new ViewerFilter() {
public boolean select(Viewer viewer, Object parent, Object element) {
return ((ListItem) element).value % 2 == 0;
}
});
viewer.setSorter(new ViewerSorter() {
public int compare(Viewer viewer, Object obj1, Object obj2) {
return ((ListItem) obj2).value - ((ListItem) obj1).value;
}
});
viewer.setContentProvider(new IStructuredContentProvider() {
public Object[] getElements(Object inputElement) {
return ((List) inputElement).toArray();
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput,
Object newInput) {
}
});
List input = new ArrayList();
for (int i = 0; i < 20; i++) {
input.add(new ListItem("item " + i, i));
}
viewer.setInput(input);
}
}
class ListItem {
public String name;
public int value;
public ListItem(String n, int v) {
name = n;
value = v;
}
}
SWT List Example Demo
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
/**
* @author Steven Holzner
*
*/
public class SWTListExampleDemo {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("List Example");
shell.setSize(300, 200);
shell.setLayout(new FillLayout(SWT.VERTICAL));
final List list = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
for (int loopIndex = 0; loopIndex < 100; loopIndex++) {
list.add("Item " + loopIndex);
}
list.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
int[] selections = list.getSelectionIndices();
String outText = "";
for (int loopIndex = 0; loopIndex < selections.length; loopIndex++)
outText += selections[loopIndex] + " ";
System.out.println("You selected: " + outText);
}
public void widgetDefaultSelected(SelectionEvent event) {
int[] selections = list.getSelectionIndices();
String outText = "";
for (int loopIndex = 0; loopIndex < selections.length; loopIndex++)
outText += selections[loopIndex] + " ";
System.out.println("You selected: " + outText);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
SWT List Selection Event
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ListClass {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("List Example");
shell.setSize(300, 200);
final List list = new List(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
list.setBounds(40, 20, 220, 100);
for (int loopIndex = 0; loopIndex < 9; loopIndex++) {
list.add("Item Number " + loopIndex);
}
final Text text = new Text(shell, SWT.BORDER);
text.setBounds(60, 130, 160, 25);
list.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent event) {
int[] selectedItems = list.getSelectionIndices();
String outString = "";
for (int loopIndex = 0; loopIndex < selectedItems.length; loopIndex++)
outString += selectedItems[loopIndex] + " ";
text.setText("Selected Items: " + outString);
}
public void widgetDefaultSelected(SelectionEvent event) {
int[] selectedItems = list.getSelectionIndices();
String outString = "";
for (int loopIndex = 0; loopIndex < selectedItems.length; loopIndex++)
outString += selectedItems[loopIndex] + " ";
System.out.println("Selected Items: " + outString);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}