TextCellEditor
passEdit 具有 SWT.PASSWORD 样式。但是,当点击添加按钮时,会显示原始字符,而不是默认的 * 回显字符。此外, passEdit 上的关注者也无法正常工作。当用户双击它时应该显示原始字符,并且在丢失焦点时它应该显示密码回声字符。
如何解决这个问题?
public class UserAddSolution {
public static boolean flag = true;
private TextCellEditor userNameEdit;
private TextCellEditor passEdit;
class UserNamePassword {
private final String name;
private final String password;
public UserNamePassword(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
}
public UserAddSolution(final Shell shell) {
shell.setLayout(new GridLayout(2, false));
GridData layoutData = new GridData();
layoutData.heightHint = 300;
shell.setLayoutData(layoutData);
final TableViewer viewer = new TableViewer(shell, SWT.BORDER
| SWT.FULL_SELECTION | SWT.MULTI);
viewer.setContentProvider(ArrayContentProvider.getInstance());
Table table = viewer.getTable();
table.setLayout(new GridLayout());
table.setLayoutData(layoutData);
userNameEdit = new TextCellEditor(table);
passEdit = new TextCellEditor(table, SWT.PASSWORD);
final Text tex = (Text) passEdit.getControl();
final char echar = tex.getEchoChar();
tex.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
tex.setEchoChar(echar);
}
@Override
public void focusGained(FocusEvent e) {
tex.setEchoChar('\0');
}
});
viewer.setCellEditors(new CellEditor[] { userNameEdit, passEdit });
viewer.setCellModifier(new ICellModifier() {
@Override
public boolean canModify(Object element, String property) {
return true;
}
@Override
public Object getValue(Object element, String property) {
UserNamePassword ele = (UserNamePassword) element;
if (property.equals("1")) {
return ele.getName();
} else if (property.equals("2")) {
return ele.getPassword();
}
return "";
}
@Override
public void modify(Object element, String property, Object value) {
}
});
viewer.setColumnProperties(new String[] { "1", "2" });
ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
viewer) {
@Override
protected boolean isEditorActivationEvent(
ColumnViewerEditorActivationEvent event) {
return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
}
};
int feature = ColumnViewerEditor.TABBING_HORIZONTAL
| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
| ColumnViewerEditor.TABBING_VERTICAL
| ColumnViewerEditor.KEYBOARD_ACTIVATION;
TableViewerEditor.create(viewer, actSupport, feature);
TableViewerColumn userNameColumn;
userNameColumn = new TableViewerColumn(viewer, SWT.NONE);
userNameColumn.getColumn().setWidth(200);
userNameColumn.getColumn().setMoveable(true);
userNameColumn.getColumn().setText("Name");
userNameColumn.setLabelProvider(new ColumnLabelProvider() {
@Override
public Image getImage(Object element) {
return super.getImage(element);
}
@Override
public String getText(Object element) {
UserNamePassword fdf = (UserNamePassword) element;
return super.getText(fdf.getName());
}
});
TableViewerColumn passwordColumn;
passwordColumn = new TableViewerColumn(viewer, SWT.NONE);
passwordColumn.getColumn().setWidth(200);
passwordColumn.getColumn().setMoveable(true);
passwordColumn.getColumn().setText("Password");
passwordColumn.setLabelProvider(new ColumnLabelProvider() {
@Override
public Image getImage(Object element) {
return super.getImage(element);
}
@Override
public String getText(Object element) {
UserNamePassword fdf = (UserNamePassword) element;
return super.getText(fdf.getPassword());
}
@Override
public void update(ViewerCell cell) {
super.update(cell);
}
});
viewer.setInput(new ArrayList<UserNamePassword>());
table.setLinesVisible(true);
table.setHeaderVisible(true);
viewer.refresh();
Button add = new Button(shell, SWT.PUSH);
add.setText("Add");
add.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
viewer.add(new UserNamePassword("abc", "xyz"));
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new UserAddSolution(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
答案 0 :(得分:1)
ColumnLabelProvider
负责显示表格中的数据。只有在编辑单元格时才会显示文本小部件。这就是为什么你需要告诉标签提供者掩盖字符而不是显示纯文本的原因。
因此,您不需要将SWT.PASSWORD
样式设置为passEdit
,因为您希望在编辑器中以纯文本形式显示密码:
passEdit = new TextCellEditor(table, SWT.NONE);
您也不需要FocusListener
,您可以将echo char设置为常量:
private static final String ECHARSTR = Character.toString((char)9679);
因此,您可以在初始化passEdit
我不知道JFace中是否有解决方案(我认为没有),但您可以通过修改现有的标签提供商轻松解决此问题。我的建议是修改getText()
的{{1}} ColumnLabelProvider
方法,如下所示:
passwordColumn