This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes simple login - help asp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "simple login - help asp" Watch "simple login - help asp" New topic
Author

simple login - help asp

mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24

ok this is a simple login, but ive got errors in my code or something. you get 3 attempts to login before the textboxes are disabled. what am i doing wrong here. thanks alot toothpick :roll:
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ March 30, 2004: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Welcome to JavaRanch!
Your logic doesn't make sense for what you're trying to do.
Is jButton1ActionPerformed being called once after each login attempt?
Remember that variables declared inside a method die when that method is finished, and when that method is called again, the variables are created new.
Are these hints giving you any ideas?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
ok its a simple login. you put in a username and hit the login button. it checks to see if the login is equals to "hello" for example. if its right, it comes up "logged in" if its wrong it adds 1 onto the login attempt counter. once the counter gets to 3 it doesnt allow any more attempts, this is what im trying to create.
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
im still a little lost by that reply. is the code neer correct or would it work if the varibles are moved to a different location or something, or is it completely wrong.
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
thanks man, i got recomended this site from someone who said its the best place for getting java solutions and feedback. and that sure was fast feedback.
Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
yeah, as Dirk says, you gotta "Remember that variables declared inside a method die when that method is finished, and when that method is called again, the variables are created new."
therefore the icount variable has to be stored in the main class as a data member so that you can keep track of the errors made. variables created in a method get erased after execution.
other than that, you got to be careful by using brackets {}, and remember that white spaces do not matter to the compiler, there here only for human readability.
also, you dont need a loop, since the method should only get the input when called by the Button, i assume. simply use a if-else statement to check whether the input is valid and then you decide to do this or that stuff.
[ March 29, 2004: Message edited by: Ben Buchli ]
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
still no luck man, it goesnt like declaring the varibles in void main for me. :| ive tryed my hardest with the code. ive gave up...
if (login.equals ("123"))
jLabel4.setText("logged in");
//proceed
else
icount = icount + 1;
jLabel4.setText("error" + icount);
even this doesnt seem to work
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Please post the complete code file so we can see where you have moved your variable to.
Regards
Nigel
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
/*
* LoginScreen.java
*
* Created on March 29, 2004, 2:54 PM
*/
package Login;
/**
*
* @author mo
*/
public class LoginScreen extends javax.swing.JFrame {
String username;
int retry = 0;
/** Creates new form LoginScreen */
public LoginScreen() {
initComponents();


}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
getContentPane().setLayout(null);
setTitle("Login Screen");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
getContentPane().add(jTextField1);
jTextField1.setBounds(170, 90, 120, 20);
jLabel1.setText("Login Name:");
getContentPane().add(jLabel1);
jLabel1.setBounds(70, 92, 80, 16);
jLabel2.setText("Password:");
getContentPane().add(jLabel2);
jLabel2.setBounds(70, 122, 90, 16);
getContentPane().add(jTextField2);
jTextField2.setBounds(170, 120, 120, 20);
jLabel3.setText("Attempt:");
getContentPane().add(jLabel3);
jLabel3.setBounds(70, 162, 60, 16);
jLabel4.setText("0/3");
getContentPane().add(jLabel4);
jLabel4.setBounds(140, 162, 80, 16);
jLabel5.setFont(new java.awt.Font("Verdana", 1, 24));
jLabel5.setText("Ossian Travel");
getContentPane().add(jLabel5);
jLabel5.setBounds(90, 20, 190, 40);
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(224, 160, 65, 20);
jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
getContentPane().add(jPanel1);
jPanel1.setBounds(60, 70, 240, 130);
pack();
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setSize(new java.awt.Dimension(372, 271));
setLocation((screenSize.width-372)/2,(screenSize.height-271)/2);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
username = "" + jTextField1.getText ();
if (username.equals ("123"))
jLabel4.setText("logged in");
else
retry = retry + 1;
jLabel4.setText("0/" + retry);




}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new LoginScreen().show();

}


// Variables declaration - do not modify
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel2;
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField1;
private javax.swing.JLabel jLabel5;
// End of variables declaration
-----------------------------
if i enter the wrong password it adds 1 to the 'retry' counter correctly. but if im entering the correct password, its not doing anything. not even displaying the text 'logged in'. also im unsure on how to make it only 3 attempts.
do
{
my code
}
while(retry >=3) ???
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Try this
regards
Nigel
mo robertson
Greenhorn

Joined: Mar 29, 2004
Posts: 24
works a treat man, and by looking at ur code i see what u did to achive this. very nice. and thanks alot.
while im posting ill ask another quick question. how do you make another jframe show by command button, for example if i have a GUI frame called
'login' and one called 'main'. on a command button click, how do i make the other frame apear. ive read up on this. is this the correct way to do it.
on the main of my 'main' i put this code:
main theGUI = new main ();
then on my login i put, under the command button action i put:
theGUI.show ();
however i had no luck with this. is this correct or am i doing it wrong
Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
try this:
theGui.setVisible(false);
then, when you want to show it: theGui.setVisible(true);
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Swing / JFC / AWT forum...
 
GeeCON Prague 2014
 
subject: simple login - help asp