everything is working fine except Encryption button.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class encryption extends JPanel implements ActionListener {

   char [] str= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
   char [] str1={'1','2','3','4','5','6','7','8','9','0','!','@','#','$','%','^','&','*','(','_','_','+','=',',','.','?'};
   char [] a= new char [50];
   char [] b= new char [50];
   JLabel pt, et,dt;
   String k="";
    String l="";

   JTextField pf,ef,df;
   JButton encbutt, clebutt,decbutt,exibutt;





   public encryption(){

       pt=new JLabel("Plain Text");
          et=new JLabel("Encrypted Text");
          dt=new JLabel("Decrypted Text");

         pf= new JTextField(20);

        ef= new JTextField(20);

        df= new JTextField(20);

         encbutt= new JButton("Encryption");

        clebutt= new JButton("Clear");

          decbutt= new JButton("Decryption");

         exibutt= new JButton("Exit");

         ef.setEditable(false);
   df.setEditable(false);
     decbutt.setEnabled(false);

      setLayout(null);
       add(pt);
                add(et);
                add(dt);
                add(pf);
                add(ef);
                add(df);
                add(encbutt);
                add(clebutt);
                add(decbutt);

                add(exibutt);


      pt.setBounds(20, 20, 200,30);

        et.setBounds(20,80,200,30);

         dt.setBounds(20,140, 200,30);

          pf.setBounds(220,20,200,30);

           ef.setBounds(220,80,200,30);

            df.setBounds(220,140,200,30);

             encbutt.setBounds(30,200,160,30);

              clebutt.setBounds(30,240,160,30);

               decbutt.setBounds(240,200,160,30);

                exibutt.setBounds(240,240,160,30);


                decbutt.addActionListener(this);



                clebutt.addActionListener(this);

                exibutt.addActionListener(this);







   }



public void actionPerformed(ActionEvent ae)
 {
    String action=ae.getActionCommand();
    if(ae.equals("Encryption"))
    {
         k=pf.getText().toString().toLowerCase();
        if(k.equals(""))
        {
            pf.setFocusable(true);
            JOptionPane.showMessageDialog(this, "Please Enter Any text !", "Warning", JOptionPane.WARNING_MESSAGE);

        }
 else
        {
        decbutt.setEnabled(true);
        encbutt.setEnabled(false);


        a=k.toCharArray();
        for(int i=0;i<k.length();i++)
        {
            for(int j=0;j<26;j++)
            {
             if(a[i]==str[j])
             {
                 a[i]=str1[j];

             }
            }

        }
        for(int i=0;i<k.length();i++)
        {
          l=l+a[i] ;
        }
        ef.setText(l);


       // JOptionPane.showMessageDialog(bencrypt,l.toString(),"Message",JOptionPane.INFORMATION_MESSAGE);

    }
  }
 else if(action.equals("Decryption"))
    {
  String x=ef.getText().toString();
  String y="";
  if(x.equals(""))
  {
      JOptionPane.showMessageDialog(this,"No Encrypted Message","Warning",JOptionPane.WARNING_MESSAGE);
  }
 else
  {
  b=x.toCharArray();
   //JOptionPane.showMessageDialog(this,k.toString(),"Warning",JOptionPane.WARNING_MESSAGE);
  for(int i=0;i<x.length();i++)
  {
      for(int j=0;j<26;j++)
      {
      if(b[i]==str1[j])
      {
          b[i]=str[j];
          j=26;
      }
      }
  }
  for( int i=0;i<x.length();i++)
  {
      y=y+b[i];
  }
  df.setText(y);
  y="";
 }
 }
 else if(action.equals("Clear"))
    {

     pf.setText("");
     ef.setText("");
     df.setText("");
     pf.setFocusable(true);
     decbutt.setEnabled(false);
     encbutt.setEnabled(true);

    }
 else if(action.equals("Exit"))
    {
     System.exit(0);
 }
}

public static void main(String[] args) {


       JFrame n=new JFrame();
        n.getContentPane().add(new encryption());
        n.setSize(440,310);
        n.setVisible(true);
        n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}


}

not working fine ...

can you specify this a bit ? Does it run ? Does it run the code for that button or not ? Does it produce different output ?
What do you expect ? What do you get ?

Have you tried debugging it ?
Is the code you've written according to your requirements ? What are your requirements ?

when i type something and click at Encryption button. I don't get encrypted text.other buttons are working fine except this one.

shouldnt line 108 be
if(action.equals("Encryption")) instead of if(ae.equals("Encryption"))

and have you tried debugging your code ?
"I don't get encrypted text". do you get other output, does it run that code at all ? there still are to many questions you haven't looked into yet.

As i said before all buttons are working fine (clear, decrypted, exit)except encrypted button and when i debug i don't get any error.

That's still a useless description of your problem.

But anyway - if you were to add an action listener to that button it may help.

Edited 1 Year Ago by JamesCherrill

Comments
nice catch

ps
That style of having one giant actionPerformed is a horrible hard-to-read error-prone hang-over from the Java of 10 years ago. In 2015 it’s far better to use the facilities of modern Java and just have one method for each button, linked by a trivial lambda, e.g.

JButton buttonA, buttonB;
…

buttonA.addActionListener(this::buttonAClicked);
buttonB.addActionListener(this::buttonBClicked);

...

void buttonAClicked(ActionEvent e) {
    // handle click on button A
}

void buttonBClicked(ActionEvent e) {
    // handle click on button B
}

(the method names can be anything you like)

Edited 1 Year Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.