JButton not performing it's Required Function in java

67 views Asked by At

Here's My Code of Currency Converter Program . Earlier Null Pointer Exception Occurred , but it was Rectified . Now there are No Errors in the Program . Program Runs but Convert Button Does not Perform Any Operation . I believe Problem Lies in the ActionPerformed Function , But I'm not being able to Identify it .

 import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;



    public class Trial extends JFrame implements ActionListener{
           public String[] list={"DOLLARS","EUROS ","YEN","POUNDS","RUPEES"};

        JPanel panel1 = new JPanel();

        JPanel panel2 = new JPanel();
        public JComboBox cb,cb1;

        public JTextField tf1;
        public JTextField tf2;
        public JLabel label3;
        public JLabel label4;

        JPanel panel3 = new JPanel();

        public JButton button1;
        public JButton button2;

        public Trial(){
        setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));         
    getContentPane().setLayout(
        new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)
    );


        JLabel label1 = new JLabel("FROM  :");
        label1.setFont(new Font("Serif", Font.BOLD, 15));

        JComboBox cb = new JComboBox();


        cb= new JComboBox(list);
        cb.getSelectedIndex();
        panel1.setBackground(Color.RED);
        panel1.add(label1);
        panel1.add(cb);


        JLabel label2 = new JLabel("TO :");
        label2.setFont(new Font("Serif", Font.BOLD, 15));
        JComboBox cb1 = new JComboBox();
        cb1=new JComboBox(list);
        cb1.getSelectedIndex();
         cb.addActionListener(this);
         cb1.addActionListener(this);    

        panel1.add(label2);
        panel1.add(cb1);
        add(panel1);
        JLabel label3 = new JLabel("\n\nENTER THE AMOUNT  :");
        label3.setFont(new Font("Serif", Font.BOLD, 15));
        JTextField tf1 = new JTextField(15);

    //  label3.setHorizontalAlignment(SwingConstants.CENTER);
    //  label3.setVerticalAlignment(SwingConstants.CENTER);
        panel2.add(label3);
        panel2.add(tf1);    

        JLabel label4 = new JLabel("CONVERTED AMOUNT :");
        label4.setFont(new Font("Serif", Font.BOLD, 15));
        label4.setDisplayedMnemonic(KeyEvent.VK_O);
        JTextField tf2 = new JTextField(15);

        tf1.addActionListener(this);     
        tf2.addActionListener(this);

        panel2.add(label4);
        panel2.add(tf2);
        panel2.setBackground(Color.BLUE);
        add(panel2);

        JButton button1 = new JButton("CONVERT");
        button1.setFont(new Font("Serif", Font.BOLD, 15));
        button1.setMnemonic(KeyEvent.VK_K);
        panel3.add(button1);

        JButton button2 = new JButton("CLEAR ");
        button2.setFont(new Font("Serif", Font.BOLD, 15));
        button2.setMnemonic(KeyEvent.VK_C);

         button1.addActionListener(this);    
         button2.addActionListener(this);    

        panel3.add(button2);
        panel3.setBackground(Color.GREEN);

        add(panel3);      
    }

        public void actionPerformed(ActionEvent e) {
            double a,b=0; 
            Object src = e.getSource();
            if(src.equals(button1)){
            a=Double.valueOf(tf1.getText());
            try{
                if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==1)
                    {b=a*0.89;}
                if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==2)
                    {b=a*124.75;}
                if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==3)
                    {b=a*0.65;}
                if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==4)
                    {b=a*64.08;}
                if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==0)
                    {b=a*1.13;}
                if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==2)
                    {b=a*140.49;}
                if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==3)
                    {b=a*0.74;}
                if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==4)
                    {b=a*71.34;}
                if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==0)
                    {b=a*0.0080;}
                if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==1)
                    {b=a*0.0071;}
                if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==3)
                    {b=a*0.0052;}
                if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==4)
                    {b=a*0.51;}
                if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==0)
                    {b=a*1.53;}
                if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==1)
                    {b=a*1.36;}
                if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==2)
                    {b=a*191.26;}
                if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==4)
                    {b=a*97.88;}
                if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==0)
                    {b=a*0.0156;}
                if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==1)
                {   b=a*0.014;}
                if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==2)
                    {b=a*1.9607;}
                if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==3)
                    {b=a*0.0108;}

                tf2.setText(String.valueOf(b));

            }catch(Exception x){System.out.println("Error");}
            }
            if(src.equals(button2)){
                tf1.setText("0000");
                tf2.setText("0000");
                            }

        }


        }
2

There are 2 answers

3
Reimeus On BEST ANSWER

You're shadowing variable button1. Replace

JButton button1 = new JButton("CONVERT");

with

button1 = new JButton("CONVERT");
1
Masud Parves Bhuiyan On

Use below code:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;


    public class Trial extends JFrame implements ActionListener
    {
           public String[] list={"DOLLARS","EUROS ","YEN","POUNDS","RUPEES"};

        JPanel panel1 = new JPanel();

        JPanel panel2 = new JPanel();
        public JComboBox cb,cb1;

        public JTextField tf1;
        public JTextField tf2;
        public JLabel label3;
        public JLabel label4;

        JPanel panel3 = new JPanel();

        public JButton button1;
        public JButton button2;

        public Trial(){
        setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));         
    getContentPane().setLayout(
        new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)
    );


        JLabel label1 = new JLabel("FROM  :");
        label1.setFont(new Font("Serif", Font.BOLD, 15));

        cb = new JComboBox();


        cb= new JComboBox(list);
        cb.getSelectedIndex();
        panel1.setBackground(Color.RED);
        panel1.add(label1);
        panel1.add(cb);


        JLabel label2 = new JLabel("TO :");
        label2.setFont(new Font("Serif", Font.BOLD, 15));
        cb1 = new JComboBox();
        cb1=new JComboBox(list);
        cb1.getSelectedIndex();
         cb.addActionListener(this);
         cb1.addActionListener(this);    

        panel1.add(label2);
        panel1.add(cb1);
        add(panel1);
        label3 = new JLabel("\n\nENTER THE AMOUNT  :");
        label3.setFont(new Font("Serif", Font.BOLD, 15));
        tf1 = new JTextField(15);

    //  label3.setHorizontalAlignment(SwingConstants.CENTER);
    //  label3.setVerticalAlignment(SwingConstants.CENTER);
        panel2.add(label3);
        panel2.add(tf1);    

        label4 = new JLabel("CONVERTED AMOUNT :");
        label4.setFont(new Font("Serif", Font.BOLD, 15));
        label4.setDisplayedMnemonic(KeyEvent.VK_O);
        tf2 = new JTextField(15);

        tf1.addActionListener(this);     
        tf2.addActionListener(this);

        panel2.add(label4);
        panel2.add(tf2);
        panel2.setBackground(Color.BLUE);
        add(panel2);

        button1 = new JButton("CONVERT");
        button1.setFont(new Font("Serif", Font.BOLD, 15));
        button1.setMnemonic(KeyEvent.VK_K);
        panel3.add(button1);

        button2 = new JButton("CLEAR ");
        button2.setFont(new Font("Serif", Font.BOLD, 15));
        button2.setMnemonic(KeyEvent.VK_C);

         button1.addActionListener(this);    
         button2.addActionListener(this);    

        panel3.add(button2);
        panel3.setBackground(Color.GREEN);

        add(panel3);      
    }

        public void actionPerformed(ActionEvent e) {
            double a,b=0; 
            Object src = e.getSource();
            if(src == button1){
            a=Double.valueOf(tf1.getText());
            try{
                if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==1)
                    {b=a*0.89;}
                if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==2)
                    {b=a*124.75;}
                if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==3)
                    {b=a*0.65;}
                if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==4)
                    {b=a*64.08;}
                if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==0)
                    {b=a*1.13;}
                if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==2)
                    {b=a*140.49;}
                if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==3)
                    {b=a*0.74;}
                if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==4)
                    {b=a*71.34;}
                if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==0)
                    {b=a*0.0080;}
                if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==1)
                    {b=a*0.0071;}
                if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==3)
                    {b=a*0.0052;}
                if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==4)
                    {b=a*0.51;}
                if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==0)
                    {b=a*1.53;}
                if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==1)
                    {b=a*1.36;}
                if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==2)
                    {b=a*191.26;}
                if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==4)
                    {b=a*97.88;}
                if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==0)
                    {b=a*0.0156;}
                if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==1)
                {   b=a*0.014;}
                if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==2)
                    {b=a*1.9607;}
                if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==3)
                    {b=a*0.0108;}

                tf2.setText(String.valueOf(b));

            }catch(Exception x){System.out.println("Error");}
            }
            if(src.equals(button2)){
                tf1.setText("0000");
                tf2.setText("0000");
                            }

        }        
        }

Problem with your code is, there are lot of local variable whose name is identical with instance variable. Those local variables are making instance variables hidden. In constructor you've write statement like JComboBox cb = new JComboBox(); This cb variable is a local variable. The statement panel1.add(cb); adds a local cb variable to panel1. But in actionPerformed when cb.getSelectedIndex()==0 executed it's using the instance variable which is declared public JComboBox cb,cb1;. As this instance variable is never added to gui. The comboBox user is choosing is local vaiable cb not instance variable cb.