Hướng dẫn và ví dụ Java SWT Password Field
Xem thêm các chuyên mục:

Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi.


Trường mật khẩu (Password field) là một thành phần giao diện người dùng cho phép người dùng nhập vào mật khẩu, nội dung của nó có thể đọc được bởi ứng dụng. Password Field không hiển thị các ký tự mà người dùng nhập vào, thay vào đó nó hiển thị dấu hoa thị tương ứng với mỗi ký tự nhập vào.
Để tạo một trường mật khẩu (password field), bạn tạo nó từ class Text với style SWT.PASSWORD. Chú ý rằng trường mật khẩu chỉ cho phép nhập trên một dòng, không cho phép người dùng nhập trên nhiều dòng.
// Tạo một trường mật khẩu.
Text passwordField = new Text(shell, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD);
// Sét ký tự đại diện cho mỗi ký tự người dùng nhập vào.
passwordField.setEchoChar('*');


PasswordFieldDemo.java
package org.o7planning.swt.passwordfield;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class PasswordFieldDemo {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
// Layout
RowLayout rowLayout = new RowLayout();
rowLayout.spacing = 10;
rowLayout.marginLeft = 10;
rowLayout.marginTop = 10;
shell.setLayout(rowLayout);
Text passwordField = new Text(shell, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD);
passwordField.setEchoChar('*');
Button button = new Button(shell, SWT.PUSH);
button.setText("Show Password");
Label labelInfo = new Label(shell, SWT.NONE);
labelInfo.setText("?");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
labelInfo.setText(passwordField.getText());
labelInfo.pack();
}
});
shell.setText("SWT Password Field (o7planning.org)");
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}