SHARE
TWEET

Untitled

a guest Jun 25th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Code for BookButton
  2. public BookButton (Book book)
  3.     {
  4.         this.book = book;
  5.         BookButtonListener clicked = new BookButtonListener();
  6.         addActionListener(clicked);
  7.         String buttonText;
  8.         if (book.getTitle().length() >= 20)
  9.         {
  10.             buttonText = book.toString().substring(0,19);
  11.         }
  12.         else
  13.             buttonText = book.getTitle();
  14.         setToolTipText(buttonText);
  15.     }
  16.      
  17. public class LibraryPanel extends JPanel {
  18.  
  19. private Library library;
  20. private BookButton bookButton;
  21. private JScrollPane jScrollPane;
  22. private JButton jButton;
  23. private JTextField fileName;
  24. private JPanel bookList;
  25.  
  26. public LibraryPanel ()
  27. {
  28.     library = new Library();
  29.     JPanel libraryPanel = new JPanel();
  30.     libraryPanel.setLayout(new BoxLayout(libraryPanel, BoxLayout.Y_AXIS));  
  31.     JPanel bookBox = new JPanel();
  32.     bookBox.setLayout(new BorderLayout());
  33.     JPanel importBooks = new JPanel();
  34.     fileName = new JTextField(4);
  35.     jButton = new JButton("Load");
  36.  
  37.     fileName.addActionListener(new MyTextAreaListener());
  38.     jButton.addActionListener(new MyjButtonActionListener());
  39.     JPanel bookList = new JPanel();
  40.     bookList.setLayout(new BoxLayout(bookList, BoxLayout.Y_AXIS));  
  41.     importBooks.add(fileName);
  42.     importBooks.add(jButton);
  43.  
  44.     setLayout(new BorderLayout());
  45.     jScrollPane = new JScrollPane(bookList);
  46.     jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  47.     jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  48.     bookBox.add(jScrollPane);
  49.     libraryPanel.add(bookBox, BorderLayout.CENTER);
  50.     libraryPanel.add(importBooks);
  51.     add(libraryPanel);
  52. }
  53. private class MyjButtonActionListener implements ActionListener
  54.     {
  55.  
  56.         @Override
  57.         public void actionPerformed(ActionEvent e)
  58.         {
  59.             String something = fileName.getText();
  60.             File file = new File(something);
  61.             if(file.exists() && file.isFile())
  62.             {
  63.                 library.loadLibraryFromCSV(something);
  64.                 for(int i = 0;i<library.getBooks().size(); i++)
  65.                 {
  66.                     Book book = library.getBook(i);
  67.                     bookButton = new BookButton(book);
  68.                     bookList.add(bookButton);
  69.                 }
  70.             }
  71.             else
  72.             {
  73.                 fileName.setText("Invalid File");
  74.             }
  75.         }
  76.     }
  77. //I have the actionListener called, however it results in a huge list of errors.
  78.  
  79. Results:
  80. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  81.     at LibraryPanel$MyjButtonActionListener.actionPerformed(LibraryPanel.java:86)
  82.     at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  83.     at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  84.     at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  85.     at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
  86.     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
  87.     at java.awt.Component.processMouseEvent(Unknown Source)
  88.     at javax.swing.JComponent.processMouseEvent(Unknown Source)
  89.     at java.awt.Component.processEvent(Unknown Source)
  90.     at java.awt.Container.processEvent(Unknown Source)
  91.     at java.awt.Component.dispatchEventImpl(Unknown Source)
  92.     at java.awt.Container.dispatchEventImpl(Unknown Source)
  93.     at java.awt.Component.dispatchEvent(Unknown Source)
  94.     at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
  95.     at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
  96.     at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
  97.     at java.awt.Container.dispatchEventImpl(Unknown Source)
  98.     at java.awt.Window.dispatchEventImpl(Unknown Source)
  99.     at java.awt.Component.dispatchEvent(Unknown Source)
  100.     at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
  101.     at java.awt.EventQueue.access$500(Unknown Source)
  102.     at java.awt.EventQueue$3.run(Unknown Source)
  103.     at java.awt.EventQueue$3.run(Unknown Source)
  104.     at java.security.AccessController.doPrivileged(Native Method)
  105.     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
  106.     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
  107.     at java.awt.EventQueue$4.run(Unknown Source)
  108.     at java.awt.EventQueue$4.run(Unknown Source)
  109.     at java.security.AccessController.doPrivileged(Native Method)
  110.     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
  111.     at java.awt.EventQueue.dispatchEvent(Unknown Source)
  112.     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  113.     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  114.     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  115.     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  116.     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  117.     at java.awt.EventDispatchThread.run(Unknown Source)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top