Sunday, November 14, 2010

Create an application which will implement the concept of JRadioButton to change the text into TextArea according to the selected Radio options.


/* Program : 10 
   Create an application which will implement the concept of JRadioButton to change the text into TextArea according to the selected Radio options.
   Developed by : Malhar Vora
   Developed on : 5-11-2010
   Development Status : Completed and tested
   Email     : vbmade2000@gmail.com
   WebSite   : http://technojungle.co.cc
*******************************************************************************************************************/
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class P10 extends JFrame implements ActionListener
{

 ButtonGroup rdoGroup=null;
 JRadioButton rdoSenCase=null;
 JRadioButton rdoLowerCase=null;
 JRadioButton rdoUpperCase=null;
 JRadioButton rdoTitleCase=null;
 JRadioButton rdoToggleCase=null;
 JTextField txtData=null;
 
    P10()
 {
   
   rdoSenCase = new JRadioButton("Sentence case");
   rdoLowerCase = new JRadioButton("lowercase");
   rdoUpperCase = new JRadioButton("UPPERCASE");
   rdoTitleCase = new JRadioButton("Title Case");
   rdoToggleCase = new JRadioButton("tOOGLE cASE");
   rdoGroup = new ButtonGroup();
   txtData = new JTextField("Enter your text here");
   
   rdoGroup.add(rdoSenCase);
   rdoGroup.add(rdoLowerCase );
   rdoGroup.add(rdoUpperCase);
   rdoGroup.add(rdoTitleCase);
   rdoGroup.add(rdoToggleCase);
   
   
   rdoSenCase.addActionListener(this); 
   rdoLowerCase.addActionListener(this);
   rdoUpperCase.addActionListener(this);
   rdoTitleCase .addActionListener(this);
   rdoToggleCase.addActionListener(this);
   
   setLayout(null);
   txtData.setBounds(50,50,300,30);
   rdoSenCase.setBounds(50,90,200,30);
   rdoLowerCase.setBounds(50,120,200,30);
   rdoUpperCase.setBounds(50,150,200,30);
   rdoTitleCase.setBounds(50,180,200,30);
   rdoToggleCase.setBounds(50,210,200,30);

   this.getContentPane().add(txtData); 
   this.getContentPane().add(rdoSenCase); 
   this.getContentPane().add(rdoLowerCase); 
   this.getContentPane().add(rdoUpperCase); 
   this.getContentPane().add(rdoTitleCase); 
   this.getContentPane().add(rdoToggleCase); 
      
   setTitle("Program 10 - Developed by Malhar Vora");
   setSize(380,300);
   setVisible(true);
   setDefaultCloseOperation(EXIT_ON_CLOSE);      
 }
  
  
 public void actionPerformed(ActionEvent ae)
 {
   String cmd=ae.getActionCommand();
   if(txtData.getText().equals(""))
   {
    JOptionPane.showMessageDialog(this,"Please enter some text","Error",JOptionPane.ERROR_MESSAGE);
    return;
   }
   
   if(cmd.equals("lowercase"))
   {
    txtData.setText(txtData.getText().toLowerCase());
   }
   else if(cmd.equals("UPPERCASE"))
   {
    txtData.setText(txtData.getText().toUpperCase());
   }
   else if(cmd.equals("Sentence case"))
   {
    String temp="";
    temp = temp + Character.toUpperCase(txtData.getText().charAt(0));
    
    for(int i=1;i<txtData.getText().length();i++)
    {
     temp = temp + Character.toLowerCase(txtData.getText().charAt(i));
    } 
    
    txtData.setText(temp);   
         
   }
   else if(cmd.equals("tOOGLE cASE"))
   {
    String temp="";
        
    for(int i=0;i<txtData.getText().length();i++)
    {
     if(Character.isUpperCase(txtData.getText().charAt(i)))
     {
      temp = temp + Character.toLowerCase(txtData.getText().charAt(i));
     }
     else
     {
      temp = temp + Character.toUpperCase(txtData.getText().charAt(i));
     }
    } 
    txtData.setText(temp); 
   }
   else if(cmd.equals("Title Case"))
   {
     
    StringTokenizer st = new StringTokenizer(txtData.getText()," ");
    String tempToken="";
    txtData.setText("");
    while(st.hasMoreTokens())
    {
      tempToken = st.nextToken(); 
      txtData.setText(txtData.getText() + titleCase(tempToken) + " ");             
    }
     
    
   }
   
   
   
 } 
 
 String titleCase(String str)
 {
    
    String temp="";
    temp = temp + Character.toUpperCase(str.charAt(0));
    
    for(int i=1;i<str.length();i++)
    {
     temp = temp + Character.toLowerCase(str.charAt(i));
    } 
    return temp;
    
 }
 public static void main(String []str)
 {
   P10 p = new P10();
 }  


}




  

No comments:

Post a Comment