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

时间:2017-01-03 08:19:12

标签: swt

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

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

如果我选择plainBtn,我希望pwdT中显示的密码更改为纯文本而不是密文?有谁知道怎么做?

1 个答案:

答案 0 :(得分:3)

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

要显示实际输入的字符,请清除echo char:

text.setEchoChar( '\0' );

您甚至可以在没有Text样式标志的情况下创建SWT.PASSWORD窗口小部件,并且只在运行时更改密码字符。

如果某些目标平台不支持像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 older
for( int i = 0; i < tabList.length; i++ ) {
  if( tabList[ i ] == oldText ) {
    tabList[ i ] = text;
  }
}
parent.setTabList( tabList );
parent.requestLayout();