Do not use on any
shared computer
August 29, 2008 09:09pm pdt
 
[x]
Attachment Details

How do I get my popup calendar selector to return its date value?

Tags: J2SE
Hello, all!

I've been putting together this popup calendar as a means of making sure users don't put in a bad date format for a program I'm developing that has to communicate with a SQL database.  I know I'm missing something blatantly obvious, but I have no other Java programmers around here to check my code, so I'm turning to the experts.  If you guys can slap some sense into me, feel free to have at my code and use my little widget for your future toys.

To speed up the GUI process, I used NetBeans to drag-and-drop the skeleton of where everything should go.  For the real meat of the project--the positions of the weekday names and the days of the months themselves--was programmed by hand along with all the calculations and such.  However, that's why for the PopupCal class all the variables are at the bottom:  I just wanted to keep it consistent since NetBeans automatically drops its variables down there.

The project's due on Friday, but I've got an idea for something else if this doesn't get fixed by then.

Thanks for all your help in advance.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529:
530:
531:
532:
533:
534:
535:
536:
537:
538:
539:
540:
541:
542:
543:
544:
545:
546:
547:
548:
549:
550:
551:
552:
553:
554:
555:
556:
557:
558:
559:
560:
561:
562:
563:
564:
565:
566:
567:
568:
569:
570:
571:
572:
573:
574:
575:
576:
577:
578:
579:
580:
581:
582:
583:
584:
585:
586:
587:
588:
589:
590:
591:
592:
593:
594:
595:
596:
597:
598:
599:
600:
601:
602:
603:
604:
605:
606:
607:
608:
609:
610:
611:
612:
613:
// PopupCal
 
import java.awt.event.ActionEvent;
import java.text.ParseException;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
 
public class PopupCal extends javax.swing.JDialog implements ActionListener {
    
    /** Creates new form PopupCal */
    public PopupCal(java.awt.Frame parent) {
        super(parent);
        initComponents();
        
        // Line up the days
        pnlDays.setLayout(new GridLayout(0,7));
        pnlWeekdays.setLayout(new GridLayout(0,7));
        
        // Set the weekday names
        for (int weekdays = 0; weekdays < MAX_WEEKDAYS; weekdays++)
        {
            lblWeekdays[weekdays] = new JLabel(DAY_NAMES[weekdays]);
            lblWeekdays[weekdays].setBackground(NORM_DAY_BG);
            lblWeekdays[weekdays].setFont(DAY_NAME_FONT);
            lblWeekdays[weekdays].setHorizontalTextPosition(SwingConstants.CENTER);
            
            pnlWeekdays.add(lblWeekdays[weekdays]);
        }
        
        // Lay out the month days
        for (int days = 0; days < MAX_CAL_DAYS; days++)
        {
            lblDays[days] = new JLabel();
            lblDays[days].setBackground(NORM_DAY_BG);
            lblDays[days].setHorizontalTextPosition(SwingConstants.CENTER);
            lblDays[days].setFont(NORM_DAY_FONT);
            lblDays[days].setOpaque(true);
            lblDays[days].setBorder(BorderFactory.createLineBorder(Color.BLACK));
            pnlDays.add(lblDays[days]);
        }
        
        setComboBoxes();
        setSelectedMonthYear();
 
        btnToday.addActionListener(this);
        btnOK.addActionListener(this);
        btnCancel.addActionListener(this);
        cboMonths.addActionListener(this);
        cboYears.addActionListener(this);
        
        // get rid of extra space
        pack();
    }
 
    /**
     * Open the calendar to the requested date.  Then return the
     * date selected if the user doesn't cancel out.
     * 
     * @param date      Date to open the calendar initially to.
     * @param df        Format the date is in.
     * @return Formatted selected date or <code>null</code> 
     *      if user cancelled or has not exited the screen yet.
     */
    public String selectDate(String date, SimpleDateFormat df)
    {
        try {
            
            Calendar convertDate = Calendar.getInstance();
            
            Date toConvert = df.parse(date);
            
            convertDate.setTime(toConvert);
            
            initiateViewWithDate(convertDate);
            
            exitScreen = false;
            
            if (!exitScreen)
            {
                return null;
            }
            
            toConvert = selectedDate.getTime();
            
            return df.format(toConvert);
 
        } catch (ParseException ex) {
            // for debugging
            ex.printStackTrace();
            
            return null;
        }
    }
    
    /**
     * Initialize the calendar view to a certain date.
     * 
     * @param date      Date to set the calendar to.
     */
    public void initiateViewWithDate(Calendar date)
    {
        selectedDate.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), 
                date.get(Calendar.DATE));
        tempSelectedDate.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), 
                date.get(Calendar.DATE));
        
        displayedMonth = tempSelectedDate.get(Calendar.MONTH);
        displayedYear = tempSelectedDate.get(Calendar.YEAR);
        
        setSelectedMonthYear();
        
        setVisible(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     * 
     * Auto-inserted by NetBeans
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
 
        pnlMonthYear = new javax.swing.JPanel();
        cboMonths = new javax.swing.JComboBox();
        cboYears = new javax.swing.JComboBox();
        btnToday = new javax.swing.JButton();
        pnlMonthDisp = new javax.swing.JPanel();
        pnlWeekdays = new javax.swing.JPanel();
        pnlDays = new javax.swing.JPanel();
        pnlButtons = new javax.swing.JPanel();
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
 
        setTitle("Calendar");
        setResizable(false);
 
        btnToday.setText("Today");
 
        javax.swing.GroupLayout pnlMonthYearLayout = new javax.swing.GroupLayout(pnlMonthYear);
        pnlMonthYear.setLayout(pnlMonthYearLayout);
        pnlMonthYearLayout.setHorizontalGroup(
            pnlMonthYearLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMonthYearLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(cboMonths, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cboYears, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnToday)
                .addContainerGap(33, Short.MAX_VALUE))
        );
        pnlMonthYearLayout.setVerticalGroup(
            pnlMonthYearLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMonthYearLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMonthYearLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboMonths, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnToday)
                    .addComponent(cboYears, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );
 
        javax.swing.GroupLayout pnlWeekdaysLayout = new javax.swing.GroupLayout(pnlWeekdays);
        pnlWeekdays.setLayout(pnlWeekdaysLayout);
        pnlWeekdaysLayout.setHorizontalGroup(
            pnlWeekdaysLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 340, Short.MAX_VALUE)
        );
        pnlWeekdaysLayout.setVerticalGroup(
            pnlWeekdaysLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 25, Short.MAX_VALUE)
        );
 
        javax.swing.GroupLayout pnlDaysLayout = new javax.swing.GroupLayout(pnlDays);
        pnlDays.setLayout(pnlDaysLayout);
        pnlDaysLayout.setHorizontalGroup(
            pnlDaysLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 340, Short.MAX_VALUE)
        );
        pnlDaysLayout.setVerticalGroup(
            pnlDaysLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 128, Short.MAX_VALUE)
        );
 
        javax.swing.GroupLayout pnlMonthDispLayout = new javax.swing.GroupLayout(pnlMonthDisp);
        pnlMonthDisp.setLayout(pnlMonthDispLayout);
        pnlMonthDispLayout.setHorizontalGroup(
            pnlMonthDispLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlWeekdays, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(pnlDays, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pnlMonthDispLayout.setVerticalGroup(
            pnlMonthDispLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMonthDispLayout.createSequentialGroup()
                .addComponent(pnlWeekdays, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlDays, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
 
        btnOK.setText("OK");
 
        btnCancel.setText("Cancel");
 
        javax.swing.GroupLayout pnlButtonsLayout = new javax.swing.GroupLayout(pnlButtons);
        pnlButtons.setLayout(pnlButtonsLayout);
        pnlButtonsLayout.setHorizontalGroup(
            pnlButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlButtonsLayout.createSequentialGroup()
                .addGap(102, 102, 102)
                .addComponent(btnOK)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancel)
                .addContainerGap(102, Short.MAX_VALUE))
        );
 
        pnlButtonsLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCancel, btnOK});
 
        pnlButtonsLayout.setVerticalGroup(
            pnlButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlButtonsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOK)
                    .addComponent(btnCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlMonthYear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlButtons, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlMonthDisp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlMonthYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlMonthDisp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnlButtons, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
 
        pack();
    }// </editor-fold>                        
    
    /**
     * Fill the combo boxes with their respective values.
     */
    private void setComboBoxes()
    {
        for (int months = 0; months < MAX_MONTHS; months++)
        {
            cboMonths.addItem(MONTH_NAMES[months]);
        }
        
        for (int years = 0; years < MAX_YEARS; years++)
        {
            cboYears.addItem(CURRENT_YEAR + years);
        }
    }
    
    /**
     * Set the month and year to for a specified date then set the
     * month days.
     */
    private void setSelectedMonthYear()
    {
        cboMonths.setSelectedIndex(tempSelectedDate.get(Calendar.MONTH));
        cboYears.setSelectedItem(tempSelectedDate.get(Calendar.YEAR));
        
        displayedMonth = cboMonths.getSelectedIndex();
        Integer dispYr = (Integer)cboYears.getSelectedItem();
        displayedYear = dispYr.intValue();
        
        setMonthDays();
    }
    
    /**
     * Lay out the days of the month, checking for if the current
     * date lies within that particular month for formatting purposes.
     * Also run checks for setting up what day to select from as the user
     * moves from month to month.
     */
    private void setMonthDays()
    {
        clearBackgrounds();
        
        for (int clearDays = 0; clearDays < MAX_CAL_DAYS; clearDays++)
        {
            lblDays[clearDays].setText("");
            lblDays[clearDays].setFont(NORM_DAY_FONT);
            lblDays[clearDays].setForeground(NORM_DAY_FONT_COL);
            
            lblDays[clearDays].removeMouseListener(new DaySelectHandler());
        }
        
        Calendar firstDay = Calendar.getInstance();
        firstDay.set(displayedYear, displayedMonth, 1);
        
        // Determines what day the next month would start on.
        int nextMonthStart = firstDay.get(Calendar.DAY_OF_WEEK) 
                + firstDay.getActualMaximum(Calendar.DAY_OF_MONTH) - 1;
        
        int monthDay = 1;
        
        // Cycle through the days of the month, setting the day of the 
        // month as it goes along.
        for (int daySet = firstDay.get(Calendar.DAY_OF_WEEK)-1; 
            daySet < nextMonthStart; daySet++)
        {
            lblDays[daySet].setText(Integer.toString(monthDay));
            lblDays[daySet].addMouseListener(new DaySelectHandler());
            
            // Checks to see if the currently selected date is larger than
            // the month currently on the calendar.
            if (tempSelectedDate.get(Calendar.DATE) > 
                    firstDay.getActualMaximum(Calendar.DAY_OF_MONTH))
            {
                if (monthDay == firstDay.getActualMaximum(Calendar.DAY_OF_MONTH))
                {
                    colorSelected(lblDays[daySet]);
                }
            }
            else
            {
                if (monthDay == tempSelectedDate.get(Calendar.DATE))
                {
                    colorSelected(lblDays[daySet]);
                }
            }
            
            if (isToday(monthDay))
            {
                colorToday(lblDays[daySet]);
            }
            
            monthDay++;
        }
    }
    
    private void clearBackgrounds()
    {
        for (int clear = 0; clear < MAX_CAL_DAYS; clear++)
        {
            lblDays[clear].setBackground(NORM_DAY_BG);
        }
    }
    
    private void colorSelected(JLabel dayLabel)
    {
        dayLabel.setBackground(SEL_DAY_BG);
        
        int dayOnLabel = Integer.parseInt(dayLabel.getText());
        
        if (isToday(dayOnLabel))
        {
            colorToday(dayLabel);
        }
    }
    
    private void colorToday(JLabel dayLabel)
    {
        dayLabel.setForeground(TODAY_FONT_COL);
        dayLabel.setFont(TODAY_FONT);
    }
    
    private boolean isToday(int date)
    {
        if ((displayedMonth == CURRENT_MONTH) 
                && (date == TODAY.get(Calendar.DATE)) 
                && (displayedYear == CURRENT_YEAR))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /**
     * MouseHandler for when a month day label is clicked.  Determines
     * which day is clicked, colors it appropriately, and then sets the 
     * selected date to that day.
     */
    private class DaySelectHandler extends MouseAdapter
    {
        @Override
        public void mouseClicked(MouseEvent e)
        {
            clearBackgrounds();
            
            for (int findSelected = 0; findSelected < MAX_CAL_DAYS; 
                findSelected++)
            {   
                if (e.getSource() == lblDays[findSelected])
                {
                    tempSelectedDate.set(displayedYear, displayedMonth, 
                            Integer.parseInt(lblDays[findSelected].getText()));
                    
                    colorSelected(lblDays[findSelected]);
                    
                    // kick out of loop
                    findSelected = MAX_CAL_DAYS;
                }
            }
        }
    }
    
    public static Calendar getSelectedDate()
    {
        return selectedDate;
    }
    
    public void actionPerformed(ActionEvent e) 
    {
        if (e.getSource() == btnToday)
        {
            displayedMonth = CURRENT_MONTH;
            cboMonths.setSelectedIndex(displayedMonth);
            
            displayedYear = CURRENT_YEAR;
            cboYears.setSelectedItem(displayedYear);
            
            tempSelectedDate.set(displayedYear, displayedMonth, 
                    TODAY.get(Calendar.DATE));
            
            setMonthDays();
        }
        
        if (e.getSource() == btnOK)
        {
            selectedDate.set(tempSelectedDate.get(Calendar.YEAR),
                    tempSelectedDate.get(Calendar.MONTH), 
                    tempSelectedDate.get(Calendar.DATE));
            
            exitScreen = true;
            
            setVisible(false);
        }
        
        if (e.getSource() == btnCancel)
        {
            exitScreen = true;
            
            setVisible(false);
        }
        
        if (e.getSource() == cboMonths)
        {
            displayedMonth = cboMonths.getSelectedIndex();
            setMonthDays();
        }
        
        if (e.getSource() == cboYears)
        {
            Integer dispYr = (Integer)cboYears.getSelectedItem();
            displayedYear = dispYr.intValue();
            
            setMonthDays();
        }
    }
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.JButton btnToday;
    private javax.swing.JComboBox cboMonths;
    private javax.swing.JComboBox cboYears;
    private javax.swing.JPanel pnlButtons;
    private javax.swing.JPanel pnlDays;
    private javax.swing.JPanel pnlMonthDisp;
    private javax.swing.JPanel pnlMonthYear;
    private javax.swing.JPanel pnlWeekdays;
    // End of variables declaration                   
    
    private static final int MAX_CAL_DAYS = 42;
    private JLabel lblDays[] = new JLabel[MAX_CAL_DAYS];
    
    // Variables having to do with date management
    
    /** Holds month names.  Calendar.MONTH constant is equal to month name position. */
    private static final String[] MONTH_NAMES = {"January", "February", "March",
        "April", "May", "June", "July", "August", "September", "October", 
        "November", "December"};
    
    private static final int MAX_MONTHS = 12;
    
    /** Holds day names. */
    private static final String[] DAY_NAMES = {"Sun", "Mon", "Tue", "Wed", 
        "Thu", "Fri", "Sat"};
    
    private static final int MAX_WEEKDAYS = 7;
    private JLabel lblWeekdays[] = new JLabel[MAX_WEEKDAYS];
    
    /** Maximum number of years to plan meals ahead.  Currently set to 2. */
    private static final int MAX_YEARS = 2;
    
    private static Calendar TODAY = Calendar.getInstance();
    
    private static int CURRENT_MONTH = TODAY.get(Calendar.MONTH);
    private static int CURRENT_YEAR = TODAY.get(Calendar.YEAR);
    
    private static Calendar selectedDate = Calendar.getInstance();
    
    private int displayedMonth;
    private int displayedYear;
    
    /** Holds the selected date until user commits it with OK button. */
    private Calendar tempSelectedDate = Calendar.getInstance();
    
    // Variables having to do with display
    private static java.awt.Color TODAY_FONT_COL = java.awt.Color.BLUE;
    
    private static java.awt.Font TODAY_FONT = new java.awt.Font("Arial", 
            java.awt.Font.BOLD, 12);
    
    private static Font NORM_DAY_FONT = new Font("Arial", Font.PLAIN, 12);
    private static Color NORM_DAY_FONT_COL = Color.BLACK;
    
    private static Font DAY_NAME_FONT = new Font("Arial", Font.BOLD, 12);
    
    private static java.awt.Color NORM_DAY_BG = java.awt.Color.WHITE;
    
    private static java.awt.Color SEL_DAY_BG = java.awt.Color.LIGHT_GRAY;
    
    // determine if screen's been exited
    private boolean exitScreen;
}
 
 
 
// PopupCalBtn
 
import javax.swing.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
 
public class PopupCalBtn extends JButton
{
    private static SimpleDateFormat DATE_FORMAT;
    
    private static PopupCal POPUP_CAL = new PopupCal(null);
    
    private String formattedDate;
    
    /**
     * Sets the displayed formatted date along with storing the
     * date format and the formatted date for future reference.
     * 
     * @param date
     * @param df
     */
    public PopupCalBtn(Calendar date, SimpleDateFormat df)
    {
        DATE_FORMAT = df;
        
        Date formatDate = date.getTime();
        
        formattedDate = df.format(formatDate);
        
        setText(formattedDate);
    }
    
    /**
     * Calls overloading constructor with current date/time and the
     * passed <code>SimpleDateFormat</code>.
     * 
     * @param df
     */
    public PopupCalBtn(SimpleDateFormat df)
    {
        this(Calendar.getInstance(), df);
    }
    
    @Override
    protected void fireActionPerformed( ActionEvent e ) {
	String newDate = POPUP_CAL.selectDate(formattedDate, DATE_FORMAT);
	if ( newDate == null )
        {
	    return;
        }
	setDate( newDate );
    }
    
    /**
     * Changes the date displayed on the button.
     * 
     * @param newDate
     */
    public void setDate(String newDate)
    {
        setText(formattedDate);
    }
}
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: sizemorew
Solution Provided By: girionis
Participating Experts: 1
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by girionis
Expert Comment by girionis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by sizemorew
Author Comment by sizemorew:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by girionis
Expert Comment by girionis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by sizemorew
Author Comment by sizemorew:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by girionis
Expert Comment by girionis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by girionis
Expert Comment by girionis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by sizemorew
Author Comment by sizemorew:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by sizemorew
Author Comment by sizemorew:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by girionis
Expert Comment by girionis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by girionis
Expert Comment by girionis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by sizemorew
Author Comment by sizemorew:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by girionis
Expert Comment by girionis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by sizemorew
Author Comment by sizemorew:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by girionis
Expert Comment by girionis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by sizemorew
Author Comment by sizemorew:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by girionis
Expert Comment by girionis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by girionis
Expert Comment by girionis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by sizemorew
Author Comment by sizemorew:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by girionis
Accepted Solution by girionis:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Open Discussion
Open Discussion
 
Comment by girionis
Thanks for accepting, glad I was of help.
 
 
20080723-EE-VQP-34 / EE_QW_2_20070628