Solved

Problem with Java GUI

Posted on 2011-02-26
13
307 Views
Last Modified: 2012-05-11
The code below is for a GUI to interface with a bookshelf class and perform functions such as adding a book etc.  eclipse is giving an error at addbook, any advice on how to resolve this problem please?

import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.ActionListener;

public class BookGUI extends JFrame implements ActionListener

{

      //String addBook="";
      String book = "";
      String title  = "";
      String author  = "";
      String year = "";
      String publisher  = "";
      String cost = "";
      
      private BookShelf bookShelf = new BookShelf();
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;

    //Creates & displays a window of the class FlowLayoutDemo
    public static void main(String[] args)
    {
        BookGUI gui = new BookGUI( );
        gui.setVisible(true);
    }
   
   // public String getTitle()
   // {
    //    return title;
    //}
   
    public void setTitle(String title) //this is relevant
    {
        this.title = title;
    }
    public void Book()
    {
        this.title = title;
        this.author = author;
        this.year = year;
        this.publisher = publisher;
        //this.cost = cost;
    }
   
       
    public BookGUI( )
    {
       
          setSize(WIDTH, HEIGHT);
        addWindowListener(new WindowDestroyer( ));
        setTitle("GUI Assignment");
        Container content = getContentPane( );

        content.setLayout(new FlowLayout());
             
        JButton button1 = new JButton("Title");
        content.add(button1);
        button1.addActionListener(this);
        //contentPane.add(button1);
       
        JButton button2 = new JButton("Author");
        content.add(button2);
        button2.addActionListener(this);
       
        JButton button3 = new JButton("Publisher");
        content.add(button3);
        button3.addActionListener(this);
       
        JButton button4 = new JButton("Add Book");
        content.add(button4);
        button4.addActionListener(this);    
       
       
    }
   
   
   
   
    public void actionPerformed(ActionEvent e)
    {
       
        if (e.getActionCommand().equals("Add Book"))
               book = JOptionPane.showInputDialog("Add Book");
             //set up the book object with all the data passed in
             
       
              bookShelf.addBook(book);
           
             
              
        // set up each individual title, author, etc.  use joptionpane and mutators?
       //exception handling...
        // add the book to the bookShelf
        //
       
        if (e.getActionCommand().equals("Author"))
            author = JOptionPane.showInputDialog("Enter Author");
       
        else if (e.getActionCommand().equals("Publisher"))
            publisher = JOptionPane.showInputDialog("Enter Publisher");
       
        else
              System.out.println("Error!");
       
        //String message =  "The title of the book is :" + title +
            //"the Author of the Book is : " + author + " and it's published by " + publisher;
       
        //JOptionPane.showMessageDialog(null, message, "Book Details", JOptionPane.PLAIN_MESSAGE);
       
        Container content = getContentPane( );

       
        //label2 = new JButton("Get Book Information");
        //content.add(label2);

        //JButton label3 = new JButton("Show Total Cost of Books");
        //content.add(label3);
   
    }
   
   
   
   
   
}





0
Comment
Question by:t38
  • 7
  • 5
13 Comments
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
<<eclipse is giving an error at addbook>>
Please share the exception stack trace
0
 

Author Comment

by:t38
Comment Utility
This is the error Eclipse is returning.  The method addBook(Book) in the type BookShelf is not applicable for the arguments (String).

I should add that i have seperate classes for book and bookshelf

0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
can you share the code for Bookshelf?

apparently in this code
bookShelf.addBook(book);

book is not of type String which the method 'addBook' is expecting
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
And also I don't think I see the code for the BookShelf class - if the exception happens inside its method
that is probably the first place to look - post it (of couesr stck trcae is most important).
0
 

Author Comment

by:t38
Comment Utility
Here's the bookshelf class - thanks.

import java.util.ArrayList;

/**
  * This class holds an ArrayList of Book.  There are methods for adding Book objects to the ArrayList,
  * calculating the total cost of all books in the ArrayList, determining how many Book objects are
  * in the ArrayList and determining the highest price paid for any one Book.
  *
  * @author  Siobhan Drohan
  * @version 1.0
  */

public class BookShelf
{
    //create an ArrayList of Book.
    private ArrayList<Book> books;
   
    public BookShelf()
    {    
       books = new ArrayList<Book>();
    }

    /**
     * This method adds a Book object to the ArrayList
     *
     * @param theBook The book object that will be added to the ArrayList
     *
     */
    public void addBook(Book theBook)
    {
        books.add(theBook);
    }
   
    /**
     * This method returns the size of the ArrayList, that is, the number of books
     * that have been added to the ArrayList
     *
     * @return The size of the ArrayList
     */
    public int sizeOfBookshelf()
    {
        return books.size();
    }
   
    /**
     * This method calculates the cost of the book shelf, that is, it totals the
     * cost of all the books in the ArrayList and returns it.
     *
     * @return The cost of the book shelf
     */
    public double costOfBookshelf(){
       
        double total = 0;
       
        for(Book book : books) {
            total = total + book.getCost();
        }
        return total;
    }
       
   
    //create a method called highestPricePAid that will return the cost of the most expensive book in the ArrayList

    /**
     * This method finds the price of the most expensive book in the ArrayList and returns it.
     *
     * @return The cost of the most expensive book on the book shelf
     */
    public double highestPricePaid(){
       
       double highestPrice = 0;
       
       for(Book book : books) {
                     
            if((highestPrice == 0) || (highestPrice < book.getCost())) {
                highestPrice = book.getCost(); }
       }
       
       return highestPrice;
    }
         
}
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
book variable that you have declared is of type String, but Bookshelf class method addBook is expecting Book class variable as argument. So you need to either
--change the variable to Book
-- change the argument to String in BookShelf class
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:t38
Comment Utility
thanks, change the variable to Book where?
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
please share the source code of Book class also
0
 

Author Comment

by:t38
Comment Utility
here you go, thanks.

import java.text.*;

/**
  * This class defines an object, Book.  
  *
  * There are methods for adding Book objects to the ArrayList,
  * calculating the total cost of all books in the ArrayList, determining how many Book objects are
  * in the ArrayList and determining the highest price paid for any one Book.
  *
  * @
  * @version 1.0
  */

public class Book
{
    // Instance variables
    private String title, author, publisher;
    private int year;
    private double cost;

    /**
     * Book constructor that initialises the instance variables based on user input.
     *
     * @param title     The title of the book
     * @param author    The author of the book
     * @param year      The year the book was published
     * @param publisher The name of the publisher of the book
     * @param cost      The cost of the book
     */
    public Book(String title, String author, int year, String publisher, double cost)
    {
        this.title = title;
        this.author = author;
        this.year = year;
        this.publisher = publisher;
        this.cost = cost;
    }
   
    /**
     * Accessor for the title of the book.
     * @return the title of the book.
     */
    public String getTitle()
    {
        return title;
    }
   
    /**
     * Accessor for the author of the book.
     * @return the author of the book.
     */
    public String getAuthor()
    {
        return author;
    }
   
    /**
     * Accessor for the year the book was published.
     * @return the year the book was published.
     */    
    public int getYear()
    {
        return year;
    }
   
    /**
     * Accessor for the publisher of the book.
     * @return the publisher of the book.
     */    
    public String getPublisher()
    {
        return publisher;
    }
   
    /**
     * Accessor for the cost of the book.
     * @return the cost of the book.
     */    
    public double getCost()
    {
        return cost;
    }
   
    /**
     * Mutator for updating the title of the book.
     * @param title The new title of the book.
     */
    public void setTitle(String title)
    {
        this.title = title;
    }
   
    /**
     * Mutator for updating the author of the book.
     * @param author The new author of the book.
     */
    public void setAuthor(String author)
    {
        this.author = author;
    }
   
    /**
     * Mutator for updating the year of the book.
     * @param year The new year of the book.
     */
    public void setYear(int year)
    {
        this.year = year;
    }
   
    /**
     * Mutator for updating the publisher of the book.
     * @param publisher The new publisher of the book.
     */    
    public void setPublisher(String publisher)
    {
        this.publisher = publisher;
    }
   
    /**
     * Mutator for updating the cost of the book.
     * @param cost The new cost of the book.
     */    
    public void setCost(double cost)
    {
        this.cost = cost;
    }
   
   
    /**
     * The standard toString method that returns the details of the book
     * @return the details of the book formatted as a String.
     */
    public String toString()
    {
        return "The details of the book are: " + title +
               ", " + author + ", " + year + ", " + publisher + ", " + cost;
    }

}

0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
Comment Utility
replace the
String book = "";
with
Book book = new Book(null, null, -1, null, -1);

and add this lines before bookShelf.addBook(book);

String bookTitle = JOptionPane.showInputDialog("Add Book");
book.setTitle( bookTitle )
0
 

Author Comment

by:t38
Comment Utility
thank you, i'm not able to access eclipse at the moment but will do so asap and report back. thanks for all your help
0
 

Author Comment

by:t38
Comment Utility
Gurvinder 372 - is the code you suggested just working around the problem and no using the addbook method at all?  i'll have to do a book.set for Author, Cost etc. as well?
0
 

Author Closing Comment

by:t38
Comment Utility
thank you for your help, much appreciated.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now