import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.ut
ID: 3693034 • Letter: I
Question
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class BookGUI extends JFrame
{
ArrayList<Book> bookList = new ArrayList<Book>();
private JCheckBox fictionBox;
private JCheckBox nonFictionBox;
private ButtonGroup boxGroup0;
private ButtonGroup boxGroup1;
private JLabel bookTitleLabel;
private JLabel authorLabel;
private JLabel isbnLabel;
private JLabel publisherLabel;
private JLabel stateLabel;
private JLabel dateLabel;
private JLabel priceLabel;
private JLabel quantityLabel;
private JLabel fictionCodeLabel;
private JLabel nonFictionLabel;
private JTextField bookTitleField;
private JTextField authorField;
private JTextField isbnField;
private JTextField publisherField;
private JTextField stateField;
private JTextField dateField;
private JTextField priceField;
private JTextField quantityField;
private JTextField fictionCodeField;
private JRadioButton signedButton;
private JRadioButton notSignedButton;
private JComboBox<String> categoryBox;
private JButton submitButton;
private JButton finishButton;
private String[] myList = {"Select One","Biography", "Technology", "History", "Science","Autobiography"};
public BookGUI()
{
super("Book Company");
setLayout(new GridLayout(13,2));
fictionBox = new JCheckBox("Add Fiction");
nonFictionBox = new JCheckBox("Add NonFiction");
boxGroup1 = new ButtonGroup();
boxGroup1.add(fictionBox);
boxGroup1.add(nonFictionBox);
bookTitleLabel = new JLabel(" Enter Title");
bookTitleField = new JTextField(20);
authorLabel = new JLabel(" Enter author");
authorField = new JTextField(20);
isbnLabel = new JLabel(" Enter ISBN");
isbnField = new JTextField(20);
publisherLabel = new JLabel(" Enter Publisher Name");
publisherField = new JTextField(20);
stateLabel = new JLabel(" Enter Publisher State");
stateField = new JTextField(20);
dateLabel = new JLabel(" Enter Date Published");
dateField = new JTextField(20);
priceLabel = new JLabel(" Enter Price");
priceField = new JTextField(20);
quantityLabel = new JLabel(" Enter Quantity Sold");
quantityField = new JTextField(20);
fictionCodeLabel = new JLabel(" Enter Fiction Code");
fictionCodeField = new JTextField(20);
JRadioButton signedButton = new JRadioButton("Signed");
JRadioButton notSignedButton = new JRadioButton("Not Signed");
boxGroup0 = new ButtonGroup();
boxGroup0.add(signedButton);
boxGroup0.add(notSignedButton);
nonFictionLabel = new JLabel(" Select Fiction Category");
categoryBox = new JComboBox<String>(myList);
categoryBox.setMaximumRowCount(3);
submitButton = new JButton("SUBMIT");
finishButton = new JButton("FINISH");
dateField.setToolTipText("Format xx/xx/xx");
fictionBox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent ie)
{
bookTitleLabel.setEnabled(true);
bookTitleField.setEnabled(true);
authorLabel.setEnabled(true);
authorField.setEnabled(true);
isbnLabel.setEnabled(true);
isbnField.setEnabled(true);
publisherLabel.setEnabled(true);
publisherField.setEnabled(true);
stateLabel.setEnabled(true);
stateField.setEnabled(true);
dateLabel.setEnabled(true);
dateField.setEnabled(true);
priceLabel.setEnabled(true);
priceField.setEnabled(true);
quantityLabel.setEnabled(true);
quantityField.setEnabled(true);
fictionCodeLabel.setEnabled(true);
fictionCodeField.setEnabled(true);
signedButton.setEnabled(true);
notSignedButton.setEnabled(true);
nonFictionLabel.setEnabled(false);
categoryBox.setEnabled(false);
}
});
nonFictionBox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent ie)
{
bookTitleLabel.setEnabled(true);
bookTitleField.setEnabled(true);
authorLabel.setEnabled(true);
authorField.setEnabled(true);
isbnLabel.setEnabled(true);
isbnField.setEnabled(true);
publisherLabel.setEnabled(true);
publisherField.setEnabled(true);
stateLabel.setEnabled(true);
stateField.setEnabled(true);
dateLabel.setEnabled(true);
dateField.setEnabled(true);
priceLabel.setEnabled(true);
priceField.setEnabled(true);
quantityLabel.setEnabled(true);
quantityField.setEnabled(true);
fictionCodeLabel.setEnabled(false);
fictionCodeField.setEnabled(false);
signedButton.setEnabled(false);
notSignedButton.setEnabled(false);
nonFictionLabel.setEnabled(true);
categoryBox.setEnabled(true);
}
});
ButtonHandler myHandler = new ButtonHandler();
submitButton.addActionListener(myHandler);
finishButton.addActionListener(myHandler);
add(fictionBox);
add(nonFictionBox);
add(bookTitleLabel);
add(bookTitleField);
add(authorLabel);
add(authorField);
add(isbnLabel);
add(isbnField);
add(publisherLabel);
add(publisherField);
add(stateLabel);
add(stateField);
add(dateLabel);
add(dateField);
add(priceLabel);
add(priceField);
add(quantityLabel);
add(quantityField);
add(fictionCodeLabel);
add(fictionCodeField);
add(signedButton);
add(notSignedButton);
add(nonFictionLabel);
add(categoryBox);
add(submitButton);
add(finishButton);
bookTitleLabel.setEnabled(false);
bookTitleField.setEnabled(false);
authorLabel.setEnabled(false);
authorField.setEnabled(false);
isbnLabel.setEnabled(false);
isbnField.setEnabled(false);
publisherLabel.setEnabled(false);
publisherField.setEnabled(false);
stateLabel.setEnabled(false);
stateField.setEnabled(false);
dateLabel.setEnabled(false);
dateField.setEnabled(false);
priceLabel.setEnabled(false);
priceField.setEnabled(false);
quantityLabel.setEnabled(false);
quantityField.setEnabled(false);
fictionCodeLabel.setEnabled(false);
fictionCodeField.setEnabled(false);
signedButton.setEnabled(false);
notSignedButton.setEnabled(false);
nonFictionLabel.setEnabled(false);
categoryBox.setEnabled(false);
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String title;
String author;
String isbn;
String publisher;
String state;
String datePublished;
double price;
int quantity;
int fictionCode;
int categoryCode;
String nonFictionCategory;
Boolean signedByAuthor = true;
if (ae.getSource() == submitButton)
{
boolean input;
input = checkInput();
if (input)
{
if (fictionBox.isSelected())
{
title = bookTitleField.getText();
author = authorField.getText();
isbn = isbnField.getText();
publisher = publisherField.getText();
state = stateField.getText();
Publisher myPub = new Publisher(publisher, state);
datePublished = dateField.getText();
String date[] = datePublished.split("/");
Date newDate = new Date(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]));
price = Double.parseDouble(priceField.getText());
quantity = Integer.parseInt(quantityField.getText());
fictionCode = Integer.parseInt(fictionCodeField.getText());
fictionBox.setSelected(false);
nonFictionBox.setSelected(false);
bookTitleField.setText("");
authorField.setText("");
isbnField.setText("");
publisherField.setText("");
stateField.setText("");
priceField.setText("");
dateField.setText("");
quantityField.setText("");
fictionCodeField.setText("");
signedButton.setSelected(false);
notSignedButton.setSelected(false);
bookTitleField.requestFocus();
if (notSignedButton.isSelected())
signedByAuthor = false;
bookList.add(new Fiction(title, author, isbn, myPub , price, fictionCode, signedByAuthor, quantity, newDate));
}
else if (nonFictionBox.isSelected())
{
title = bookTitleField.getText();
author = authorField.getText();
isbn = isbnField.getText();
publisher = publisherField.getText();
state = stateField.getText();
Publisher myPub = new Publisher(publisher, state);
datePublished = dateField.getText();
String date[] = datePublished.split("/");
Date newDate = new Date(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]));
price = Double.parseDouble(priceField.getText());
quantity = Integer.parseInt(quantityField.getText());
nonFictionCategory = categoryBox.getSelectedItem().toString();
System.out.println(myList[categoryBox.getSelectedIndex()]);
fictionBox.setSelected(false);
nonFictionBox.setSelected(false);
bookTitleField.setText("");
authorField.setText("");
isbnField.setText("");
publisherField.setText("");
stateField.setText("");
priceField.setText("");
dateField.setText("");
quantityField.setText("");
nonFictionCategory = categoryBox.getSelectedItem().toString();
bookTitleField.requestFocus();
bookList.add(new NonFiction(title, author, isbn, myPub , price, NonFiction.CategoryCode.valueOf(nonFictionCategory) , quantity, newDate));
}
}
}
else if (ae.getSource() == finishButton)
{
writeFile();
String message = "";
for(Book b:bookList)
if(b instanceof Fiction)
{
message += String.format("%s%n",((Fiction) b).printInvoice());
}
else if(b instanceof NonFiction)
{
message += String.format("%s%n",((NonFiction) b).printInvoice());
}
JOptionPane.showMessageDialog(null,message);
System.exit(0);
}
}
public boolean checkInput()
{
boolean input = true;
if (bookTitleField.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Enter Book Title");
bookTitleField.requestFocus();
input = false;
}
else if (authorField.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Enter Author Name");
authorField.requestFocus();
input = false;
}
else if (isbnField.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Enter ISBN");
isbnField.requestFocus();
input = false;
}
else if (publisherField.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Enter Publisher");
publisherField.requestFocus();
input = false;
}
else if (stateField.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Enter State");
stateField.requestFocus();
input = false;
}
else if (dateField.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Enter Date mm/dd/yyyy");
dateField.requestFocus();
input = false;
}
else if (priceField.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Enter Price");
priceField.requestFocus();
input = false;
}
else if (quantityField.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "Enter Quantity");
quantityField.requestFocus();
input = false;
}
else if ((fictionBox.isSelected()) && (fictionCodeField.getText().equals("")))
{
JOptionPane.showMessageDialog(null, "Enter Fiction Code");
fictionCodeField.requestFocus();
input = false;
}
else if((!priceField.getText().equals("")) && (!quantityField.getText().equals("")) && (!fictionCodeField.getText().equals("")))
{
try
{
Double.parseDouble(priceField.getText());
}
catch(NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null, "Enter double for price");
priceField.setText("");
priceField.requestFocus();
return false;
}
try
{
Integer.parseInt(quantityField.getText());
}
catch(NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null, "Enter Integer for quantity");
quantityField.setText("");
quantityField.requestFocus();
return false;
}
try
{
Integer.parseInt(fictionCodeField.getText());
}
catch(NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null, "Enter Integer for Code");
fictionCodeField.setText("");
fictionCodeField.requestFocus();
return false;
}
}
return input;
}
}
public void writeFile()
{
ObjectOutputStream output;
try
{
output = new ObjectOutputStream(new FileOutputStream("books.ser"));
for (Book b:bookList)
output.writeObject(b);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
public static ArrayList<Book> readFile()
{
ArrayList<String> bookArray = new ArrayList<String>();
ArrayList<Book> dataArray = new ArrayList<Book>();
Scanner input;
try
{
input = new Scanner( new File("books.txt"));
while (input.hasNext())
{
String temp = input.nextLine();
bookArray.add(temp);
}
input.close();
}
catch (FileNotFoundException fne)
{
fne.printStackTrace();
}
for (int k=0; k<bookArray.size(); k++)
{
String[] temp = bookArray.get(k).split(" ");
if (temp[0].equals("Fiction"))
{
String[] tempSplit = temp[10].split("/");
dataArray.add(new Fiction(temp[1], temp[2], temp[5], new Publisher(temp[3], temp[4]), Double.parseDouble(temp[6]), Integer.parseInt(temp[7]), Boolean.parseBoolean(temp[8]), Integer.parseInt(temp[9]), new Date(Integer.parseInt(tempSplit[0]), Integer.parseInt(tempSplit[1]),Integer.parseInt(tempSplit[2]))));
}
else
{
String[] tempSplit = temp[9].split("/");
dataArray.add(new NonFiction(temp[1], temp[2],temp[5], new Publisher(temp[3], temp[4]) , Double.parseDouble(temp[6]), NonFiction.CategoryCode.valueOf(temp[7]), Integer.parseInt(temp[8]),new Date(Integer.parseInt(tempSplit[0]), Integer.parseInt(tempSplit[1]),Integer.parseInt(tempSplit[2]))));
}
}
return dataArray;
}
}
import javax.swing.*;
import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.Formatter;
import java.io.*;
import java.util.*;
public class BookGUITest
{
public static void main(String args[])
{
BookGUI b = new BookGUI();
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setSize(550,400);
b.setLocationRelativeTo(null);
b.setVisible(true);
}
}
My GUI Opens up. My Submit Button and My FInish Button doesn't work. As soon as I clikc on submit I get these many errors. Can please someone help me?
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: No enum constant NonFiction.CategoryCode.History
at java.lang.Enum.valueOf(Enum.java:238)
at NonFiction$CategoryCode.valueOf(NonFiction.java:6)
at BookGUI$ButtonHandler.actionPerformed(BookGUI.java:338)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Explanation / Answer
what exactly you want?
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.