package com.test; import java.util.ArrayList; import java.util.List; import model.People; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.ListViewer; import org.eclipse.jface.viewers.TableViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; public class LBMShow {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() { } @Override public void inputChanged(Viewer arg0, Object arg1, Object arg2) { // TODO Auto-generated method stub } } 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); 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(); } }}
SWT的ListVierer的使用
最新推荐文章于 2021-09-23 23:20:32 发布