讨论:org.eclipse.jface.viewers.ComboBoxCellEditor 下拉菜单功能有时会失效的问题

megson 2009-04-09 11:00:33
大家好,

我最近使用Eclipse Version: 3.4.1 Build id: M20080911-1700开发swt/jface运用时发现,使用ComboBoxCellEditor 时,点击下拉按钮,经常会失效,即:完成点击,没有任何下拉框出来,不知大家有没有注意到这种状况?

附样例:

package beta_2.test;

/*
* CoolBar example snippet: drop-down a chevron menu containing hidden tool items
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
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;
import org.eclipse.swt.widgets.TableItem;

public class Snippet140 {
private class MyCellModifier implements ICellModifier {

private TableViewer viewer;

public MyCellModifier(TableViewer viewer) {
this.viewer = viewer;
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object,
* java.lang.String)
*/
public boolean canModify(Object element, String property) {
return true;
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object,
* java.lang.String)
*/
public Object getValue(Object element, String property) {
// We need to calculate back to the index
return new Integer(((MyModel) element).counter / 10);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object,
* java.lang.String, java.lang.Object)
*/
public void modify(Object element, String property, Object value) {
TableItem item = (TableItem) element;
// We get the index and need to calculate the real value
((MyModel) item.getData()).counter = ((Integer) value).intValue() * 10;
viewer.update(item.getData(), null);
}
}

private class MyContentProvider implements IStructuredContentProvider {

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
*/
public Object[] getElements(Object inputElement) {
return (MyModel[]) inputElement;
}

/*
* (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) {

}

}

public class MyModel {
public int counter;
public int num = 0;

public MyModel(int counter) {
this.counter = counter;
}

public String toString() {
return "Item " + this.counter;
}
}

public Snippet140(Shell shell) {
final Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
final TableViewer v = new TableViewer(table);
final MyCellModifier modifier = new MyCellModifier(v);

TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(200);

column = new TableColumn(table, SWT.NONE);
column.setWidth(200);

v.setLabelProvider(new LabelProvider());
v.setContentProvider(new MyContentProvider());
v.setCellModifier(modifier);
v.setColumnProperties(new String[] { "column1" , "column2"});
v.setCellEditors(new CellEditor[] { new ComboBoxCellEditor(
v.getTable(), new String[] { "Zero", "Ten", "Twenty", "Thirty",
"Fourty", "Fifty", "Sixty", "Seventy", "Eighty",
"Ninety" }) });

MyModel[] model = createModel();
v.setInput(model);
v.getTable().setLinesVisible(true);
}

private MyModel[] createModel() {
MyModel[] elements = new MyModel[10];

for (int i = 0; i < 10; i++) {
elements[i] = new MyModel(i * 10);
}

return elements;
}

/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new Snippet140(shell);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}

display.dispose();

}

}
...全文
213 点赞 收藏 1
写回复
1 条回复
megson 2009年04月10日
没人捧场???
回复 点赞
发动态
发帖子
Java SE
创建于2007-09-28

3.4w+

社区成员

30.7w+

社区内容

Java 2 Standard Edition
社区公告
暂无公告