Swt/Jface listviewer的实现
前边将过了TableViewer和TreeViewer再来看ListViewer就简单了,操作步骤一样,但更简单。这里只给出使用的代码一、代码,这个实现不同于前边是分开实现(都是public 的类),这里给出的是内部类的实现方式。
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.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TestListViewer {
static class ListLabelProvider extends LabelProvider {
public String getText(Object element) {
Country country = (Country)element;
return country.getName();
}
public Image getImage(Object element) {
return null;
}
}
static class ContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
if(inputElement instanceof List){
List list = (List)inputElement;
return list.toArray();
}
return new Object[0];
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
static class Country {
private String name;
public Country(){
}
public Country(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static List getCountryList(){
List list = new ArrayList();
list.add(new Country("中国"));
list.add(new Country("美国"));
list.add(new Country("日本"));
return list;
}
}
private static org.eclipse.swt.widgets.List list;
/** *//**
* Launch the application
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
//
shell.open();
final ListViewer listViewer = new ListViewer(shell, SWT.BORDER);
listViewer.setLabelProvider(new ListLabelProvider());
listViewer.setContentProvider(new ContentProvider());
listViewer.setInput(new Object());
list = listViewer.getList();
list.setBounds(122, 63, 100, 100);
listViewer.setContentProvider(new ContentProvider());
listViewer.setLabelProvider(new ListLabelProvider());
listViewer.setInput(Country.getCountryList());
//可以直接添加
listViewer.add(new Country("英国"));
shell.setLayout(new FillLayout());
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
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.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TestListViewer {
static class ListLabelProvider extends LabelProvider {
public String getText(Object element) {
Country country = (Country)element;
return country.getName();
}
public Image getImage(Object element) {
return null;
}
}
static class ContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
if(inputElement instanceof List){
List list = (List)inputElement;
return list.toArray();
}
return new Object[0];
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
static class Country {
private String name;
public Country(){
}
public Country(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static List getCountryList(){
List list = new ArrayList();
list.add(new Country("中国"));
list.add(new Country("美国"));
list.add(new Country("日本"));
return list;
}
}
private static org.eclipse.swt.widgets.List list;
/** *//**
* Launch the application
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
//
shell.open();
final ListViewer listViewer = new ListViewer(shell, SWT.BORDER);
listViewer.setLabelProvider(new ListLabelProvider());
listViewer.setContentProvider(new ContentProvider());
listViewer.setInput(new Object());
list = listViewer.getList();
list.setBounds(122, 63, 100, 100);
listViewer.setContentProvider(new ContentProvider());
listViewer.setLabelProvider(new ListLabelProvider());
listViewer.setInput(Country.getCountryList());
//可以直接添加
listViewer.add(new Country("英国"));
shell.setLayout(new FillLayout());
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
二、注意代码的注释部分,可以直接添加和移除。