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)
插入按钮后,我无法将数据添加到表格中;它突然关闭我的JTable
框架并返回主页。我已经搜索了所有论坛和其他网络,但仍无法找到答案。那么有什么方法可以让按钮与table
一起使用?谢谢!
答案 0 :(得分:0)
从堆栈跟踪中,event.getSource()返回一个JButton,而不是一个JMenuItem。转换为JButton而不是JMenuItem。您收到错误的行应为:
JButton menu = (JButton) event.getSource();