如何动态隐藏或显示 SWT 密码

How to dynamically hide or show SWT password

在我的登录对话框中,有一个按钮:

Text pwdT = new Text(container, SWT.BORDER|SWT.PASSWORD);
Button plainBtn  = new Button(container,SWT.CHECK);

如果我selectplainBtn,我想让pwdT中显示的密码变成明文而不是密文?有人知道怎么做吗?

setEchoChar()方法可以控制输入的字符是否显示。

要显示实际输入的字符,像这样清除回显字符:

text.setEchoChar('[=10=]');

您甚至可以创建不带 SWT.PASSWORD 样式标志的 Text 小部件,并且只在运行时更改密码字符。

如果您的某些目标平台不支持更改 echo 字符,例如 macOS,您可以重新创建没有 SWT.PASSWORD 样式标志的密码文本字段。例如:

Text oldText = text;
Composite parent = oldText.getParent();
Control[] tabList = parent.getTabList();
// clone the old password text and dispose of it
text = new Text(parent, SWT.BORDER);
text.setText(oldText.getText());
text.setLayoutData(oldText.getLayoutData());
oldText.dispose();
// insert new password text at the right position in the tab order
for(int i = 0; i < tabList.length; i++) {
  if(tabList[i] == oldText) {
    tabList[i] = text;
  }
}
parent.setTabList(tabList);
parent.requestLayout();