我已经实现了一个树,并在SWT中添加了5个treeColumn。现在使用父treeItem和child treeItem以编程方式展开此树。现在我要编辑所有列。当我为此编写代码时,我只获得第一列进行编辑,其余列不符合条件。请建议我如何让所有列都可编辑。请找到以下代码:
private void editTreeTable(final Tree table) {
final TreeEditor editor = new TreeEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
table.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e)
Control oldEditor = editor.getEditor();
if (oldEditor != null) oldEditor.dispose();
TreeItem item = (TreeItem)e.item;
if (item == null) return;
// The control that will be the editor must be a
Text newEditor = new Text(table, SWT.NONE);
newEditor.setText(item.getText());
newEditor.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
Text text=(Text)editor.getEditor();
editor.getItem().setText(text.getText());
}
});
newEditor.selectAll();
newEditor.setFocus();
editor.setEditor(newEditor, item);`
}
});
}
答案 0 :(得分:0)
这里的挑战是知道哪个列已被选中。实现此目的的一种方法是使用MouseEvent
并从TreeItem
的坐标获取相应的MouseEvent
和列索引。
查看Tree
课程,我们找到方法getItem(Point),TreeItem
课程提供方法getBounds(int)。
将getItem(Point)
与Point
中的MouseEvent
一起使用,然后迭代列数(请参阅:Tree.getColumnCount())并将索引传递给getBounds(int)
,我们可以轻松识别要提供给TreeEditor.setEditor(Control, TreeItem, int)方法的TreeItem
和列索引。
例如(稍微修改代码并添加一些注释以便澄清):
// Use a MouseListener so that we can get the widget-relative x,y coordinates
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(final MouseEvent e) {
final Control oldEditor = editor.getEditor();
if (oldEditor != null) {
oldEditor.dispose();
}
// Get the Point from the MouseEvent
final Point p = new Point(e.x, e.y);
// Get the TreeItem corresponding to that point
final TreeItem item = table.getItem(p);
if (item == null) {
return;
}
// Now that we know the TreeItem, we can use the getBounds() method
// to locate the corresponding column
for (int i = 0; i < table.getColumnCount(); ++i) {
if (item.getBounds(i).contains(p)) {
final int columnIndex = i;
// The control that will be the editor must be a
final Text newEditor = new Text(table, SWT.NONE);
newEditor.setText(item.getText(columnIndex ));
newEditor.addModifyListener(new ModifyListener() {
public void modifyText(final ModifyEvent e) {
final Text text = (Text) editor.getEditor();
editor.getItem().setText(columnIndex , text.getText());
}
});
newEditor.selectAll();
newEditor.setFocus();
// Set the editor for the matching column
editor.setEditor(newEditor, item, columnIndex );
}
}
}
});
完整MCVE:
public class TreeEditorTest {
private final Display display;
private final Shell shell;
public TreeEditorTest() {
display = new Display();
shell = new Shell(display);
shell.setLayout(new FillLayout());
final Composite baseComposite = new Composite(shell, SWT.NONE);
baseComposite.setLayout(new GridLayout());
baseComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Tree table = new Tree(baseComposite, SWT.BORDER | SWT.FULL_SELECTION);
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final int columnWidth = 100;
new TreeColumn(table, SWT.NONE, 0).setWidth(columnWidth);
new TreeColumn(table, SWT.NONE, 1).setWidth(columnWidth);
new TreeColumn(table, SWT.NONE, 2).setWidth(columnWidth);
new TreeColumn(table, SWT.NONE, 3).setWidth(columnWidth);
new TreeColumn(table, SWT.NONE, 4).setWidth(columnWidth);
final String[] strings = new String[] { "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" };
final TreeItem parent = new TreeItem(table, SWT.NONE);
parent.setText(strings);
final TreeItem child1 = new TreeItem(parent, SWT.NONE);
child1.setText(strings);
final TreeItem child2 = new TreeItem(parent, SWT.NONE);
child2.setText(strings);
parent.setExpanded(true);
final TreeEditor editor = new TreeEditor(table);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(final MouseEvent e) {
final Control oldEditor = editor.getEditor();
if (oldEditor != null) {
oldEditor.dispose();
}
final Point p = new Point(e.x, e.y);
final TreeItem item = table.getItem(p);
if (item == null) {
return;
}
for (int i = 0; i < table.getColumnCount(); ++i) {
if (item.getBounds(i).contains(p)) {
final int columnIndex = i;
// The control that will be the editor must be a
final Text newEditor = new Text(table, SWT.NONE);
newEditor.setText(item.getText(columnIndex ));
newEditor.addModifyListener(new ModifyListener() {
public void modifyText(final ModifyEvent e) {
final Text text = (Text) editor.getEditor();
editor.getItem().setText(columnIndex , text.getText());
}
});
newEditor.selectAll();
newEditor.setFocus();
editor.setEditor(newEditor, item, columnIndex );
}
}
}
});
}
public void run() {
shell.setSize(600, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(final String... args) {
new TreeEditorTest().run();
}
}