I am attempting to create a login GUI and when I press the submit button nothing happens. The other two buttons work as intended. I have attempted to add in a JOptionPane, but that seems to do nothing as well. Does anyone have any tips?
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
shell.setSize(450, 300);
shell.setText("Homework 1 GUI");
CLabel lblHomework1Login = new CLabel(shell, SWT.NONE);
lblHomework1Login.setBounds(141, 10, 149, 26);
lblHomework1Login.setText("Homework 1 Login");
CLabel lblUsername = new CLabel(shell, SWT.NONE);
lblUsername.setBounds(10, 74, 76, 26);
lblUsername.setText("Username");
CLabel lblPassword = new CLabel(shell, SWT.NONE);
lblPassword.setBounds(10, 129, 76, 26);
lblPassword.setText("Password");
txtUsername = new Text(shell, SWT.BORDER);
txtUsername.setBounds(117, 74, 255, 26);
txtPassword = new Text(shell, SWT.BORDER);
txtPassword.setBounds(116, 129, 256, 26);
Button btnSubmit = new Button(shell, SWT.BORDER);
btnSubmit.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_FOREGROUND));
btnSubmit.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String password = txtPassword.getText();
String username = txtUsername.getText();
// Implementing an action event listener class with conditional statement
@Override
public void actionPerformed(ActionEvent arg0) {
String username = txtUsername.getText();
String password = txtPassword.getText();
if (username.equals("student1") && password.equals("987123"))
JOptionPane.showMessageDialog(null, "Login Successful");
else
JOptionPane.showMessageDialog(null, "Username or Password mismatch ");
}
}
});
Newest Edit and I am presented with the following error:Syntax error on token "MessageDialog", delete this token. Also the submit button still does not work. Thanks to all who have helped.
// Implementing an selection event listener class with conditional statement
@Override
public void handle(SelectionEvent onClick) {
String Username = txtUsername.getText();
String Password = txtPassword.getText();
if (onClick.getSource() == btnSubmit)(Username.equals("student1") && Password.equals("987123"))
MessageDialog.openInformation(shell, "Login", "Login Successful");
else
MessageDialog.openError(shell, "Login", "Username or Password mismatch");
}
}
);
CodePudding user response:
The code you show doesn't compile, you can't have a actionPerformed
method nested in a widgetSelected
method like that.
JOptionPane
is a Java Swing class, do not try to mix Swing with SWT, they are different GUIs and are hard to make work together.
This code works for me:
shell.setSize(450, 300);
shell.setText("Homework 1 GUI");
shell.setLayout(new GridLayout(2, false));
Label lblHomework1Login = new Label(shell, SWT.NONE);
lblHomework1Login.setText("Homework 1 Login");
lblHomework1Login.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false, 2, 1));
Label lblUsername = new Label(shell, SWT.NONE);
lblUsername.setText("Username");
Text txtUsername = new Text(shell, SWT.BORDER);
txtUsername.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Label lblPassword = new Label(shell, SWT.NONE);
lblPassword.setText("Password");
Text txtPassword = new Text(shell, SWT.BORDER);
txtPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Button btnSubmit = new Button(shell, SWT.BORDER);
btnSubmit.setText("Submit");
btnSubmit.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
String password = txtPassword.getText();
String username = txtUsername.getText();
if (username.equals("student1") && password.equals("987123"))
MessageDialog.openInformation(shell, "Login", "Login Successful");
else
MessageDialog.openError(shell, "Login", "Username or Password mismatch");
}
});
Your btnSubmit
had no text, I added some.
I have use the JFace MessageDialog
for the messages, if you don't want to use JFace you could use the SWT MessageBox
instead.
I have use Layouts rather than setBounds. setBounds should be avoided as it will not work well with different fonts.
To use MessageBox
use:
btnSubmit.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
String password = txtPassword.getText();
String username = txtUsername.getText();
MessageBox msgBox = new MessageBox(shell);
if (username.equals("student1") && password.equals("987123"))
msgBox.setMessage("Login Successful");
else
msgBox.setMessage("Username or Password mismatch");
msgBox.open();
}
});
CodePudding user response:
Whenever I used the Button
class in my Java GUIs, I would always set up the buttons as so:
Button submitButton = new Button("Submit Button")
submitButton.setOnAction(this);
Then you would need the code to "hear" when the button is pressed. The following method "hears" that the button is pressed, then runs the following code:
@Override
public void handle(ActionEvent onClick) {
if (onClick.getSource() == submitButton)
{
// Do whatever you need to do
}
}
Perks of doing it this way:
- There is a lot less code to deal with
- All you need to do is change what the
getSource()
method is set equal to - It's easier to read