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

[1886 byte] By [puzzlitea] at [2007-11-26 20:01:43]
# 1

> i haven't had much tim

If you don't have time to do basic tutorial: http://java.sun.com/docs/books/tutorial/uiswing/

you better quit programming a while. And come back when you have enough time.

Your current code:

for(int i = 1; i <= 31; i++){

String date = i + "";

JLabel date_entry = new JLabel(num, SwingConstants.CENTER);

// you create a single same button 31 times, NOT 31 buttons

JButton date_click = new JButton();

date_click.addActionListener(this);

click.add(numbers);// we don't see what is 'click'

testpanel.add(click); // we don't see what is 'testpane'

}

//--

/* we don't see full relevant code for your menu */

JMenuItem menuitem1;

// ....

menuBar = new JMenuBar();

menuBar.add(menu);

menuitem1 = new JMenuItem("New Calendar");

menuitem1.addActionListener(this);

menu.add(menuitem1);

//--

public void actionPerformed(ActionEvent event){

JButton source = (JButton)event.getSource();

source.setBackground(Color.red);

// you can't get two different objects from single

// getSource() call. You should use if statement here.

JMenuItem source1 = (JMenuItem)(event.getSource());

if(source1 == menuitem1){

// are you going to make so many of frames

// by running following code each time the

// menu is selected? Terrible, isn't it?

newframe = new JFrame("New Calendar");

newframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

newframe.pack();

newframe.setVisible(true);

// you don't add nothing on the frame -- an empty JFrame is made

}

}

hiwaa at 2007-7-9 23:00:27 > top of Java-index,Java Essentials,New To Java...