我正在向主GUI添加一个表。它确实显示并且具有要显示的数据。但我觉得我的代码很乱,而且结构不正确。我正在寻找一个使用SWT的人来帮助我将正确的代码放在正确的位置。
A类 - 具有TableViewer
B类 - (ArrayList
)表/ B1类数据 - ArrayList
结构的DataModel
A类 - 有创建TableViewer
//////////////////////////////////////////////////////////////////////////
// createTableViewer() //
//////////////////////////////////////////////////////////////////////////
private TableViewer createTableViewer(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
createColumns(parent, viewer);
table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
// Layout the viewer
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
viewer.setContentProvider(new ArrayContentProvider());
*** Getting Array from Class B ***
viewer.setInput(AplotDataModel.getInstance().getArrayData());
viewer.getControl().setLayoutData(gridData);
return viewer;
}
A类还有createColumns()
方法和createTableViewerColumn()
方法。
//////////////////////////////////////////////////////////////////////////
// createColumns() //
//////////////////////////////////////////////////////////////////////////
private void createColumns(final Composite parent, final TableViewer viewer) {
String[] titles = { "ItemId", "RevId", "PRL", "Dataset Name", "EC Markup" };
int[] bounds = { 150, 150, 100, 150, 100 };
TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
AplotDatasetData item = (AplotDatasetData) element;
return item.getDataset().toString();
}
});
col = createTableViewerColumn(titles[1], bounds[1], 1);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
AplotDatasetData item = (AplotDatasetData) element;
return item.getRev().toString();
}
});
col = createTableViewerColumn(titles[2], bounds[2], 2);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
AplotDatasetData item = (AplotDatasetData) element;
return item.getPRLValue();
}
});
col = createTableViewerColumn(titles[3], bounds[3], 3);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
AplotDatasetData item = (AplotDatasetData) element;
return item.getDatasetName();
}
});
col = createTableViewerColumn(titles[4], bounds[4], 4);
col.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
AplotDatasetData item = (AplotDatasetData) element;
return item.getECMarkupValue();
}
});
}
//////////////////////////////////////////////////////////////////////////
// createTableViewerColumn() //
//////////////////////////////////////////////////////////////////////////
private TableViewerColumn createTableViewerColumn(String title, int bound, final int colNumber) {
final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
column.setText(title);
column.setWidth(bound);
column.setResizable(true);
column.setMoveable(true);
return viewerColumn;
}
问题1 :这是将表添加到GUI类的最佳做法吗?看起来像GUI类的很多代码。
问题2 :createColumns()
方法和createTableViewerColumn()
方法是否应移至B类?
问题3 :表格中的最后一列将是一个下拉/组合框。所以我将不得不用EditingSupport
扩展一个类。它应该是A类还是B类?
在我进一步完成这个项目之前,我想确保我的结构正确。
答案 0 :(得分:25)
回答这个问题就像回答你喜欢哪种冰淇淋味道:)
问题1:这是将表添加到GUI的最佳做法 类?看起来像GUI类的很多代码。
如果代码不是一个坏主意。但是如果 -
Table
就足够了,但将来我可能会使用Grid
。我通常更喜欢对查看器/控件进行子类化。通过这种方式,我可以保持裸骨消息泵代码,GUI控件和我的数据模型之间的分离。
问题2:应该是createColumns()方法和 createTableViewerColumn()方法被移动到B类?
不,你不应该。在您的情况下,B类是您的数据模型/供应商。 JFace编程模型支持MVC架构,如果可能,应该遵循它。建议的解决方案是使用扩展TableViewer的新类。
问题3:我表格中的最后一栏将是一个 下拉/组合框。所以我将不得不延长一个课程 EditingSupport。它应该是A类还是B类?
我建议你去一个新类并在扩展的TableViewer中使用它。一个好处是,如果您将数据写回某个数据库,那么您的查看器类仍然是db / persistence层不可知的。
以下是一个简单的示例应用程序。如您所见,我可以通过更改OptionEditingSupport
类将我的组合编辑器更改为文本编辑器。而且,代码看起来简洁明了。
主类
package sample;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SampleApp
{
public SampleApp(Shell shell)
{
AppPersonViewer personViewer = new AppPersonViewer(shell, SWT.BORDER|SWT.V_SCROLL|SWT.FULL_SELECTION);
DataModel model = new DataModel(20);
personViewer.setInput(model);
}
public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new SampleApp(shell);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
<小时/> 扩展表查看器
package sample;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
public class AppPersonViewer extends TableViewer
{
public AppPersonViewer(Composite parent, int style)
{
super(parent, style);
Table table = getTable();
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
table.setLayoutData(gridData);
createColumns();
table.setHeaderVisible(true);
table.setLinesVisible(true);
setContentProvider(new AppContentProvider());
}
private void createColumns()
{
String[] titles = { "First Name", "Second Name", "Age", "Country", "Likes SO" };
int[] bounds = { 150, 150, 100, 150, 100 };
TableViewerColumn column = createTableViewerColumn(titles[0], bounds[0], 0);
column.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element) {
if(element instanceof Person)
return ((Person)element).getFirst();
return super.getText(element);
}
});
column = createTableViewerColumn(titles[1], bounds[1], 1);
column.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element) {
if(element instanceof Person)
return ((Person)element).getSecond();
return super.getText(element);
}
});
column = createTableViewerColumn(titles[2], bounds[2], 2);
column.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element) {
if(element instanceof Person)
return ""+((Person)element).getAge();
return super.getText(element);
}
});
column = createTableViewerColumn(titles[3], bounds[3], 3);
column.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element) {
if(element instanceof Person)
return ((Person)element).getCountry();
return super.getText(element);
}
});
column = createTableViewerColumn(titles[4], bounds[4], 4);
column.setLabelProvider(new ColumnLabelProvider(){
public String getText(Object element) {
if(element instanceof Person)
return ((Person)element).getLikes();
return super.getText(element);
}
});
column.setEditingSupport(new OptionEditingSupport(this));
}
private TableViewerColumn createTableViewerColumn(String header, int width, int idx)
{
TableViewerColumn column = new TableViewerColumn(this, SWT.LEFT, idx);
column.getColumn().setText(header);
column.getColumn().setWidth(width);
column.getColumn().setResizable(true);
column.getColumn().setMoveable(true);
return column;
}
}
<小时/> 内容提供商
package sample;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
public class AppContentProvider implements IStructuredContentProvider
{
/* (non-Javadoc)
* @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
*/
public Object[] getElements(Object inputElement) {
if(inputElement instanceof DataModel)
return ((DataModel)inputElement).getData().toArray();
return null;
}
/* (non-Javadoc)
* @see org.eclipse.jface.viewers.IContentProvider#dispose()
*/
public void dispose() {
}
/* (non-Javadoc)
* @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
*/
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
<小时/> 单元格编辑器支持
package sample;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
public class OptionEditingSupport extends EditingSupport
{
private ComboBoxCellEditor cellEditor;
public OptionEditingSupport(ColumnViewer viewer) {
super(viewer);
cellEditor = new ComboBoxCellEditor(((TableViewer)viewer).getTable(), new String[]{"Y", "N"});
}
protected CellEditor getCellEditor(Object element) {
return cellEditor;
}
protected boolean canEdit(Object element) {
return true;
}
protected Object getValue(Object element) {
return 0;
}
protected void setValue(Object element, Object value)
{
if((element instanceof Person) && (value instanceof Integer)) {
Integer choice = (Integer)value;
String option = (choice == 0? "Y":"N");
((Person)element).setLikes( option );
getViewer().update(element, null);
}
}
}
<小时/> 数据模型
package sample;
import java.util.ArrayList;
import java.util.List;
public class DataModel
{
private int samples;
public DataModel(int samples) {
this.samples = samples;
}
List<Person> getData()
{
List<Person> persons = new ArrayList<Person>();
for(int i=0; i<samples; i++)
persons.add(Person.createRandomPerson());
return persons;
}
}
<小时/> 人员实体
package sample;
import java.util.Random;
public class Person
{
private static final String[] FIRST = {"Favonius", "Tim", "Brad", "Scott", "Linda"};
private static final String[] SECOND = {"Cruise", "Temp", "Abbey", "Adam", "Albert", "Thomas"};
private static final String[] COUNTRY = {"India", "USA", "Russia", "UK", "France", "Germany"};
private static final int[] AGE = {22, 23, 24, 25, 26, 27, 28, 29, 30};
private static Random random = new Random(System.currentTimeMillis());
private String first;
private String second;
private String country;
private String likes;
private int age;
public Person(String first, String second, String country, String likes, int age)
{
super();
this.first = first;
this.second = second;
this.country = country;
this.likes = likes;
this.age = age;
}
public String getFirst() {
return first;
}
public String getSecond() {
return second;
}
public String getCountry() {
return country;
}
public String getLikes() {
return likes;
}
public int getAge() {
return age;
}
public void setLikes(String likes) {
this.likes = likes;
}
public static Person createRandomPerson(){
return new Person(FIRST[random.nextInt(FIRST.length)],
SECOND[random.nextInt(SECOND.length)], COUNTRY[random.nextInt(COUNTRY.length)],
"Y", AGE[random.nextInt(AGE.length)]);
}
}
答案 1 :(得分:0)
使用TableViewer
插入列名和表项的简单方法
学生TableViewer
package rcp_demo.Views;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.widgets.Text;
import rcp_demo.TableView.Student;
import org.eclipse.swt.widgets.TableItem;
import java.util.ArrayList;
import java.util.List;
public class TaskView extends ViewPart {
private Table table;
private Text txt_search;
private TableViewer tableViewer;
public TaskView() {
setTitleImage(SWTResourceManager.getImage("D:\\Icon\\Tasksview.png"));
}
@Override
public void createPartControl(Composite parent) {
parent.setLayout(null);
//Search Text
txt_search = new Text(parent, SWT.BORDER);
txt_search.setBounds(21, 10, 238, 19);
//TableViewer
tableViewer = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION);
table = tableViewer.getTable();
table.setBounds(21, 47, 290, 213);
table.setHeaderVisible(true);
table.setLinesVisible(true);
//Table(Student): TableViewerColumn Names
String[] Col_names={"Stud_id","Stud_Name","Stud_Gender"};
for(int i=0;i<Col_names.length;i++)
{
TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
tblclmnNewColumn.setWidth(100);
tblclmnNewColumn.setText(Col_names[i]);
}
//Table(Student) Item List
List<Student> std=new ArrayList<Student>();
std.add(new Student("110","AAA","Male"));
std.add(new Student("111","FFF","Female"));
std.add(new Student("112","MMM","Male"));
for(Student s:std)
{
TableItem std_item=new TableItem(table, SWT.NONE);
std_item.setText(0,s.getStd_id());
std_item.setText(1,s.getStd_nm());
std_item.setText(2,s.getStd_gender());
// System.out.println(s);
}
}
public TableViewer getViewer() {
return tableViewer;
}
@Override
public void setFocus() {
tableViewer.getControl().setFocus();
}
}
班级:学生
package rcp_demo.TableView;
public class Student {
private String std_id;
private String std_nm;
private String std_gender;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String sid,String snm,String sgender) {
std_id=sid;
std_nm=snm;
std_gender=sgender;
}
public String getStd_id() {
return std_id;
}
public void setStd_id(String std_id) {
this.std_id = std_id;
}
public String getStd_nm() {
return std_nm;
}
public void setStd_nm(String std_nm) {
this.std_nm = std_nm;
}
public String getStd_gender() {
return std_gender;
}
public void setStd_gender(String std_gender) {
this.std_gender = std_gender;
}
}
我获得了SWT Table,TableViewer
的最佳实践感谢..