I am using Netbeans to create a login system which then gets redirected to an Account page. However I'm stuck on this if statement and need help.
private void loginBtnActionPerformed(java.awt.event.ActionEvent evt) {
String password = passWord.getText();
String username = userName.getText();
if("red".equals(password) && ("safdari".equals(username))) {
MembershipPage Account = new MembershipPage();
Account.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "login is incorrect!");
}
}
What's expected is for another JFrame to open up after I type in the login credentials "safdari" and "red". However, for some reason that's not the case and the else statement activates instead. I'm confused new to Java btw.
Related
Today I am practicing new tech skills which is java swing with mysql for the database. My task today is to create a login form and when the user click the sign in button, user will go to the next form together with the details of users.
Goal: How to get the users details to the second form?
Is java swing have a session like what programming language have?
If yes, what term name use session on java swing programming?
Login Form:
Sign In Function:
JButton btnNewButton = new JButton("Sign In");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String email = EmailTxtInput.getText();
String password = PassTxtInput.getText();
if(email.length() == 0) {
JOptionPane.showMessageDialog(null,"Invalid Email");
}
else if(password.length() == 0) {
JOptionPane.showMessageDialog(null,"Invalid Password");
}else {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/health_check?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","");
String query = "SELECT COUNT(*) as checking_if_exist, role, first_name, users_id FROM users WHERE email = '"+email+"' AND password = '" +password +"' ";
Statement sta = con.createStatement();
ResultSet rs = sta.executeQuery(query);
rs.next();
if(rs.getInt(1) == 1) {
JOptionPane.showMessageDialog(null,"Successfully Login");
if(rs.getInt(2) == 1) {
AdminDashboardScreen admindashboardscreen = new AdminDashboardScreen();
admindashboardscreen.setUndecorated(true);
admindashboardscreen.setLocationRelativeTo(null);
admindashboardscreen.setVisible(true);
setVisible(false);
}
else if(rs.getInt(2) == 2) {
UserDashboard userdashboard = new UserDashboard();
userdashboard.setUndecorated(true);
userdashboard.setLocationRelativeTo(null);
userdashboard.setVisible(true);
setVisible(false);
}
}else {
JOptionPane.showMessageDialog(null,"Invalid Account");
}
sta.close();
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
}
});
After I solved the first problem here is what happen after I add constructor to the UserDashboard Form the gui that I created is not showing.
Here is the real output without constructor
Thank you.
You are using Eclipse WindowBuilder. In order for custom classes to appear in the design view, each one must have a default, no-arguments constructor as stipulated in the JavaBeans specification. So give class AdminDashboardScreen and class UserDashboard two constructors: one default constructor – so that you can use the class in WindowBuilder – and another constructor that takes arguments which you need when running your application.
This question already has answers here:
Pass values entered in one JFrame's text field as an input parameter in other JFrame
(4 answers)
Closed 5 years ago.
When the user login, the login frame close and open another frame where I need the user login details to get the others details. My prepareStatement & resultSet are already set at the top. Still a newbie in java
login action listener
btnLogin.addActionListener(( ActionEvent ae) -> {
if (ae.getSource().equals(btnLogin)) {
DB_connection db_connection = new DB_connection();
String username = username_login_txtf.getText();
String password = password_login_txtf.getText();
try {
String sql = "SELECT * FROM user_profile as up WHERE up.username=? AND up.user_password=?";
preStatement = db_connection.connect().prepareStatement(sql);
preStatement.setString(1, username);
preStatement.setString(2, password);
res = preStatement.executeQuery();
boolean login = false;
while(res.next()) {
if(res.getString("username").equalsIgnoreCase(username) && res.getString("user_password").equalsIgnoreCase(password)) {
JOptionPane.showMessageDialog(loginPanel, "You have Logged in!");
login = true;
fr.dispose();
Main_menu.main(null);
break;
} ...
My other frame where i need to retrieve that specific user login data
btnSearch.addActionListener(( ActionEvent ae) -> {
if (ae.getSource().equals(btnSearch)) {
// Right now i am getting the username value from another text field that I have set in this form but I want to retrieve that value directly form the login frame
String get_username = username_profile_txtf.getText();
DB_connection db_connection = new DB_connection();
try{
preStatement = db_connection.connect().prepareStatement("Select * from user_profile where username = ?");
// here I need the user login username from the login form
preStatement.setString(1, get_username);
res = preStatement.executeQuery();
if (res.next()){
String username = res.getString("username");
username_profile_txtf.setText(username);
...
}
preStatement.close();
res.close();
} catch(SQLException ex){
System.out.println(ex.getMessage());
} finally {
db_connection.disconnect();
}
}
});
Follow the MVC-pattern.
Create a model-layer which is merrily a bunch of stupid DTO classes enhanced with some infrastructure to register Listener on changes withing the model.
Create a store for username/password there.
Pass the model as dependency to any GUI class, so that all work against the same instance. (Do not use the Singelton Pattern to access the model.)
I'm making a login form in java and i having trouble with the it. Whats happening is that regardless of whether the username are correct or not the form closes. I know that the dispose; isn't suppose to be placed in its position but everytime that i attempt to place it elsewhere I'm having error in my code. This is the code that I have
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String user=jTextField1.getText();
String pwd= new String (jPasswordField1.getPassword());
if (user.equals("admin") && pwd.equals("1234"))
new Main().setVisible(true);
else {
JOptionPane.showMessageDialog(this, "Incorrect Username or Password!");
}
dispose;
}
I'm fairly new to java, so please help me
I've tried everything and i cant get with the answer, there is not much topics talking about it.
Here the escenario:
Once the user run the app, 2 FrameViews display. The main frame and the login.
Whenever if the user exist, the login frameview must be close and let me edit the main_frame.
But i cannot close the login frameview. Dispose doesnt exist, close neither. What do i have to do?
the login form is name
demo
and the main_frame
main_frame
Suggestions?
Update
HERE the code where the login login must be close
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
String pass = this.jTextField1.getText();
String user = this.jTextField2.getText();
boolean login = db.Qboolean(new String[]{
"SELECT Id_User FROM login WHERE UserName = ? AND Pass = ?",
pass +","+user,
});
if(login)
//what do i have to use here to close it!!!
else
Logs.Mensaje("No se pudo :(");
}
If you're using Netbeans , there is another way (and easy) to check username and password !
you can define a new Jfram in run(){ }
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
//here you can make log in form within while and check if the user is exist
//continue
new Graphic().setVisible(true);
}
I am developing an application in which I would like to display the username on the top of the screen after the user has logged into the system. Also I need to enable five JMenuItems only after the user has been logged in. I used the following code and called it from the successful logon If condition but it does no change to the application at all.
NOTE :- The username is to be displayed in the JFrame and the login form is a JInternalFrame
All JMenuItems are also in the JFrame
obj2 is the object created for the LoginModel class in order to retrieve the username
private String global_username="";
public String getGlobalUsername(){
return global_username;
}
The method which I call to change the state of the JMenuItems and to set the value of the JLabel
public void disableMenues(){
mntmSupplierManagement.setEnabled(false);
mntmEmployeeManagement.setEnabled(false);
mntmStockManagement.setEnabled(false);
mntmReporting.setEnabled(false);
mntmTransaction.setEnabled(false);
userName.setText("Logged in as "+obj2.getGlobalUsername());
}
I used the code below in the JInternalFrame (Login form) in order to call the above method after the user has been logged on
if(username.equals(user)&&password.equals(pass)){
System.out.println("Logged into the system");
global_username=username;
accountType=acc;
updateView();
else{
System.out.println("Unsuccessful login");
updateView();
}
Also I used the following code to create the JLabel
JLabel userName=new JLabel();
userName.setText("Logged in as "+obj2.getGlobalUsername());
This gave me a NullPointerException so I changed it to
userName.setText("Logged in as ");
Any Help is Greatly Appreciated
Thanks in Advance Everyone!!!
I would recommend having a single variable to track whether the user is logged in or not and then bind your components appropriately
private boolean loggedIn = false;
public void disableMenues(){
mntmSupplierManagement.setEnabled(!loggedIn);
mntmEmployeeManagement.setEnabled(!loggedIn);
mntmStockManagement.setEnabled(!loggedIn);
mntmReporting.setEnabled(!loggedIn);
mntmTransaction.setEnabled(!loggedIn);
userName.setText("Logged in as "+obj2.getGlobalUsername());
}
I would also suggest to track the LoginModel class throughout the session
private LoginModel lm = null;
private void authenticate(String username, String password){
//check username password (database maybe)
//and return the LoginModel for the pair
lm = returnedLoginModel;
}
And then in the JFrame all you have to do is
if(lm != null){
System.out.println("Logged into the system");
loggedIn = true;
updateView();
else{
System.out.println("Unsuccessful login");
loggedIn = false;
updateView();
}
Keep in mind that you have to track the logout as well
private void logout(){
lm = null;
}