Table of Contents hide
Example : How to change the password default symbol/character in Password field in Java                         using Net Beans IDE 8.2 .
public UserRegistrion() {
        initComponents();
        jPasswordfield1.setEchoChar('#');
}      
Example : How to display decrypt form of password character of Password field in Java                               using Net Beans IDE 8.2 .
import static javax.swing.JOptionPane.*;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String pass = new String(jPasswordField1.getPassword());
        showMessageDialog(null,pass);
        jTextField2.setText(pass);
    }
Example : How to display encrypt and decrypt letter of a Password field in Java on pressing                      a button using Net Beans IDE 8.2 .
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{
  if(jButton1.getText().equals("Show"))
    {
    JPasswordField1. setEchoChar((char)0)   // Encrypt the letter
    jButton1. setText("Hide")
    }
  else
    {
    JPasswordField1. setEchoChar('#')       // Decrypt the letter
    jButton1. setText("Show")
    }
}
Example : How to display customize decimal location of Float values in jTextField/text box in java using Net Beans IDE 8.2 .
import java.text.DecimalFormat;
import static javax.swing.JOptionPane.*;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {   

        //DecimalFormat x1= new DecimalFormat("Rs 0.0000");  //Customize format of decimal location
        DecimalFormat x1= new DecimalFormat("0.0000");
        DecimalFormat x2= new DecimalFormat("0.00");
        double y= 40.42982566364;
        //double y  = Double.parseDouble(jTextField1.getText());

        jTextField2.setText(x1.format(y));
        jTextField3.setText(x2.format(y));
}
Output :
//Rs 40.4298
40.4298
40.42
Example : How to accept Integer values in jTextField/text box in java using Net Beans IDE 8.2 .
import static javax.swing.JOptionPane.*;

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {   
        int x1 =Integer.parseInt(jTextField1.getText());
        showMessageDialog(null,x1);

        int x2 =Integer.parseInt(jTextField2.getText());
        showMessageDialog(null,x2);

        int x3 = x1+x2;       
        String x4 = Integer.toString(x3);
        //showMessageDialog(null,"Total Sum Value is = "+x4,"Calculation Result",
            INFORMATION_MESSAGE);

        jTextField3.setText(String.valueOf(x3));
        jTextField3.setText(String.valueOf(x4));       
    }
Example : How to accept Float values in jTextField/text box in java using Net Beans IDE 8.2 .
import static javax.swing.JOptionPane.*;

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
       float num1, num2, result;
       num1 = Float.parseFloat(jTextField1.getText());  //To convert String into Float
       num2 = Float.parseFloat(jTextField2.getText());
       result= num1+num2;
       showMessageDialog(null,result);
       jTextField3.setText(String.valueOf(result));     //To convert Float into String
    }
Example : How to enter only numbers in java text box using Net Beans IDE 8.2 .
private void jTxtRegMobileKeyTyped(java.awt.event.KeyEvent evt) 
{                                       
       char x = evt.getKeyChar();
       if(!(Character.isDigit(x)||(x==KeyEvent.VK_BACK_SPACE)||(x==KeyEvent.VK_DELETE)))
        {
            getToolkit().beep();
            evt.consume();
        }
}
-------------  OR  -------------
private void jTxtRegMobileKeyTyped(java.awt.event.KeyEvent evt) 
{                                       
       char x = evt.getKeyChar();
        if(Character.isLetter(x))
        {
            getToolkit().beep();
            evt.consume();
        }
}
Example : How to enter only Text/Letters with space in java text box using Net Beans IDE 8.2 .
private void jTxtRegNameKeyTyped(java.awt.event.KeyEvent evt) {                                     
        // TODO add your handling code here:
        char x= evt.getKeyChar();
        if (!(Character.isAlphabetic(x)||(x==KeyEvent.VK_SPACE)||(x == KeyEvent.VK_DELETE))){
            getToolkit().beep();
            evt.consume();
        }            
    }
Example : How to enter only Text/Letters without space in java text box using Net Beans IDE 8.2 .
private void jTxtRegEmailKeyTyped(java.awt.event.KeyEvent evt) {                                      
        // TODO add your handling code here:
        char x =evt.getKeyChar();
        if(!Character.isLetter(x))
        {
            getToolkit().beep();
            evt.consume();
        }            
    }
Example : How to Uppercase/Capitalize all letters in a jTextfield/Text box in java .
private void jTxtRegNameKeyTyped(java.awt.event.KeyEvent evt) 
{                                     
        // TODO add your handling code here:
        char x= evt.getKeyChar();
        if(Character.isUpperCase(x))
        {
            evt.setKeyChar(Character.toLowerCase(x));
        }        
}

---------  OR  ----------
private void jTxtRegNameKeyReleased(java.awt.event.KeyEvent evt) 
{                                        
        // TODO add your handling code here:        
        String UC = jTxtRegName.getText().toUpperCase();
        jTxtRegName.setText(UC);
}
Example : How to show Small/Lowercase all letters in a jTextfield/text box in java .
private void jTxtRegNameKeyTyped(java.awt.event.KeyEvent evt) 
{                                     
        // TODO add your handling code here:
        char x= evt.getKeyChar();
        if(Character.isLowerCase(x))
          {
           evt.setKeyChar(Character.toUpperCase(x));
          }        
}
Example : How to make a jTextfield/text box read only  in java .
public registration() 
{
    initComponents();
    
    jTxtRegSlno.setEditable(false);     // Readonly
}
Example : How to make a jTextfield/text box to accept upto ten digit values only  in java .
private void jTxtRegNameKeyTyped(java.awt.event.KeyEvent evt) 
{                                     
        // TODO add your handling code here:
        if(jTextfield1.getText().length()>=10)
           evt.consume();      
}
Example : How to make a jTextfield/text box visible/invisible in java .
public registration() 
{
     initComponents();    
        
     jTxtRegSlno.setVisible(false);   //Invisible text box
     //jTxtRegSlno.setVisible(true);    //Visible text box
}
Example : How to focus a cursor in jTextfield/text box in java .
private void jTxtRegNameKeyPressed(java.awt.event.KeyEvent evt) 
{                                       
        // TODO add your handling code here:
        int x = evt.getKeyCode();
        if (x==10)
        {
            jTxtRegFname.requestFocus();
            //jTxtRegFname.grabFocus();            
        }
}
Example : How to Enable/Disable a jTextfield/text box in java .
public registration() 
{
     initComponents();    
        
     jTxtRegSlno.setEnabled(true);     // To make active/enable the text box.
     jTxtRegSlno.setEnabled(false);   //  To make inactive/disable the box.
}
Example : How to convert String into Integer and Vice-versa in java .
import static javax.swing.JOptionPane.*;

public NumConverion() 
{
        initComponents();
        
        int x1 =Integer.parseInt("455");    //Conversion of String into Integer.
        int x2 =Integer.parseInt("145");

        int x3 = x1+x2;
        showMessageDialog(null,x1);
        showMessageDialog(null,x2);

        String x4 = Integer.toString(x3);    //Conversion of Integer into String.
        showMessageDialog(null,"Total Value "+x1+"+"+x2+" is "+x4,"Result",INFORMATION_MESSAGE);        
}
Example : How to convert numeric value into Words in java .
import static javax.swing.JOptionPane.*;
import java.text.DecimalFormat;

public class Num2Word extends javax.swing.JFrame 
{
    private static String input;
    private static int num;
    private static String[] units={"" , " One" , " Two" , " Three" , " Four" , " Five" , " Six" , " 
       Seven" , " Eight" , " Nine" };
    private static String[] teen={" Ten" , " Eleven" , " Twelve" , " Thirteen" , " Fourteen" , " 
       Fifteen" , " Sixteen" , " Seventeen" , " Eighteen" , " Nineteen" };
    private static String[] tens={" Twenty" , " Thirty" , " Forty" , " Fifty" , " Sixty" , " Seventy" 
       , " Eighty" , " Ninety" };
    private static String[] maxs={"" , "" , " Hundred" , " Thousand" , " Lakh" , " Crore" };

    public String convertNumberToWords(int n)
    {
        input=numToString(n);
        String converted="";
        int pos=1;
        boolean hun=false;
        while(input.length()> 0)
        {
            if(pos==1)  // TENS AND UNIT POSITION
            {   if(input.length()>= 2) // TWO DIGIT NUMBERS
                  {
                    String temp=input.substring(input.length()-2,input.length());
                    input=input.substring(0,input.length()-2);
                    //converted+=digits(temp);
                    converted =  converted + digits(temp);
                  }
                else if(input.length()==1) // 1 DIGIT NUMBER
                  {
                    converted+=digits(input);
                    input="";
                  }
                pos++;
            }
            else if(pos==2) // HUNDRED POSITION
            {
                String temp=input.substring(input.length()-1,input.length());
                input=input.substring(0,input.length()-1);
                if(converted.length()> 0&&digits(temp)!="")
                {
                    converted=(digits(temp)+maxs[pos]+" and")+converted;
                    hun=true;
                }
                else
                {
                    if
                      (digits(temp)=="");
                    else
                    converted=(digits(temp)+maxs[pos])+converted;hun=true;
                }
                pos++;
            }
            else if(pos > 2) // REMAINING NUMBERS PAIRED BY TWO
            {
                if(input.length()>= 2) // EXTRACT 2 DIGITS
                {
                  String temp=input.substring(input.length()-2,input.length());
                  input=input.substring(0,input.length()-2);
                    if(!hun&&converted.length()> 0)
                        converted=digits(temp)+maxs[pos]+" and"+converted;
                    else
                     {
                        if(digits(temp)=="");
                        else
                        converted=digits(temp)+maxs[pos]+converted;
                     }
                 }
                 else if(input.length()==1) // EXTRACT 1 DIGIT
                 {
                    if(!hun&&converted.length()> 0)
                       converted=digits(input)+maxs[pos]+" and"+converted;
                    else
                    {
                        if(digits(input)=="")  ;
                        else
                          converted=digits(input)+maxs[pos]+converted;
                          input="";
                    }
                 }
                 pos++;
             }
        }
        return converted;
    }

    private String digits(String temp)    // TO RETURN SELECTED NUMBERS IN WORDS
    {
        String converted="";
        for(int i=temp.length()-1;i >= 0;i--)
         {  
            int ch=temp.charAt(i)-48;
            if(i==0&&ch>1 && temp.length()> 1)
                converted=tens[ch-2]+converted; // IF TENS DIGIT STARTS WITH 2 OR MORE IT FALLS UNDER 
                   TENS
            else if(i==0&&ch==1&&temp.length()==2) // IF TENS DIGIT STARTS WITH 1 IT FALLS UNDER 
                   TEENS
               {
                int sum=0;
                for(int j=0;j < 2;j++)
                sum=(sum*10)+(temp.charAt(j)-48);
                return teen[sum-10];
                }
            else
              {
                if(ch > 0)
                converted=units[ch]+converted;
              } // IF SINGLE DIGIT PROVIDED
          }
        return converted;
     }

    private String numToString(int x) // CONVERT THE NUMBER TO STRING
    {
        String num="";
        while(x!=0)
        {
            num=((char)((x%10)+48))+num;
            x/=10;
        }
        return num;
    }

    private void inputNumber()
    {
        try
        {
              num=Integer.parseInt(jTextField1.getText()); /From textbox/
              //  num=Integer.parseInt(jLabel2.getText());  /From static value from jlabel1/
        }
        catch(Exception e)
        { 
            JOptionPane.showMessageDialog(null, "Number should be Less than 1 Arab ");
            System.exit(1);
        }
    }        



private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
        // TODO add your handling code here:
        Num2Word obj=new Num2Word();  //change the classname NumberToWord from extends class name 
             where this code is inserted

        inputNumber();
        jLabel1.setText("Total Rs. = : " +obj.convertNumberToWords(num)+" Only");
}

Loading

Categories: Java Project

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.