Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

menuItem ActionListener bug (MVC pattern)

843806Mar 16 2008 — edited Mar 16 2008
Hello. I'm implementing a book store system using the MVC pattern, but due to the fact that the method actionPerformed() is in class libraryController it doesn't work (and I need it in this class due to the MVC pattern). Can anyone help me? Thanks.
   class libraryView extends JFrame{
    
  JMenuItem addBook = new JMenuItem("Add Book");
  JMenuItem deleteBook = new JMenuItem("Delete Book");
   JMenuItem modifyByTitle = new JMenuItem("Modify by Title");
   JMenuItem modifyByAuthor = new JMenuItem("Modify by Author");
   JMenuItem searchByTitle = new JMenuItem("Search by Title");
    JMenuItem searchByAuthor = new JMenuItem("Search by Author");
    JMenuItem searchByID = new JMenuItem("Search by ID");
    JMenuItem bookListing = new JMenuItem("Book Listing");
   
   public JPanel createContentPane()
    {
        JPanel totalGUI = new JPanel();
        totalGUI.setBackground(Color.white);
        totalGUI.setMinimumSize(new Dimension(300, 200));
        totalGUI.setPreferredSize(new Dimension(500, 500));
        totalGUI.setMaximumSize(new Dimension(300, 200));
        
        totalGUI.setOpaque(true);
        return totalGUI;
    }
    public JMenuBar createMenuBar(){
       
        
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Options");
        
        
        menuBar.add(menu);
       
        menuBar.setBackground(Color.pink);
        
     
        addBook.setFont(new Font("Helvetica",Font.PLAIN,13));
        menu.add(addBook);
        
       
        
        deleteBook.setFont(new Font("Microsoft Sans Serif",Font.PLAIN,13));
        menu.add(deleteBook);
        
       JMenu modifyBook = new JMenu("Modify Book");
        modifyBook.setFont(new Font("Helvetica",Font.PLAIN,13));
        menu.add(modifyBook);
        
    
        modifyByTitle.setFont(new Font("Helvetica",Font.PLAIN,13));
        modifyBook.add(modifyByTitle);
        
      
        modifyByAuthor.setFont(new Font("Helvetica",Font.PLAIN,13));
    
        modifyBook.add(modifyByAuthor);
        
        JMenu searchBook = new JMenu("Search Book");
        searchBook.setFont(new Font("Helvetica",Font.PLAIN,13));
        menu.add(searchBook);
        
      
        searchByTitle.setFont(new Font("Helvetica",Font.PLAIN,13));
   
        searchBook.add(searchByTitle);
      
        searchByAuthor.setFont(new Font("Helvetica",Font.PLAIN,13));

        searchBook.add(searchByAuthor);
        
    
        searchByID.setFont(new Font("Helvetica",Font.PLAIN,13));
    
        searchBook.add(searchByID);
       

        bookListing.setFont(new Font("Helvetica",Font.PLAIN,13));
   
        menu.add(bookListing);
        return menuBar;
    }
    
   
    
    private static void createAndShowGUI(){
        //Create and set up the window.
        // JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] JMenuBar [=]");

        //Create and set up the content pane.
        libraryView libr_obj = new libraryView();
        frame.setContentPane(libr_obj.createContentPane());
        // We now also set the MenuBar of the Frame to our MenuBar 
        frame.setJMenuBar(libr_obj.createMenuBar());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        
   
        
    }
   public void init_view(){
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
   
}

    void MenuActionListeners(ActionListener al) {
        addBook.setActionCommand("addBook");
        addBook.addActionListener(al);
        
        deleteBook.setActionCommand("deleteBook");
        deleteBook.addActionListener(al);
        
        modifyByTitle.setActionCommand("modifyByTitle");
        modifyByTitle.addActionListener(al);
        
        modifyByAuthor.setActionCommand("modifyByAuthor");
        modifyByAuthor.addActionListener(al);
        
        searchByTitle.setActionCommand("searchByTitle");
        searchByTitle.addActionListener(al);
        
        searchByAuthor.setActionCommand("searchByAuthor");
        searchByAuthor.addActionListener(al);
        
        searchByID.setActionCommand("searchByID");
        searchByID.addActionListener(al);
        
        bookListing.setActionCommand("bookListing");
        bookListing.addActionListener(al);
     
    }
    
    

  }

  
 class libraryController implements ActionListener{
     libraryModel model;
     libraryView view;
    
   public libraryController (libraryModel model, libraryView view) {
        this.model = model;
        this.view  = view;
     
        this.view.init_view();
        view.MenuActionListeners(this);
       
   }
  
   public void actionPerformed(ActionEvent ae)
  {
      
    String action_com = ae.getActionCommand();
        if (action_com.equals("addBook"))
       {
          System.out.println("add Book");
         
        }
  }


}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 13 2008
Added on Mar 16 2008
4 comments
200 views