Still experiencing error message. PLEASE HELP!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • babs115
    New Member
    • Jun 2015
    • 18

    Still experiencing error message. PLEASE HELP!

    ERROR
    [method addActionListen er in class javax.swing.Abs tractButton cannot be applied to giventypes;
    requires:java.a wt.event.Action Listener found: Bookshop.produc t
    reason: actual argument Bookshop.produc t cannot be converted to java.awt.event. ActionListener by method invocation conversion]

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Bookshop extends JFrame implements ActionListener
    {
     
     JTextField productIdText;
     JTextField productNameText;
     JTextField productCostText;
     JTextField productyearOfPublicationText;
     JTextField productpublishingHouseText;
    
     JButton submit;
     Product [] productList = new Product [100];//productList array to store Person objects
     int numberOfProduct = 0;// counter to know how many objects are in the array
     
    public void actionPerformed(ActionEvent e)
    {
            if(e.getSource() == submit)// get the data from textfields
            {
                int id =  Integer.parseInt(productIdText.getText());
                String name = productNameText.getText();
                double cost = Double.parseDouble(productCostText.getText());
                int yearOfPublication = Integer.parseInt(productyearOfPublicationText.getText()); 
                String publishingHouse = productpublishingHouseText.getText();
                //System.out.println(id);
                //System.out.println(name);
                
                // put new object into array
                productList[numberOfProduct] = new Product(id,name,cost,yearOfPublication,publishingHouse);
                numberOfProduct++;
            }
        
        }
    
    public static void main(String []args)
        { 
           new Bookshop();
        }
    
    class Product
    {
       private int productId;
       private String productName;
       private double cost;
       private int yearOfPublication;
       private String publishingHouse;
    
    
    public Product(int productId,String productName,double cost,int yearOfPublication,String publishingHouse)
        {
           this.productId = productId;
           this.productName = productName;
           this.cost = cost;
           this.yearOfPublication = yearOfPublication;
           this.publishingHouse = publishingHouse;
           
          
                setSize(400,400);
                setLayout(new FlowLayout());
                
                productIdText = new JTextField(5);        
                add(productIdText);
                
                productNameText = new JTextField(5);        
                add(productNameText);
                
                productCostText = new JTextField(5);        
                add(productCostText);
                
                productyearOfPublicationText = new JTextField(5);        
                add(productyearOfPublicationText);
                
                productpublishingHouseText = new JTextField(5);        
                add(productpublishingHouseText);
                
                submit = new JButton("click");
                submit.addActionListener(this);
                add(submit);
                
            
                setVisible(true);
            
        }
    
    public void setSize(int x,int y)
    {
    }
    
    public void setproductIdText(int productIdText)
        {
         this.productIdText=productIdText;
        }
    
    public void setproductNameText(String productNameText)
        {
          this.productNameText = productNameText;
        }
    public void setId(int productId)
       {
         this.productId = productId;
       }
       
    public int getproductId()
       {
           return productId;
       }
    public void setproductName(String productName)
       {
         this.productName = productName;
       }
       
    public String getproductName()
       {
           return productName;
       }
       
    public void setcost(double cost)
       {
         this.cost = cost;
       }
       
    public double getcost()
       {
           return cost;
       }
       
    public void setyearOfPublication(int yearOfPublication)
       {
         this.yearOfPublication = yearOfPublication;
       }
       
    public int getyearOfPublication()
       {
           return yearOfPublication;
       }
       
    public void setpublishingHouse(String publishingHouse)
       {
         this.publishingHouse = publishingHouse;
       }
       
    public String getpublishingHouse()
       {
           return publishingHouse;
       }
    }
    
    class Book extends Product
    {
     private String author;
     private int isbn;
     private int numberOfPages;
    
    
    public Book(String author, int isbn, int numberOfPages, int productId, String productName, double cost,
                           int yearOfPublication, String publishingHouse)
        {
         super(productId,productName,cost,yearOfPublication,publishingHouse);      
         this.author = author;
         this.isbn = isbn;
         this.numberOfPages = numberOfPages; 
        }
    }
    
    class Software extends Product
    {
     private int ram;
     private int processor;
     
     
     
     public Software(int ram,int processor,int productId, String productName, double cost,
                      int yearOfPublication, String publishingHouse)
         {
             super(productId,productName,cost,yearOfPublication,publishingHouse);      
             this.ram = ram;
             this.processor = processor; 
         }
         
    }
     
    }
    section marked in the attachment
    Thanks
    Attached Files
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    In line 79 you wrote "submit.addActi onListener(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.

    Comment

    Working...