Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JButton.... actionListener.... help!!!!....

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question JButton.... actionListener.... help!!!!....

    my problem is the button doesn't seems listen to the action I set..... pls. help!!!!!
    can you correct my codings....

    import javax.swing.*;
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.BorderFactory;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
     
    public class CurrencyConverter extends Applet implements ActionListener
     
    {	
     
    	JLabel header;
    	JLabel amountlabel;
    	JLabel fromlabel;
    	JLabel exchangeratelabel;
    	JLabel calcamtlabel;
    	JTextField amountfield;
    	JTextField exchangeratefield;
    	JTextField calcamtfield;
    	 Choice fromchoice;
    	 	double[] value={1.00000,43.5241,101.830,4.85908,10.1931,2980.70,29.8823,1.45021,1.45006,5.48783,0.43355,17.9677,5.52844,11.3770,4.40437,4.85908,1.44881,252.415,201.184,117.540,0.58340,1,434.32,0.52105,2.55983,29.8823,45.3683,0.31801,11.7450,5.85440,2.91617,148.510,31,434.3,29.2834,76.4078,22.3162,177.516,123.253,6.49595,0.97210,1.59249,7.85630,0.62392,76.4078};
     
    	JButton calculate;
     
    	public void init()
    	{
    	setBackground(Color.green);
     
    	JPanel panel = new JPanel(new GridBagLayout());
    	GridBagConstraints a = new GridBagConstraints();
     
    		JLabel header = new JLabel("Currency Conversion Calculator");
    		a.gridx = 0;
    		a.gridy = 0;
    		a.insets = new Insets(10,10,10,10);
    		header.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel.add(header,a);
     
    		add(panel, BorderLayout.NORTH);
     
    	JPanel panel1 = new JPanel(new GridBagLayout());
    	GridBagConstraints b = new GridBagConstraints();
     
    		JLabel amountlabel = new JLabel("Amount of Dollars to be Exchange");
    		b.gridx = 1;
    		b.gridy = 1;
    		b.insets = new Insets(10,10,10,10);
    		amountlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel1.add(amountlabel,b);
     
    		JLabel fromlabel = new JLabel("Convert From:");
    		b.gridx = 1;
    		b.gridy = 2;
    		b.insets = new Insets(10,10,10,10);
    		fromlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel1.add(fromlabel,b);
     
    	add(panel1, BorderLayout.WEST);
     
    	JPanel panel4 = new JPanel(new GridBagLayout());
    	GridBagConstraints f = new GridBagConstraints();
     
    		JTextField amountfield = new JTextField(15);
    		f.gridx = 2;
    		f.gridy = 1;
    		f.insets = new Insets(10,10,10,10);
    		amountfield.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel4.add(amountfield,f);
     
    	fromchoice = new Choice();
    	f.gridx = 2;
    	f.gridy = 2;
    	f.insets = new Insets(10,10,10,10);
    	fromlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    	panel4.add(fromchoice,f);
    		fromchoice.addItem("United States  US Dollar          (USD)");
    		fromchoice.addItem("Philippines    Philippine Peso    (PHP)");
    		fromchoice.addItem("Albania        Albanian Lek       (ALL)");
    		fromchoice.addItem("Andorra        Andorran Franc     (ADF)");
    		fromchoice.addItem("Austria        Austrian Schilling (ATS)");
    		fromchoice.addItem("Belarus        Belarusian Ruble   (BYR)");
    		fromchoice.addItem("Belgium        Belgian Franc      (BEF)");
    		fromchoice.addItem("Bosnia         Bosnia Mark        (BAM)");
    		fromchoice.addItem("Bulagria       Bulgarian Lev      (BGN)");
    		fromchoice.addItem("Cyprus         Cypriot Pound      (CYP)");
    		fromchoice.addItem("Czech Republic Czech Koruna       (CZK)");
    		fromchoice.addItem("Denmark        Danish Krone       (DKK)");
    		fromchoice.addItem("Estonia        Estonian Kroon     (EEK)");
    		fromchoice.addItem("Findland       Finnish Markka     (FIM)");
    		fromchoice.addItem("Germany        German Mark        (DEM)");
    		fromchoice.addItem("Greece         Greek Drachma      (GRD)");
    		fromchoice.addItem("Hungary        Hungarian Forint   (HUF)");
    		fromchoice.addItem("Iceland        Iceland Krona      (ISK)");
    		fromchoice.addItem("Ireland        Irish Punt         (IEP)");
    		fromchoice.addItem("Italy          Italian Lira       (ITL)");
    		fromchoice.addItem("Latvia         Latvian Lats       (LVL)");
    		fromchoice.addItem("Lithuania      Lithuanian Litas   (LTL)");
    		fromchoice.addItem("Luxemburg      Luxembourg Franc   (LUF)");
    		fromchoice.addItem("Macedonia      Macedonian Denar   (MKD)");
    		fromchoice.addItem("Malta          Maltese Lira       (MTL)");
    		fromchoice.addItem("Moldova        Moldovan Leu       (MDL)");
    		fromchoice.addItem("Norway         Norwegian Kroner   (NOK)");
    		fromchoice.addItem("Poland         Polish Zloty       (PLN)");
    		fromchoice.addItem("Portugal       Portuguese Escudo  (PTE)");
    		fromchoice.addItem("Romania        Romanian Lei       (ROL)");
    		fromchoice.addItem("Russia         Russian Rouble     (RUB)");
    		fromchoice.addItem("Serbia         Serbian Dinar      (RSD)");
    		fromchoice.addItem("Slovakia       Slovak Koruna      (SKK)");
    		fromchoice.addItem("Slovenia       Slovenian Tolar    (SIT)");
    		fromchoice.addItem("Spain          Spanish Peseta     (ESP)");
    		fromchoice.addItem("Sweden         Swedish Krona      (SEK)");
    		fromchoice.addItem("Switzerland    Swiss Franc        (CHF)");
    		fromchoice.addItem("Turkey         Turkish Lira       (TRY)");
    		fromchoice.addItem("Ukraine        Ukraine Hryvnia    (UAH)");
    		fromchoice.addItem("United Kingdom British Pound      (GBP)");
    		fromchoice.addItem("Yugoslavia     Yugoslav Dinar     (YUN)");
     
    	add(panel4, BorderLayout.NORTH);
     
    	JPanel panel3 = new JPanel(new GridBagLayout());
    	GridBagConstraints d = new GridBagConstraints();
     
    		JLabel exchangeratelabel = new JLabel("Exchange Rate");
    		d.gridx = 1;
    		d.gridy = 4;
    		d.insets = new Insets(10,10,10,10);
    		exchangeratelabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel3.add(exchangeratelabel,d);
     
    		JLabel calcamtlabel = new JLabel("Calculated Amount");
    		d.gridx = 1;
    		d.gridy = 5;
    		d.insets = new Insets(10,10,10,10);
    		calcamtlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));	
    		panel3.add(calcamtlabel,d);
     
    	add(panel3, BorderLayout.EAST);
     
    	JPanel panel5 = new JPanel(new GridBagLayout());
    	GridBagConstraints e = new GridBagConstraints();
     
     
    		JTextField calcamtfield = new JTextField(15);
    		e.gridx = 2;
    		e.gridy = 5;
    		e.insets = new Insets(10,10,10,10);
    		calcamtfield.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel5.add(calcamtfield,e);
     
    		JTextField exchangeratefield = new JTextField(15);
    		e.gridx = 2;
    		e.gridy = 4;
    		e.insets = new Insets(10,10,10,10);
    		exchangeratefield.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		panel5.add(exchangeratefield,e);
     
    	add(panel5, BorderLayout.WEST);
     
     
    		JButton calculate = new JButton("Calculate");
    		c.gridx = 1;
    		c.gridy = 3;
    		c.insets = new Insets(10,10,10,10);
    		calculate.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
    		calculate.addActionListener(this);
    	add(calculate);
     
     
    	}
     
    	calculate.addActionListener(new ActionListener())
    {
    	public void actionPerformed(ActionEvent e)
    	{
    		double cal,total;
     
    		int actval = fromchoice.getSelectedIndex();
    		total = value[actval];
     
    		int af = Integer.parseInt(amountfield.getText());
     
    		cal=af*total;
     
    		exchangeratefield.setText(""+total);		
    		calcamtfield.setText(""+cal);
     
     
    	}
    	});
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Re: JButton.... actionListener.... help!!!!....

    It appears that your class itself doesn't have a method called actionPerformed(ActionEvent e).

    To get it to add the ActionListener defined by the class,

    calculate.addActionListener(this);

    and in the class defined ActionListener(the method actionPerformed),

    have

    if (e.getActionCommand().equals("Calculate"))
    then put what you had.

    However, your code isn't compiling still even after that adjustment. Am fixing it now.

    It can't seem to find a variable called c and neither can the compiler figure out where it came from.

    I got the following code to compile and run successfully, but you still need to uncomment the variable c and definite it so it''ll do what you want. Right now c is commented out so that the program will compile and run successfully.

       import javax.swing.*;
       import java.awt.*;
       import java.applet.*;
       import java.awt.event.*;
       import javax.swing.JPanel;
       import javax.swing.JLabel;
       import javax.swing.JTextField;
       import javax.swing.BorderFactory;
       import java.awt.GridBagLayout;
       import java.awt.GridBagConstraints;
     
        public class CurrencyConverter extends Applet implements ActionListener
     
       {	
     
          JLabel header;
          JLabel amountlabel;
          JLabel fromlabel;
          JLabel exchangeratelabel;
          JLabel calcamtlabel;
          JTextField amountfield;
          JTextField exchangeratefield;
          JTextField calcamtfield;
          Choice fromchoice;
     
          double[] value={1.00000,43.5241,101.830,4.85908,10.1931,2980.70,29.8823,1.45021,1.45006,5.48783,0.43355,17.9677,5.52844,11.3770,4.40437,4.85908,1.44881,252.415,201.184,117.540,0.58340,1,434.32,0.52105,2.55983,29.8823,45.3683,0.31801,11.7450,5.85440,2.91617,148.510,31,434.3,29.2834,76.4078,22.3162,177.516,123.253,6.49595,0.97210,1.59249,7.85630,0.62392,76.4078};
     
          JButton calculate;
     
     
     
           public void init()
          {
             setBackground(Color.green);
     
             JPanel panel = new JPanel(new GridBagLayout());
             GridBagConstraints a = new GridBagConstraints();
     
             JLabel header = new JLabel("Currency Conversion Calculator");
             a.gridx = 0;
             a.gridy = 0;
             a.insets = new Insets(10,10,10,10);
             header.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
             panel.add(header,a);
     
             add(panel, BorderLayout.NORTH);
     
             JPanel panel1 = new JPanel(new GridBagLayout());
             GridBagConstraints b = new GridBagConstraints();
     
             JLabel amountlabel = new JLabel("Amount of Dollars to be Exchange");
             b.gridx = 1;
             b.gridy = 1;
             b.insets = new Insets(10,10,10,10);
             amountlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
             panel1.add(amountlabel,b);
     
             JLabel fromlabel = new JLabel("Convert From:");
             b.gridx = 1;
             b.gridy = 2;
             b.insets = new Insets(10,10,10,10);
             fromlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
             panel1.add(fromlabel,b);
     
             add(panel1, BorderLayout.WEST);
     
             JPanel panel4 = new JPanel(new GridBagLayout());
             GridBagConstraints f = new GridBagConstraints();
     
            amountfield = new JTextField(15);
             f.gridx = 2;
             f.gridy = 1;
             f.insets = new Insets(10,10,10,10);
             amountfield.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
             panel4.add(amountfield,f);
     
             fromchoice = new Choice();
             f.gridx = 2;
             f.gridy = 2;
             f.insets = new Insets(10,10,10,10);
             fromlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
             panel4.add(fromchoice,f);
             fromchoice.addItem("United States  US Dollar          (USD)");
             fromchoice.addItem("Philippines    Philippine Peso    (PHP)");
             fromchoice.addItem("Albania        Albanian Lek       (ALL)");
             fromchoice.addItem("Andorra        Andorran Franc     (ADF)");
             fromchoice.addItem("Austria        Austrian Schilling (ATS)");
             fromchoice.addItem("Belarus        Belarusian Ruble   (BYR)");
             fromchoice.addItem("Belgium        Belgian Franc      (BEF)");
             fromchoice.addItem("Bosnia         Bosnia Mark        (BAM)");
             fromchoice.addItem("Bulagria       Bulgarian Lev      (BGN)");
             fromchoice.addItem("Cyprus         Cypriot Pound      (CYP)");
             fromchoice.addItem("Czech Republic Czech Koruna       (CZK)");
             fromchoice.addItem("Denmark        Danish Krone       (DKK)");
             fromchoice.addItem("Estonia        Estonian Kroon     (EEK)");
             fromchoice.addItem("Findland       Finnish Markka     (FIM)");
             fromchoice.addItem("Germany        German Mark        (DEM)");
             fromchoice.addItem("Greece         Greek Drachma      (GRD)");
             fromchoice.addItem("Hungary        Hungarian Forint   (HUF)");
             fromchoice.addItem("Iceland        Iceland Krona      (ISK)");
             fromchoice.addItem("Ireland        Irish Punt         (IEP)");
             fromchoice.addItem("Italy          Italian Lira       (ITL)");
             fromchoice.addItem("Latvia         Latvian Lats       (LVL)");
             fromchoice.addItem("Lithuania      Lithuanian Litas   (LTL)");
             fromchoice.addItem("Luxemburg      Luxembourg Franc   (LUF)");
             fromchoice.addItem("Macedonia      Macedonian Denar   (MKD)");
             fromchoice.addItem("Malta          Maltese Lira       (MTL)");
             fromchoice.addItem("Moldova        Moldovan Leu       (MDL)");
             fromchoice.addItem("Norway         Norwegian Kroner   (NOK)");
             fromchoice.addItem("Poland         Polish Zloty       (PLN)");
             fromchoice.addItem("Portugal       Portuguese Escudo  (PTE)");
             fromchoice.addItem("Romania        Romanian Lei       (ROL)");
             fromchoice.addItem("Russia         Russian Rouble     (RUB)");
             fromchoice.addItem("Serbia         Serbian Dinar      (RSD)");
             fromchoice.addItem("Slovakia       Slovak Koruna      (SKK)");
             fromchoice.addItem("Slovenia       Slovenian Tolar    (SIT)");
             fromchoice.addItem("Spain          Spanish Peseta     (ESP)");
             fromchoice.addItem("Sweden         Swedish Krona      (SEK)");
             fromchoice.addItem("Switzerland    Swiss Franc        (CHF)");
             fromchoice.addItem("Turkey         Turkish Lira       (TRY)");
             fromchoice.addItem("Ukraine        Ukraine Hryvnia    (UAH)");
             fromchoice.addItem("United Kingdom British Pound      (GBP)");
             fromchoice.addItem("Yugoslavia     Yugoslav Dinar     (YUN)");
     
             add(panel4, BorderLayout.NORTH);
     
             JPanel panel3 = new JPanel(new GridBagLayout());
             GridBagConstraints d = new GridBagConstraints();
     
             JLabel exchangeratelabel = new JLabel("Exchange Rate");
             d.gridx = 1;
             d.gridy = 4;
             d.insets = new Insets(10,10,10,10);
             exchangeratelabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
             panel3.add(exchangeratelabel,d);
     
             JLabel calcamtlabel = new JLabel("Calculated Amount");
             d.gridx = 1;
             d.gridy = 5;
             d.insets = new Insets(10,10,10,10);
             calcamtlabel.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));	
             panel3.add(calcamtlabel,d);
     
             add(panel3, BorderLayout.EAST);
     
             JPanel panel5 = new JPanel(new GridBagLayout());
             GridBagConstraints e = new GridBagConstraints();
     
     
              calcamtfield = new JTextField(15);
             e.gridx = 2;
             e.gridy = 5;
             e.insets = new Insets(10,10,10,10);
             calcamtfield.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
             panel5.add(calcamtfield,e);
     
             exchangeratefield = new JTextField(15);
             e.gridx = 2;
             e.gridy = 4;
             e.insets = new Insets(10,10,10,10);
             exchangeratefield.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
             panel5.add(exchangeratefield,e);
     
             add(panel5, BorderLayout.WEST);
     
     
             JButton calculate = new JButton("Calculate");
          //	c.gridx = 1;
          //	c.gridy = 3;
          //	c.insets = new Insets(10,10,10,10);
             calculate.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD | java.awt.Font.ITALIC, 12));
             calculate.addActionListener(this);
             add(calculate);
             calculate.addActionListener(this);
     
          }
     
     
           public void actionPerformed(ActionEvent e)
          {
             if (e.getActionCommand().equals("Calculate"))
             {
                double cal,total;
     
                int actval = fromchoice.getSelectedIndex();
                total = value[actval];
                         int af = Integer.parseInt(amountfield.getText());
     
                cal=af*total;
     
                exchangeratefield.setText(""+total);		
                calcamtfield.setText(""+cal);
             }
          }
       }
    Last edited by javapenguin; February 26th, 2011 at 03:19 PM.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JButton.... actionListener.... help!!!!....

    thank you so much..... it run now.....

    the c is for the gridbag, but i can fix it......

    i was really frustrated on how to fix it....
    it really help me a lot.....


    thank you very much....

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  2. ActionListener help
    By QBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 12:25 PM
  3. [SOLVED] ActionListener help
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 14th, 2010, 06:57 PM
  4. ActionListener Help?
    By Drag01 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 08:21 PM
  5. Question about ActionListener
    By TimW in forum AWT / Java Swing
    Replies: 6
    Last Post: November 4th, 2009, 11:00 AM