By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,938 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.

constructor Book in class Book cannot be applied to this given type[Need help]

P: 18
I do not know how to go further from here, please help.
[constructor Book in class Book cannot be applied to this given type]I attached a picture of the area of concern.thanks


Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.text.*;
  5. import javax.swing.*;
  6. import java.awt.event.*;
  7. import javax.imageio.ImageIO;
  8. import java.awt.image.BufferedImage;
  9. import net.miginfocom.swing.*;
  10.  
  11. public class AddBooks extends JPanel
  12. {
  13.    static JLabel productId, isbn, bookName, author, numberOfPages, yearOfPublication, publishingHouse, cost;
  14.    static JButton back, addNewBook;
  15.    static JTextField productIdTF, isbnTF, bookNameTF, authorTF, numberOfPagesTF, yearOfPublicationTF, publishingHouseTF, costTF;
  16.    static JPanel mainPanel, buttonsPanel;
  17.    static CardLayout cardLayout;     
  18.    private BufferedImage wallpaper;    
  19.  
  20.    @SuppressWarnings("unchecked") //  gets rid of the unchecked or unsafe warning
  21.    public AddBooks(){                    
  22.        final String dir = System.getProperty("user.dir"); 
  23.        Color labelColor = new Color(76,0,153);        
  24.        Color textFieldColor = new Color(255,0,0); 
  25.  
  26.         //------------------------     WALLPAPER    ---------------------//
  27.         try {
  28.           wallpaper = ImageIO.read(new File(dir + "/Images/AddBookBG.png"));
  29.         } catch(IOException e) {
  30.           e.printStackTrace();
  31.         }
  32.         //********************  END OF WALLPAPER    ***********************//
  33.  
  34.         setLayout(new MigLayout(""));
  35.  
  36.         productId = new JLabel("ProductId");
  37.         productId.setFont(new Font("Arial Black", Font.PLAIN, 12));
  38.         productId.setForeground(labelColor);
  39.         productIdTF = new JTextField(20); 
  40.         productIdTF.setForeground(textFieldColor);
  41.         productIdTF.setFont(new Font("Arial Black", Font.PLAIN, 14));
  42.  
  43.         isbn = new JLabel("BOOK ISBN");
  44.         isbn.setFont(new Font("Arial Black", Font.PLAIN, 12));
  45.         isbn.setForeground(labelColor);
  46.         isbnTF = new JTextField(20);  
  47.         isbnTF.setForeground(textFieldColor);
  48.         isbnTF.setFont(new Font("Arial Black", Font.PLAIN, 14));
  49.  
  50.         bookName = new JLabel("BOOK NAME");
  51.         bookName.setFont(new Font("Arial Black", Font.PLAIN, 12));
  52.         bookName.setForeground(labelColor);
  53.         bookNameTF = new JTextField(20);
  54.         bookNameTF.setForeground(textFieldColor);
  55.         bookNameTF.setFont(new Font("Arial Black", Font.PLAIN, 14));
  56.  
  57.         author = new JLabel("BOOK AUTHOR");
  58.         author.setFont(new Font("Arial Black", Font.PLAIN, 12));
  59.         author.setForeground(labelColor);
  60.         authorTF = new JTextField(20);
  61.         authorTF.setForeground(textFieldColor);
  62.         authorTF.setFont(new Font("Arial Black", Font.PLAIN, 14));
  63.  
  64.         numberOfPages = new JLabel("No. OF PAGES");
  65.         numberOfPages.setFont(new Font("Arial Black", Font.PLAIN, 12));
  66.         numberOfPages.setForeground(labelColor);
  67.         numberOfPagesTF = new JTextField(20);
  68.         numberOfPagesTF.setForeground(textFieldColor);
  69.         numberOfPagesTF.setFont(new Font("Arial Black", Font.PLAIN, 14));
  70.  
  71.         yearOfPublication = new JLabel("yearOfPublication");
  72.         yearOfPublication.setFont(new Font("Arial Black", Font.PLAIN, 12));
  73.         yearOfPublication.setForeground(labelColor);
  74.         yearOfPublicationTF = new JTextField(20);
  75.         yearOfPublicationTF.setForeground(textFieldColor);
  76.         yearOfPublicationTF.setFont(new Font("Arial Black", Font.PLAIN, 14));
  77.  
  78.         publishingHouse = new JLabel("PUBLISHED publishingHouse");
  79.         publishingHouse.setFont(new Font("Arial Black", Font.PLAIN, 12));
  80.         publishingHouse.setForeground(labelColor);
  81.         publishingHouseTF = new JTextField(20);
  82.         publishingHouseTF.setForeground(textFieldColor);
  83.         publishingHouseTF.setFont(new Font("Arial Black", Font.PLAIN, 14));
  84.  
  85.         cost = new JLabel("PRICE");
  86.         cost.setFont(new Font("Arial Black", Font.PLAIN, 12));
  87.         cost.setForeground(labelColor);                        
  88.         costTF = new JTextField(20);
  89.         costTF.setForeground(textFieldColor);
  90.         costTF.setFont(new Font("Arial Black", Font.PLAIN, 12));
  91.  
  92.  
  93.         back = new JButton("");
  94.         back.setFont(new Font("Arial Black", Font.PLAIN, 10));
  95.         back.setBorder(BorderFactory.createEmptyBorder()); // remove button border   
  96.         back.setContentAreaFilled(false);                // remove the filled area of the button
  97.         back.setIcon(new ImageIcon(dir + "/Images/back.png"));
  98.         back.setRolloverIcon(new ImageIcon (dir + "/Images/backG.png"));// adding roolover image
  99.         back.setVerticalTextPosition(SwingConstants.BOTTOM);    // Setting button text to buttom of the image
  100.         back.setHorizontalTextPosition(SwingConstants.CENTER);  // centerizing the text of the button
  101.  
  102.         addNewBook = new JButton("");
  103.         addNewBook.setFont(new Font("Arial Black", Font.PLAIN, 12));
  104.         addNewBook.setBorder(BorderFactory.createEmptyBorder()); // remove border from button        
  105.         addNewBook.setContentAreaFilled(false);                // remove the filled are from button
  106.         addNewBook.setIcon(new ImageIcon(dir + "/Images/addBK.png"));
  107.         addNewBook.setRolloverIcon(new ImageIcon (dir + "//Images/addBKG.png"));// adding roolover image
  108.         addNewBook.setVerticalTextPosition(SwingConstants.BOTTOM);    // Setting button text to buttom of the image
  109.         addNewBook.setHorizontalTextPosition(SwingConstants.CENTER);  // centerizing the text of the button
  110.          //------------------ END OF CREATING BUTTONS ------------------\\
  111.  
  112.  
  113.          productIdTF.addKeyListener(new KeyAdapter() {
  114.           public void keyTyped(KeyEvent e) {
  115.               char n = e.getKeyChar();
  116.               if (!((n >= '0') && (n <= '9') ||
  117.                  (n == KeyEvent.VK_BACK_SPACE) ||
  118.                  (n == KeyEvent.VK_DELETE))) {                
  119.                 getToolkit().beep();
  120.                 e.consume();
  121.                 JOptionPane.showMessageDialog(null,"Only numbers allowed!", "Info", JOptionPane.YES_NO_OPTION);
  122.               }
  123.            }
  124.         });
  125.         yearOfPublicationTF.addKeyListener(new KeyAdapter() {
  126.           public void keyTyped(KeyEvent e) {
  127.               char n = e.getKeyChar();
  128.               if (!((n >= '0') && (n <= '9') ||
  129.                  (n == KeyEvent.VK_BACK_SPACE) ||
  130.                  (n == KeyEvent.VK_DELETE))) {
  131.                 getToolkit().beep();
  132.                 e.consume();
  133.                 JOptionPane.showMessageDialog(null,"Only yearOfPublication allowed!\ne.g 1990", "Info", JOptionPane.YES_NO_OPTION);
  134.               }
  135.            }
  136.         });
  137.         costTF.addKeyListener(new KeyAdapter() {
  138.           public void keyTyped(KeyEvent e) {
  139.               char n = e.getKeyChar();
  140.               if (!((n >= '0') && (n <= '9') ||
  141.                  (n == KeyEvent.VK_BACK_SPACE) ||
  142.                  (n == KeyEvent.VK_DELETE))) {
  143.                 getToolkit().beep();
  144.                 e.consume();
  145.                 JOptionPane.showMessageDialog(null,"Only numbers allowed!", "Info", JOptionPane.YES_NO_OPTION);
  146.               }
  147.            }
  148.         });
  149.  
  150.  
  151.         back.addActionListener(new ActionListener(){
  152.             public void actionPerformed(ActionEvent arg0){
  153.                 AlphaApp.cardLayout.show(  AlphaApp.cards, "Main Menu");
  154.             }
  155.         });
  156.  
  157.         addNewBook.addActionListener(new ActionListener(){
  158.             public void actionPerformed(ActionEvent arg0){
  159.                 int productID = Integer.parseInt(productIdTF.getText());    
  160.                 String productName = bookNameTF.getText().toUpperCase().trim();                
  161.                 double productCost = Double.parseDouble(costTF.getText());               
  162.                 int productyearOfPublication = Integer.parseInt(yearOfPublicationTF.getText());                 
  163.                 String productpublishingHouse = publishingHouseTF.getText().toUpperCase().trim();
  164.                 String productAuthor = authorTF.getText().toUpperCase().trim();
  165.                 String Isbn = isbnTF.getText().toUpperCase().trim();
  166.                 String numberOfPages = numberOfPagesTF.getText().toUpperCase().trim();                 
  167.  
  168.                 MainMenu.bookList.add(new Book(
  169.                 productID, productName, cost,yearOfPublication, 
  170.                 publishingHouse, author, Isbn, numberOfPages));                
  171.  
  172.                 productIdTF.setText("");
  173.                 isbnTF.setText("");
  174.                 bookNameTF.setText("");
  175.                 authorTF.setText("");
  176.                 numberOfPagesTF.setText("");
  177.                 yearOfPublicationTF.setText("");
  178.                 publishingHouseTF.setText("");
  179.                 costTF.setText("");
  180.                 JOptionPane.showMessageDialog(null, "NEW BOOK ADDED");      
  181.                 AlphaApp.cardLayout.show(AlphaApp.cards, "Main Menu");
  182.             }
  183.         });
  184.  
  185.  
  186.         //****************** ADDING COMPONENT TO PANEL *****************\\        
  187.         add(productIdTF, " sg label, gapTop 140, split 2");    
  188.         add(productIdTF, "sg TF, wrap");
  189.         add(isbn, " sg label, gapTop 10, split 2");    
  190.         add(isbnTF, "sg TF, wrap");
  191.         add(bookName, " sg label, gapTop 10, split 2");        
  192.         add(bookNameTF, "sg TF, wrap");
  193.         add(author, " sg label, gapTop 10, split 2");        
  194.         add(authorTF, "sg TF, wrap");
  195.         add(numOfPages, " sg label, gapTop 10, split 2");        
  196.         add(numOfPagesTF, "sg TF, wrap");       
  197.         add(publishingHouse, " sg label, gapTop 10, split 2");       
  198.         add(publishingHouseTF, "sg TF, wrap");
  199.         add(yearOfPublication, " sg label, gapTop 10, split 2");       
  200.         add(yearOfPublicationTF, "sg TF, wrap");
  201.         add(cost, " sg label, gapTop 10, split 2"); 
  202.         add(costTF, "sg TF, wrap");
  203.  
  204.         add(back, "center, sg a, gapTop 70, gapRight 60, split 2");   
  205.         add(addNewBook, "sg a, gapTop 70, wrap");                               
  206.     } 
  207.  
  208.      @Override
  209.     protected void paintComponent(Graphics g) {
  210.        super.paintComponent(g);
  211.        // paint the background image and scale it to fill the entire space
  212.        g.drawImage(wallpaper, 0, 0, getWidth(), getHeight(), this);
  213.     }   
  214. }
  215.  
Attached Files
File Type: docx Doc1.docx (245.1 KB, 244 views)
Jul 27 '15 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 785
I am too afraid of virus to open a word document. And I do not have Microsoft office, but Libre Office and maybe that cannot read it in anyway.

Can you please attach a picture, or much quicker: just tell us the line number in code above where the error happens.
Jul 28 '15 #2

P: 18
The error is in line 168
Jul 28 '15 #3

Expert 100+
P: 785
I can't see the bookList object and the MainMenu, but the error says that the "thing" alias "new Book(...)" you have passed into the add() method in "MainMenu.bookList.add(thing)" is not of the correct type. To see the correct type, I need to see the definition of the "bookList" object. Can you please list it? I strongly suspect that you have given a type there which is not a superclass of "Book".
Jul 29 '15 #4

Post your reply

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