Link to home
Start Free TrialLog in
Avatar of chandanchoubey
chandanchoubey

asked on

How do i display numbers in a textfield

I have made three textfields (txtPhone, txtMobile & txtCount)  and one button (btnAdd). I want third (txtCount) field to display 1 when there is something in either txtPhone or in txtMobile. & if both the fields have something I want txtCount to display 2. its displaying 2 no matter any field has anything or not. what am I doing wrong?

Please check my code. Thanks.
private void formWindowActivated(java.awt.event.WindowEvent evt) {
        // Window activated handling code here:
        
        txtPhone.setText(null);
        txtMobile.setText(null);
    }
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {        
         if((txtPhone.getText() != null) && (txtMobile.getText() !=null))
       {
           txtCount.setText("2");
       }
       else if((txtPhone.getText() != null) || (txtMobile.getText() !=null))
       {
           txtcount.setText("1");
       }
       else
       {
                   txtcount.setText("0");
       }
}

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

it won't be null when empty, it will instead contain an empty string.
check for that instead

Avatar of chandanchoubey
chandanchoubey

ASKER

Thanks for your quick response. But I also guessed that for that I have put this code to make sure it is null when window fires up.
private void formWindowActivated(java.awt.event.WindowEvent evt) {
        // Window activated handling code here:
       
        txtPhone.setText(null);
        txtMobile.setText(null);
    }
don't think that will make a difference

Thanks again. Please suggest me then how do I do this. How do I make sure that the text fields are null when program starts.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent!! It worked thankyou very much!!