Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Simple Java Problem

Options
  • 07-02-2005 6:27pm
    #1
    Closed Accounts Posts: 32


    I have created a Jtextfield in a Jframe.
    I want it to act like a login.
    I want to enter my username, press return, have that stored as a variable and then enter my password into the same box and press return for that also.

    The action listener reads in the first 1 ok but won't read in the second
    entry

    Is there a command to like refresh the actionlistener for the next entry or what?

    Anything would be useful


Comments

  • Closed Accounts Posts: 324 ✭✭madramor


    gpsstudent wrote:
    The action listener reads in the first 1 ok but won't read in the second
    entry
    Is there a command to like refresh the actionlistener for the next entry or what?
    Anything would be useful

    actionlistner is always listening for the button to be pressed,
    provided you added the actionlistener to the button.

    it is a problem with your coding


  • Closed Accounts Posts: 32 gpsstudent


    Its a textfield, i don't want to use buttons!
    By pressing enter i enact the actionlistener
    private void barcodeField_actionPerformed(ActionEvent e)
    	{
    		
    		//assigns login to temporary variable for security purposes
    		logintmp=barcodeField.getText().trim();
    		//resets it to blank
    		barcodeField.setText(null);
    		//remove
    		//JTextField barcodeField);
    				
    		//changes the text in the instruction box
    		instructionArea.setText("Now enter your password!");
    		//barcodeField_actionPerformed(e);
    		passwordtmp=barcodeField.getText().trim();
    		
    		
    		
    		if((passwordtmp.equals(password))&&(logintmp.equals(login)))
    			instructionArea.setText("Login Successful");
    		
    		else
    			instructionArea.setText("Incorrect Username/Password!\n" +
    					"Hit Login to try again!");
    		
    				
    		
    	}
    


  • Registered Users Posts: 2,320 ✭✭✭Q_Ball


    as far as i can see it wont work because the actionevent is in the process of being invoked. if
    //changes the text in the instruction box
    		instructionArea.setText("Now enter your password!");
    		//barcodeField_actionPerformed(e);
    		passwordtmp=barcodeField.getText().trim();
    

    were outside the barcodeField_actionPerformed method i think it would work fine.

    Is it a prerequisite to use just one box? Personally i'd use two. The code for a password type textfield below :
    When the user hits RETURN, the text field fires an action event. 
        JPasswordField textfield = new JPasswordField("Initial Text");
        textfield.setEchoChar('*');
        textfield.addActionListener(actionListener);
    

    amazing what a little google can do ;)

    [edit]
    also
    http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JPasswordField.html
    [/edit]


  • Closed Accounts Posts: 324 ✭✭madramor


    the action listener runs in its own thread and is always waiting for the action
    you should split your code in 2 and have a variables to say if its the first
    or second call of the "return" action event.
    and if tmp = 1 process first part
    else if tmp = 2 process second part


Advertisement