private void addDateLabels ( ) { dateLabels = new ArrayList < JLabel > ( ) ; for ( int i = 0 ; i < 42 ; ++ i ) { int dateLabelColumnX = ( ( i % 7 ) ) + constantFirstDateLabelCell . x ; int dateLabelRowY = ( ( i / 7 ) + constantFirstDateLabelCell . y ) ; JLabel dateLabel = new JLabel ( ) ; dateLabel . setHorizontalAlignment ( SwingConstants . CENTER ) ; dateLabel . setVerticalAlignment ( SwingConstants . CENTER ) ; dateLabel . setBackground ( Color . white ) ; dateLabel . setForeground ( Color . black ) ; dateLabel . setBorder ( null ) ; dateLabel . setOpaque ( true ) ; dateLabel . setText ( "" + i ) ; CellConstraints constraints = CC . xy ( dateLabelColumnX , dateLabelRowY ) ; centerPanel . add ( dateLabel , constraints ) ; dateLabels . add ( dateLabel ) ; // Add a mouse click listener for every date label, even the blank ones. dateLabel . addMouseListener ( new MouseLiberalAdapter ( ) { @ Override public void mouseLiberalClick ( MouseEvent e ) { dateLabelMousePressed ( e ) ; } } ) ; } }
addDateLabels This adds a set of 42 date labels to the calendar and ties each of those labels to a mouse click event handler . The date labels are reused any time that the calendar is redrawn .