By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

Still experiencing error message. PLEASE HELP!

P: 18
ERROR
[method addActionListener in class javax.swing.AbstractButton cannot be applied to giventypes;
requires:java.awt.event.ActionListener found: Bookshop.product
reason: actual argument Bookshop.product cannot be converted to java.awt.event.ActionListener by method invocation conversion]

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Bookshop extends JFrame implements ActionListener
  6. {
  7.  
  8.  JTextField productIdText;
  9.  JTextField productNameText;
  10.  JTextField productCostText;
  11.  JTextField productyearOfPublicationText;
  12.  JTextField productpublishingHouseText;
  13.  
  14.  JButton submit;
  15.  Product [] productList = new Product [100];//productList array to store Person objects
  16.  int numberOfProduct = 0;// counter to know how many objects are in the array
  17.  
  18. public void actionPerformed(ActionEvent e)
  19. {
  20.         if(e.getSource() == submit)// get the data from textfields
  21.         {
  22.             int id =  Integer.parseInt(productIdText.getText());
  23.             String name = productNameText.getText();
  24.             double cost = Double.parseDouble(productCostText.getText());
  25.             int yearOfPublication = Integer.parseInt(productyearOfPublicationText.getText()); 
  26.             String publishingHouse = productpublishingHouseText.getText();
  27.             //System.out.println(id);
  28.             //System.out.println(name);
  29.  
  30.             // put new object into array
  31.             productList[numberOfProduct] = new Product(id,name,cost,yearOfPublication,publishingHouse);
  32.             numberOfProduct++;
  33.         }
  34.  
  35.     }
  36.  
  37. public static void main(String []args)
  38.     { 
  39.        new Bookshop();
  40.     }
  41.  
  42. class Product
  43. {
  44.    private int productId;
  45.    private String productName;
  46.    private double cost;
  47.    private int yearOfPublication;
  48.    private String publishingHouse;
  49.  
  50.  
  51. public Product(int productId,String productName,double cost,int yearOfPublication,String publishingHouse)
  52.     {
  53.        this.productId = productId;
  54.        this.productName = productName;
  55.        this.cost = cost;
  56.        this.yearOfPublication = yearOfPublication;
  57.        this.publishingHouse = publishingHouse;
  58.  
  59.  
  60.             setSize(400,400);
  61.             setLayout(new FlowLayout());
  62.  
  63.             productIdText = new JTextField(5);        
  64.             add(productIdText);
  65.  
  66.             productNameText = new JTextField(5);        
  67.             add(productNameText);
  68.  
  69.             productCostText = new JTextField(5);        
  70.             add(productCostText);
  71.  
  72.             productyearOfPublicationText = new JTextField(5);        
  73.             add(productyearOfPublicationText);
  74.  
  75.             productpublishingHouseText = new JTextField(5);        
  76.             add(productpublishingHouseText);
  77.  
  78.             submit = new JButton("click");
  79.             submit.addActionListener(this);
  80.             add(submit);
  81.  
  82.  
  83.             setVisible(true);
  84.  
  85.     }
  86.  
  87. public void setSize(int x,int y)
  88. {
  89. }
  90.  
  91. public void setproductIdText(int productIdText)
  92.     {
  93.      this.productIdText=productIdText;
  94.     }
  95.  
  96. public void setproductNameText(String productNameText)
  97.     {
  98.       this.productNameText = productNameText;
  99.     }
  100. public void setId(int productId)
  101.    {
  102.      this.productId = productId;
  103.    }
  104.  
  105. public int getproductId()
  106.    {
  107.        return productId;
  108.    }
  109. public void setproductName(String productName)
  110.    {
  111.      this.productName = productName;
  112.    }
  113.  
  114. public String getproductName()
  115.    {
  116.        return productName;
  117.    }
  118.  
  119. public void setcost(double cost)
  120.    {
  121.      this.cost = cost;
  122.    }
  123.  
  124. public double getcost()
  125.    {
  126.        return cost;
  127.    }
  128.  
  129. public void setyearOfPublication(int yearOfPublication)
  130.    {
  131.      this.yearOfPublication = yearOfPublication;
  132.    }
  133.  
  134. public int getyearOfPublication()
  135.    {
  136.        return yearOfPublication;
  137.    }
  138.  
  139. public void setpublishingHouse(String publishingHouse)
  140.    {
  141.      this.publishingHouse = publishingHouse;
  142.    }
  143.  
  144. public String getpublishingHouse()
  145.    {
  146.        return publishingHouse;
  147.    }
  148. }
  149.  
  150. class Book extends Product
  151. {
  152.  private String author;
  153.  private int isbn;
  154.  private int numberOfPages;
  155.  
  156.  
  157. public Book(String author, int isbn, int numberOfPages, int productId, String productName, double cost,
  158.                        int yearOfPublication, String publishingHouse)
  159.     {
  160.      super(productId,productName,cost,yearOfPublication,publishingHouse);      
  161.      this.author = author;
  162.      this.isbn = isbn;
  163.      this.numberOfPages = numberOfPages; 
  164.     }
  165. }
  166.  
  167. class Software extends Product
  168. {
  169.  private int ram;
  170.  private int processor;
  171.  
  172.  
  173.  
  174.  public Software(int ram,int processor,int productId, String productName, double cost,
  175.                   int yearOfPublication, String publishingHouse)
  176.      {
  177.          super(productId,productName,cost,yearOfPublication,publishingHouse);      
  178.          this.ram = ram;
  179.          this.processor = processor; 
  180.      }
  181.  
  182. }
  183.  
  184. }
section marked in the attachment
Thanks
Attached Files
File Type: docx assig.docx (246.4 KB, 254 views)
Jul 13 '15 #1

✓ answered by chaarmann

In line 79 you wrote "submit.addActionListener(this);". Here "this" refers to the current class, which is "Product". This current class does not implement ActionListener interface, but "Bookshop" does. So move the whole screen rendering code to the bookshop class.

There is a design confusion between Bookshop and Product. I mean, your bookshop has one screen and many products. But the products themselves never render their own screen each, they only provide their data to the bookshore which is changing its single screen. This single screen is rendered by the bookshop. The bookshop is responsile for getting the product data and filling in the values.
Or do you want to open a new window (and keep it open) each time the user selects a new product? I mean where the data of different products is so different that no common screen is possible? But then you still need to have a start screen owned by the Bookshop where you can select a product, and I would start to implement that first.

Share this Question
Share on Google+
1 Reply


Expert 100+
P: 785
In line 79 you wrote "submit.addActionListener(this);". Here "this" refers to the current class, which is "Product". This current class does not implement ActionListener interface, but "Bookshop" does. So move the whole screen rendering code to the bookshop class.

There is a design confusion between Bookshop and Product. I mean, your bookshop has one screen and many products. But the products themselves never render their own screen each, they only provide their data to the bookshore which is changing its single screen. This single screen is rendered by the bookshop. The bookshop is responsile for getting the product data and filling in the values.
Or do you want to open a new window (and keep it open) each time the user selects a new product? I mean where the data of different products is so different that no common screen is possible? But then you still need to have a start screen owned by the Bookshop where you can select a product, and I would start to implement that first.
Jul 13 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.