0
public void actionPerformed(ActionEvent event) {

        this.setVisible(false);     //hide current frame
        prevScreen.setVisible(true); //open the prev frame

        JMenuItem menu = (JMenuItem) event.getSource(); //error at here
        if (menu == menuItemAdd) {
            addNewRow();
        } else if (menu == menuItemRemove) {
            removeCurrentRow();
        } else if (menu == menuItemRemoveAll) {
            removeAllRows();
        }


    }
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JMenuItem
    at Addnewbook.actionPerformed(Addnewbook.java:109)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

After I insert the button, I cannot add my data into the table; it suddenly close my JTable frame and back to homepage. I've already search through all forum and other web but still cant find the answer. So is there any way to make the button works with the table? Thanks!

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Sho
  • 5
  • 6
  • What is the error you're getting? Can you post the stack trace? – Amber Dec 26 '15 at 14:20
  • I already edit my post, those are the error happened. – Sho Dec 26 '15 at 14:28
  • mcve: Please edit your question to include a [mcve] that shows your current approach. See also [*The Use of Multiple JFrames, Good/Bad Practice?*](http://stackoverflow.com/q/9554636/230513) – trashgod Dec 27 '15 at 12:25

1 Answers1

0

From your stack trace, event.getSource() is returning a JButton, not a JMenuItem. Cast to a JButton instead of a JMenuItem. The line where you're getting the error should be:

JButton menu = (JButton) event.getSource();    
Amber
  • 2,413
  • 1
  • 15
  • 20
  • yup. tht's the error. Is there any way to solve it? Really appreciate you help. :) – Sho Dec 26 '15 at 14:51
  • i've already cast it to my Jbutton, but it turns out the button didnt work at all. – Sho Dec 26 '15 at 14:53
  • After casting to a JButton the ClassCastException should go away. Are you getting a different error now? – Amber Dec 28 '15 at 19:10