Skip to content

Instantly share code, notes, and snippets.

@Mahkul
Created April 9, 2011 14:34
Show Gist options
  • Save Mahkul/911431 to your computer and use it in GitHub Desktop.
Save Mahkul/911431 to your computer and use it in GitHub Desktop.
Pawel's BookGUI
package pawel.bookshelf;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* This class creates the GUI for the BooShelf. There are methods for creating main frame,
* top menu, add book dialog box, saving book data to the bookshelf.
*
* @author Pawel Makulski
* @version 1.0 Beta
*/
public class BookGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private BookShelf bookList;
private JFrame mainFrame;
private JDialog bookDialog;
private JLabel authorLabel, costLabel, titleLabel, publisherLabel, yearLabel, welcomeLabel;
private JTextField authorTF, costTF, titleTF, publisherTF, yearTF;
private JButton saveButton, cancelButton, okButton, clearButton;
public static void main ( String[] args ) {
new BookGUI();
}
/**
* GUI constructor that creates main frame and book varialbes (by calling their methods) and also initiates bookList.
*/
public BookGUI() {
createMainFrame();
createBookVariables();
bookList = new BookShelf();
}
/**
* This method creates the mainFrame of type JFrame and its content pane.
* It also creates and displays the label containing a brief description of the program.
*
*/
public void createMainFrame() {
mainFrame = new JFrame(("BookShelfGUI"));
mainFrame.setTitle("Pawel's Bookshelf Manager");
mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
mainFrame.setSize(600, 150);
mainFrame.setResizable(true);
Container mainContentPane = mainFrame.getContentPane();
JLabel welcomeLabel = new JLabel("Welcome to Pawel's Bookshelf manager. Please use the options in the top menu to operate it.", JLabel.CENTER);
mainContentPane.add(welcomeLabel);
createMenu(mainFrame);
mainFrame.setVisible(true);
}
/**
* Top Menu creator.
*
* @param frame The frame we want to create the menu for.
*/
private void createMenu(JFrame frame)
{
JMenuBar topmenu = new JMenuBar();
frame.setJMenuBar(topmenu);
// create file menu
JMenu fileMenu = new JMenu("File");
topmenu.add(fileMenu);
JMenuItem quitFileMenuItem = new JMenuItem("Quit");
quitFileMenuItem.addActionListener(this);
fileMenu.add(quitFileMenuItem);
// create Bookshelf menu
JMenu bookMenu = new JMenu("Bookshelf");
topmenu.add(bookMenu);
JMenuItem addBookItem = new JMenuItem("Add book...");
addBookItem.addActionListener(this);
bookMenu.add(addBookItem);
JMenuItem bookshelfSizeItem = new JMenuItem("Bookshelf size");
bookshelfSizeItem.addActionListener(this);
bookMenu.add(bookshelfSizeItem);
JMenuItem bookshelfCostItem = new JMenuItem("Bookshelf cost");
bookshelfCostItem.addActionListener(this);
bookMenu.add(bookshelfCostItem);
JMenuItem mostExpensiveBookItem = new JMenuItem("Most expensive book");
mostExpensiveBookItem.addActionListener(this);
bookMenu.add(mostExpensiveBookItem);
// create help menu
JMenu helpMenu = new JMenu("Help");
topmenu.add(helpMenu);
JMenuItem aboutItem = new JMenuItem("About");
aboutItem.addActionListener(this);
helpMenu.add(aboutItem);
}
/**
* Method invoking events depending on the actions performed.
*
* @param e Event
*/
public void actionPerformed(ActionEvent e) {
String selectedOption = e.getActionCommand();
if (selectedOption.equals("About"))
{
JOptionPane.showMessageDialog(mainFrame,
"This BookShelf GUI was created by Pawel Makulski.");
}
else if (selectedOption.equals("Add book..."))
{
addBookDialog();
}
else if (selectedOption.equals("Bookshelf size"))
{
if (bookList.sizeOfBookshelf() == 1) {
JOptionPane.showMessageDialog(null, "The bookshelf contains one book.");
}
else
{
JOptionPane.showMessageDialog(null, "The bookshelf contains " + bookList.sizeOfBookshelf() + " books.");
}
}
// Display a dialog box with bookshelf cost info
else if (selectedOption.equals("Bookshelf cost"))
{
JOptionPane.showMessageDialog(null, "The cummulative price of all books on the bookshelf is " + bookList.costOfBookshelf());
}
else if (selectedOption.equals("Most expensive book"))
{
JOptionPane.showMessageDialog(null, "The most expensive book on the bookshelf costs " + bookList.highestPricePaid());
}
// Buttons
else if (e.getSource() == saveButton)
{
saveBook();
}
else if (e.getSource() == okButton)
{
saveBook();
clear();
close();
}
else if (e.getSource() == cancelButton)
{
clear();
close();
}
else if (e.getSource() == clearButton) {
clear();
}
else if (selectedOption.equals("Quit"))
{
System.exit(0);
}
}
/**
* Fetches user's input from the text fields, validates it and saves it to the bookshelf.
*
*/
public void saveBook() {
String author = authorTF.getText();
String title = titleTF.getText();
String cost = costTF.getText();
String publisher = publisherTF.getText();
String year = yearTF.getText();
try {
Double.parseDouble(costTF.getText());
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Cost must be a numeric value!");
costTF.setText("");
}
try {
Integer.parseInt(yearTF.getText());
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Please use the correct year format!");
yearTF.setText("");
}
if (authorTF.getText().equals("")) {
JOptionPane.showMessageDialog(null,
"Please enter author name.");
}
else if (titleTF.getText().equals("")) {
JOptionPane.showMessageDialog(null,
"Please enter book title.");
}
else if (publisherTF.getText().equals("")) {
JOptionPane.showMessageDialog(null,
"Please enter publisher name.");
}
else {
bookList.addBook(new Book(title, author, Integer.parseInt(year), publisher, Double.parseDouble(cost)));
JOptionPane.showMessageDialog(null, "Book successfully added to bookcase.");
clear();
}
}
/**
* Closes the Add Book dialog.
*
*/
public void close(){
bookDialog.dispose();
}
/**
* Creates and displays the Add Book dialog.
*
*/
public void addBookDialog(){
bookDialog = new JDialog(mainFrame, "Add new book");
bookDialog.setSize(400, 150);
bookDialog.setModal(true);
bookDialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Container bookContentPane = bookDialog.getContentPane();
bookContentPane.setLayout(new BorderLayout());
// Panel for Text fields and labels
JPanel bookInfoPanel = new JPanel();
bookInfoPanel.setLayout(new GridLayout(5,2));
bookContentPane.add(bookInfoPanel, BorderLayout.CENTER);
// Panel for Buttons
JPanel bookButtonPanel = new JPanel();
bookButtonPanel.setLayout(new GridLayout(4,1));
bookContentPane.add(bookButtonPanel, BorderLayout.EAST);
// Add text fields and labels to the panel
bookInfoPanel.add(authorLabel);
bookInfoPanel.add(authorTF);
bookInfoPanel.add(titleLabel);
bookInfoPanel.add(titleTF);
bookInfoPanel.add(publisherLabel);
bookInfoPanel.add(publisherTF);
bookInfoPanel.add(yearLabel);
bookInfoPanel.add(yearTF);
bookInfoPanel.add(costLabel);
bookInfoPanel.add(costTF);
// Add buttons to the panel
bookButtonPanel.add(saveButton);
bookButtonPanel.add(okButton);
bookButtonPanel.add(cancelButton);
bookButtonPanel.add(clearButton);
// Action listeners for buttons
saveButton.addActionListener(this);
okButton.addActionListener(this);
cancelButton.addActionListener(this);
clearButton.addActionListener(this);
bookDialog.setVisible(true);
}
/**
* Initiates variables used by the Add Book dialog.
*
*/
public void createBookVariables() {
// Create labels
authorLabel = new JLabel("Author", JLabel.CENTER);
costLabel = new JLabel("Cost", JLabel.CENTER);
titleLabel = new JLabel("Title", JLabel.CENTER);
publisherLabel = new JLabel("Publisher", JLabel.CENTER);
yearLabel = new JLabel("Year", JLabel.CENTER);
// Create text fields
authorTF = new JTextField(50);
costTF = new JTextField(10);
titleTF = new JTextField(50);
publisherTF = new JTextField(50);
yearTF = new JTextField(4);
// Create buttons
saveButton = new JButton("Save");
okButton = new JButton("OK");
cancelButton = new JButton("Cancel");
clearButton = new JButton("Clear");
}
/**
* Resets the values of the Add Book dialog text fields.
*
*/
public void clear() {
// Clear textfields
authorTF.setText("");
costTF.setText("");
titleTF.setText("");
publisherTF.setText("");
yearTF.setText("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment