Java Swing item listeners
I'm having trouble implementing listeners, btw i'm relatively new to java swing programming and i'll be honest, i haven't had much time to go through the encyclopedic java swing library.
I'm trying to implement a calendar with a menu. this calendar is a prototype just to get aquainted with all the java features. there is a "New" menu and in it, a "new calendar" menu option.i created the calendar dates using a loop which creates JButtons and attaches actionlisteners to them like so. when any of the buttons is clicked it changes color:
for(int i=1; i<=31; i++){
String date = i + "";
JLabel date_entry = new JLabel(num,SwingConstants.CENTER);
JButton date_click = new JButton();
date_click.addActionListener(this); // adds action when button is clicked.
click.add(numbers);
testpanel.add(click);
}
Also, my JMenuItem has an action listener attched to it too and when it is clicked, its supposed to open a new JFrame.
JMenuItem menuitem1;
....
menuBar = new JMenuBar();
menuBar.add(menu);
menuitem1 = new JMenuItem("New Calendar");
menuitem1.addActionListener(this);
menu.add(menuitem1);
this is my implementation of the action listener. the buttons change color as they should but when i click the "new calendar" menu option, i get a java.lang.ClassException thats diriving me insane.
public void actionPerformed(ActionEvent event){
JButton source = (JButton)event.getSource();
source.setBackground(Color.red);
JMenuItem source1 = (JMenuItem)(event.getSource());
if(source1 == menuitem1){
newframe = new JFrame("New Calendar");
newframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newframe.pack();
newframe.setVisible(true);
}
}
I desperately need help. thanks

