private void setUp() { GridBagConstraints c = new GridBagConstraints(); calendar = new GregorianCalendar(); previousMonth = new JButton("<"); // previousMonth.setPreferredSize(GUI_CONSTANTS.miniCalendarNextDimension); nextMonth = new JButton(">"); // nextMonth.setPreferredSize(GUI_CONSTANTS.miniCalendarNextDimension); heading = new JLabel("", JLabel.LEFT); heading.setFont(GUI_CONSTANTS.bigFont); Utils.setGridBagConstraints(c, 0, 0, 1, 1, GridBagConstraints.HORIZONTAL); add(previousMonth, c); GridBagConstraints cHeading = new GridBagConstraints(); cHeading.weightx = 1.0f; cHeading.weighty = 1.0f; Utils.setGridBagConstraints(cHeading, 1, 0, 5, 1, GridBagConstraints.HORIZONTAL); add(heading, cHeading); Utils.setGridBagConstraints(c, 6, 0, 1, 1, GridBagConstraints.HORIZONTAL); add(nextMonth, c); refreshCalendar(); previousMonth.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { addActiveMonth(-1); } }); nextMonth.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { addActiveMonth(1); } }); }
private void refreshCalendar() { if (monthCalendar != null) { monthCalendar.removeAll(); remove(monthCalendar); revalidate(); repaint(); } monthCalendar = new JPanel(); GridBagConstraints c = new GridBagConstraints(); c.weightx = 1.0; c.weighty = 1.0; Utils.setGridBagConstraints(c, 0, 1, 7, 1, GridBagConstraints.HORIZONTAL); add(monthCalendar, c); monthCalendar.setLayout(new GridLayout(0, 7)); int numberOfDays = calendar.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); GregorianCalendar g = (GregorianCalendar) calendar.clone(); g.set(GregorianCalendar.DATE, 1); int weekStartDelay; /* Here we assume the week starts on a monday. TODO: support for different locales */ switch (g.get(GregorianCalendar.DAY_OF_WEEK)) { case GregorianCalendar.MONDAY: weekStartDelay = 1; break; case GregorianCalendar.TUESDAY: weekStartDelay = 2; break; case GregorianCalendar.WEDNESDAY: weekStartDelay = 3; break; case GregorianCalendar.THURSDAY: weekStartDelay = 4; break; case GregorianCalendar.FRIDAY: weekStartDelay = 5; break; case GregorianCalendar.SATURDAY: weekStartDelay = 6; break; case GregorianCalendar.SUNDAY: weekStartDelay = 7; break; default: weekStartDelay = 0; break; } int activeWeekColorCountdown = 0; GregorianCalendar endActiveWeek = (GregorianCalendar) Client.activeWeek.clone(); endActiveWeek.add(GregorianCalendar.DATE, 7); int labelCounter = 0; for (int i = 1; i < weekStartDelay; i++) { JLabel blank = new JLabel(" "); monthCalendar.add(blank); // adding empty JLabels for padding // because I have no idea how to // actually use Swing :3 labelCounter++; } for (int i = 1; i <= numberOfDays; i++) { if (Client.activeWeek.get(GregorianCalendar.DATE) == i && Client.activeWeek.get(GregorianCalendar.MONTH) == calendar.get(GregorianCalendar.MONTH) && Client.activeWeek.get(GregorianCalendar.YEAR) == calendar.get(GregorianCalendar.YEAR)) { switch (Client.activeWeek.get(GregorianCalendar.DAY_OF_WEEK)) { case GregorianCalendar.MONDAY: activeWeekColorCountdown = 7; break; case GregorianCalendar.TUESDAY: activeWeekColorCountdown = 6; break; case GregorianCalendar.WEDNESDAY: activeWeekColorCountdown = 5; break; case GregorianCalendar.THURSDAY: activeWeekColorCountdown = 4; break; case GregorianCalendar.FRIDAY: activeWeekColorCountdown = 3; break; case GregorianCalendar.SATURDAY: activeWeekColorCountdown = 2; break; case GregorianCalendar.SUNDAY: activeWeekColorCountdown = 1; break; default: activeWeekColorCountdown = 0; break; } } JLabel day = new JLabel("" + i); if (activeWeekColorCountdown > 0) { day.setBackground(new Color(0.7f, 0.85f, 0.94f)); activeWeekColorCountdown--; } day.setPreferredSize(GUI_CONSTANTS.miniCalendarViewLabelDimension); day.setOpaque(true); monthCalendar.add(day); labelCounter++; day.addMouseListener( mouseListenerGenerator( calendar.get(GregorianCalendar.YEAR), calendar.get(GregorianCalendar.MONTH), i)); } /* for (int i = 0; i <= labelCounter; i++) { JLabel blank = new JLabel(" "); if(activeWeekColorCountdown>0){ blank.setBackground(Color.BLUE); activeWeekColorCountdown--; } monthCalendar.add(blank); //adding empty JLabels for padding } */ revalidate(); repaint(); monthCalendar.repaint(); }