ActionListener with array

I'm pretty new to Java and don't have much experience in it. I've created a Calendar GUI that put the days of the month in a JButton, and the JButtons into a JButton array. What I'd like to do is when a day is clicked, it will open a Schedule of Appointments.

When creating the JButton array, I add an ActionListener to each JButton created. However, the ActionListener is throwing a NullPointerException indicating that there's no longer a reference to the object. I'm not sure where I lost it. Below is the code although not all of it to actually run. It's rather long and windy, but hopefully someone can help. Perhaps there's a simpler way to handle this.

Any help would be appreciated!

protectedclass CalendarGridextends JPanel

{

private String[] daysOfWeek ={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

JButton button =null;

JButton[] buttonsArray =null;

public CalendarGrid(GregorianCalendar d)

{

setLayout(new BorderLayout());

//create the days of week labels and add to panel's NORTH border

JPanel dowPanel = getDaysOfWeek();

add(dowPanel, BorderLayout.NORTH);

//create the days for the month and add to panel's CENTER

JPanel cdPanel = getCalendarDays(d);

add(cdPanel, BorderLayout.CENTER);

}

private JPanel getDaysOfWeek()

{

JPanel dowContent =new JPanel();

dowContent.setLayout(new GridLayout(1,GRIDCOLS));

for (int i = 0; i < daysOfWeek.length; i++)

{

JLabel l =new JLabel(daysOfWeek[i], JLabel.CENTER);

l.setForeground(Color.red);

dowContent.add(l);

}

dowContent.setBackground(Color.cyan);

add(dowContent);

return dowContent;

}

private JPanel getCalendarDays(GregorianCalendar d)

{

boolean bCurrentDay =false;

String dayString;

JPanel cdContent =new JPanel();

cdContent.setLayout(new GridLayout(6, GRIDCOLS));

//GregorianCalendar d = new GregorianCalendar();

int currentDay = d.get(Calendar.DAY_OF_MONTH);

d.set(Calendar.DAY_OF_MONTH, 1);

int day = d.get(Calendar.DAY_OF_MONTH);

int maxDays = d.getMaximum(Calendar.DAY_OF_MONTH);

int month = d.get(Calendar.MONTH);

int weekday = d.get(Calendar.DAY_OF_WEEK);

int numDays = 0;

JButton[] buttonsArray =new JButton[maxDays + 1];

//JButton button = null;

// blank out button labels until first day of month

setBlanks(cdContent, button, weekday, bCurrentDay);

//create button and add to buttonsArray

do

{

if (day == currentDay)

bCurrentDay =true;

else

bCurrentDay =false;

dayString = String.valueOf(day);

button = addButton(dayString, cdContent, d, bCurrentDay);

buttonsArray[numDays] = button;

buttonsArray[numDays].addActionListener (new daySelected());

cdContent.add(button);

//System.out.println("button in array occurance: " + numDays +buttonsArray[numDays]);

d.add(Calendar.DAY_OF_MONTH, 1);

day = d.get(Calendar.DAY_OF_MONTH);

numDays++;

}while (d.get(Calendar.MONTH) == month);

// blank out button labels until first day of month

setBlanks(cdContent, button, weekday, bCurrentDay);

add(cdContent, BorderLayout.CENTER);

return cdContent;

}

privatevoid setBlanks(JPanel cdContent, JButton button,int weekday,boolean bCurrentDay)

{

for (int i = Calendar.SUNDAY; i < weekday; i++)

{

button = addButton(" ", cdContent, d, bCurrentDay);

cdContent.add(button);

}

}

private JButton addButton(String label, JPanel cdContent, GregorianCalendar d,boolean bCurrentDay)

{

JButton button =new JButton(label);

button.setFocusPainted(true);

if (label ==" " || bCurrentDay)

button.setBorderPainted(false);

else

button.setBorderPainted(true);

return button;

}

privateclass daySelectedimplements ActionListener

{

publicvoid actionPerformed(ActionEvent evt)

{

int idx = 0;

int buttonNbr = 0;

System.out.println("button in array occurance: 20 " + buttonsArray[20]);

//String buttonText = buttonsArray[idx].getText();

//if (evt.equals(buttonsArray[idx]))

//{

//System.out.println(buttonsArray);

//String buttonText = buttonsArray[idx].getText();

//buttonNbr = Integer.parseInt(buttonText);

//System.out.println("buttonNbr: "+ buttonNbr);

//}

//} while (idx < buttonsArray.length);

//d.set(Calendar.DAY_OF_MONTH, buttonNbr);

//ScheduleFrame frame = new ScheduleFrame(d);

//frame.show();

}

}

}

}

[8179 byte] By [newCoderInJavaa] at [2007-9-30 23:52:05]
# 1

Hi,

I think the problem is in:

private JPanel getCalendarDays(GregorianCalendar d)

You wrote:

JButton[] buttonsArray = new JButton[maxDays + 1];

You define a local function variable buttonsArray, but you want to use the class member buttonsArray.

buttonsArray = new JButton[maxDays + 1];

should solve your problem.

matthias_petera at 2007-7-7 15:06:54 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...