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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Simple Java Problem

  • 07-02-2005 5: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, Registered Users 2 Posts: 2,325 ✭✭✭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