Solved

Problem with Java GUI

Posted on 2011-02-26
13
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 40

Expert Comment

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

Author Comment

by:t38
ID: 34988097
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
ID: 34988110
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 47

Expert Comment

by:for_yan
ID: 34988120
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
ID: 34988255
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
ID: 34988279
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
 

Author Comment

by:t38
ID: 34988326
thanks, change the variable to Book where?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34988344
please share the source code of Book class also
0
 

Author Comment

by:t38
ID: 34988358
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
ID: 34988382
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
ID: 34988564
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
ID: 34998009
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
ID: 35001235
thank you for your help, much appreciated.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

732 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