+ 1
Login Form JAVA
I tried to write my first code. Was not so bad but I can't figure out why the first time that i'm writing in Password Text Box the password characters are not masked with ******. But it happens only the first time that you write on that box. after you check and uncheck the box one time it is running correctly. Someone could understand why please?!? Thank you Here the Code:
5 Answers
+ 3
Honestly I don't know this SWT tool you're using, but I was thinking maybe you can try to set textbox mask character immediately after you created the password text box.
text_1 = new Text( shell, SWT_BORDER );
text_1.setBounds( 147, 160, 164, 21 );
text_1.setEchoChar( '*' );
+ 1
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
public class Login1 {
private Text text;
private Text text_1;
//Launch Application
public static void main(String[] args) {
try {
Login1 window = new Login1();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
// Open the window
public void open() {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setSize(382, 480);
shell.setText("SWT Application");
Label lblUsername = new Label(shell, SWT.NONE);
lblUsername.setBounds(36, 53, 55, 15);
lblUsername.setText("Username");
Label lblPassword = new Label(shell, SWT.NONE);
lblPassword.setBounds(36, 166, 55, 15);
lblPassword.setText("Password");
text = new Text(shell, SWT.BORDER);
text.setBounds(147, 47, 164, 21);
text_1 = new Text(shell, SWT.BORDER);
text_1.setBounds(147, 160, 164, 21);
Button btnShowPassword = new Button(shell, SWT.CHECK);
// Coding Show Password Check Button
btnShowPassword.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if(e.getSource()==btnShowPassword) {
if(btnShowPassword.getSelection()==true) {
text_1.setEchoChar((char)0);
}else {
text_1.setEchoChar('*');
}
}
}
});
btnShowPassword.setBounds(147, 237, 109, 16);
btnShowPassword.setText("Show Password");
Button btnLogin = new Button(shell, SWT.NONE);
//Coding Login Button
+ 1
also for reset button, set echo back to *
btnReset.addSelectionListener(...)
...
if (e.getSource() == btnReset) {
text.setText("");
text_1.setText("");
text_1.setEchoChar('*'); //
btnShowPassword.setSelection(false); //
}
+ 1
Thank you @Ipang, now it is working correctly ;)
0
btnLogin.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if(e.getSource()==btnLogin) {
String userTxt;
String pwdTxt;
userTxt=text.getText();
pwdTxt=text_1.getText();
if(userTxt.equalsIgnoreCase("fabio") && pwdTxt.equalsIgnoreCase("12345")) {
MessageBox messageBox=new MessageBox(shell,SWT.OK|SWT.CANCEL);
messageBox.setMessage("Login Successful!\n\n" + "Welcome: " + userTxt);
messageBox.open();
}else {
MessageBox messageBox = new MessageBox(shell, SWT.OK |
SWT.ICON_WARNING |SWT.CANCEL);
messageBox.setMessage("Ivalid Username or Password!");
messageBox.open();
}
}
}
});
btnLogin.setBounds(36, 335, 75, 25);
btnLogin.setText("Login");
Button btnReset = new Button(shell, SWT.NONE);
//Coding Reset Button
btnReset.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if(e.getSource()==btnReset) {
text.setText("");
text_1.setText("");
}
}
});
btnReset.setBounds(236, 335, 75, 25);
btnReset.setText("Reset");
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}