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 4 of 4

Thread: need some help with this error

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

    Default need some help with this error

    i just learned GUI applications in my class but im getting an error i dont know why


    package Chapter7;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class RetalPrice extends JFrame {
     
    	int itemCost;
    	double markupPercentage;
    	JTextField answer;
    	JTextField answer2;
    	JButton button;
     
    	public RetalPrice()
    	{
    		int width = 400;
    		int length = 150;
     
    		setTitle("Retail Price Calculator");
    		setSize(width,length);
     
    		JLabel question = new JLabel("What is the item's cost");
    		JLabel question2 = new JLabel("what is the mark up percentage");
    		answer = new JTextField(10);
    		answer2 = new JTextField(10);
    		button = new JButton("calculate");
    		button.addActionListener(new CalcButtonListener()); //Line where im getting the error
    		JPanel panel = new JPanel();
     
    		panel.add(question);
    		panel.add(answer);
    		panel.add(question2);
    		panel.add(answer2);
    		panel.add(button);
    		add(panel);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
     
    	}
     
    	private class CalcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			int price;
    			int tax;
    			double finalPrice;
    			String input;
    			price = Integer.parseInt(answer.getText());
    			tax = Integer.parseInt(answer2.getText());
    			finalPrice = price + price * (tax/100);
     
    			JOptionPane.showMessageDialog(null, "the final price is " + finalPrice);
    		}
    	}
     
    	public static void main (String[]args)
    	{
    		RetalPrice tax = new RetalPrice();
    	}
    }



    the error says The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguement RetalPrice.CalcButtonListener)

    thank you in advance


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: need some help with this error

    I don't know what the problem is for you... I copied that code and tried it and everything worked fine.

    But actually, I did uncover another error. I input 50 for the cost and 50 for the markup percentage, and the result was 50. The problem is in this line:

    finalPrice = price + price * (tax/100);

    particularly the bolded region.

    When working with integers, Java doesn't convert to decimal unless you tell it to. So, for example, if I say

    int num = 7 / 3;

    Java will assign num's value to 2, even though the exact answer isn't 2. To get Java to preserve decimal results, you have to cast an operation to a double. So, I think if you cast tax to a double before you divide it by 100, then the math will be done correctly.

    But back to your first question... Is the code you have above ^ the exact code that you are using? I can't think of any reason that it would work for me and not for you...
    Last edited by snowguy13; January 14th, 2012 at 05:19 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

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

    Default Re: need some help with this error

    mmmm.... i dont know how to fix it then... yeah, its the exact code... maybe is the compiler? im using eclipse 3.6.2.... i copied another code from the book and im still getting the same error in that line
    Last edited by cesaral1510; January 14th, 2012 at 05:21 PM.

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: need some help with this error

    Hmm... I don't really know why that is causing an issue then. I don't know enough about different IDE's and their compilers...

    Perhaps you could ask if Eclipse has certain problems with the addActionListener() method in this forum. Someone's bound to know what the problem is. Sorry I can't help more. D:
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.