title: GUI Calendar DefaultTableModel Addig MouseListeners
body: <p>So I followed a tutorial about how to create a GUI Calendar, but now I want to take it a step further. I would like to be able to click on the individual days in the calendar in order to prompt the user to add an event. But I don't know how to achieve this using DefaultTableModel. Could someone please help me out?</p>
  
  <p>Here is the code I have so far:</p>
  
  <p>Main:</p>
  
  <pre><code>enum MONTHS
  {
      January, February, March, April, May, June, July, August, September, October, November, December;
  }
  enum DAYS
  {
      Su, Mo, Tu, We, Th, Fr, Sa;
  }
  
  public class MyCalendarTester implements java.io.Serializable {
  
      static JTable tblCalendar;
      //static JComboBox cmbYear;
      static JFrame frmMain;
      static Container panel;
      static DefaultTableModel mtblCalendar; //Table model
      static JScrollPane stblCalendar; //The scrollpane
      static JPanel pnlCalendar;
      static int realYear, realMonth, realDay, currentYear, currentMonth;
  
      static MONTHS[] arrayOfMonths = MONTHS.values();
      static DAYS[] arrayOfDays = DAYS.values();
      static MyCalendar myCal = new MyCalendar();
      static Event myEvent = new Event(null, null, null);
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          GregorianCalendar cal = new GregorianCalendar(); // capture today
  
          //initialize calendar values
          currentMonth = cal.get(Calendar.MONTH);
          currentYear = cal.get(Calendar.YEAR);
          realYear = cal.get(Calendar.YEAR);
          realMonth = cal.get(Calendar.MONTH);
          realDay = cal.get(Calendar.DATE);
  
  
          //Look and feel that matches users Operating System
          try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
          catch (ClassNotFoundException e) {}
          catch (InstantiationException e) {}
          catch (IllegalAccessException e) {}
          catch (UnsupportedLookAndFeelException e) {}
  
          //Prepare frame
          frmMain = new JFrame ("My Calendar"); //Create frame
          frmMain.setSize(400, 400); //Set size to 400x400 pixels
          panel = frmMain.getContentPane(); //Get content panel
          panel.setLayout(null); //Apply null layout
          frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Close when X is clicked
  
          //Create controls
          mtblCalendar = new DefaultTableModel(){public boolean isCellEditable(int rowIndex, int mColIndex){return false;}}; // prevents the dates from being edited
          tblCalendar = new JTable(mtblCalendar);
          stblCalendar = new JScrollPane(tblCalendar);
          pnlCalendar = new JPanel(null);
  
          //Set border
          pnlCalendar.setBorder(BorderFactory.createTitledBorder(Integer.toString(cal.get(Calendar.YEAR))));
  
          //Add controls to pane
                  panel.add(pnlCalendar);
  
                  pnlCalendar.add(stblCalendar);
  
                  //Set bounds
                  pnlCalendar.setBounds(0, 0, 320, 335);
                  stblCalendar.setBounds(10, 50, 300, 250);
                  frmMain.setResizable(false); // do not allow resizing
                  frmMain.setVisible(true);//Make frame visible
  
  
          printCalendar(cal);
          MyCalendar.monthView(cal.get(Calendar.MONTH), cal.get(Calendar.YEAR));
  
  
          System.out.println("\nI think we're done here!");
      }
  
      /**
       * displays the calendar for this month and highlights todays date
       * @param cal the instance of todays calendar
       */
      public static void  printCalendar(Calendar cal){
  
          GregorianCalendar dayCounter = new GregorianCalendar(); // capture today
          dayCounter.set(Calendar.DAY_OF_MONTH, 1);
  
          for (int i = 0; i &lt; 7; i++){
              mtblCalendar.addColumn(arrayOfDays[i]); // add headers for days of the week
          }
  
          tblCalendar.getParent().setBackground(tblCalendar.getBackground()); //Set white background
  
          //No resize/reorder
          tblCalendar.getTableHeader().setResizingAllowed(false);
          tblCalendar.getTableHeader().setReorderingAllowed(false);
  
          //Single cell selection
          tblCalendar.setColumnSelectionAllowed(true);
          tblCalendar.setRowSelectionAllowed(true);
          tblCalendar.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  
          //Set row/column count
          tblCalendar.setRowHeight(38);
          mtblCalendar.setColumnCount(7);
          mtblCalendar.setRowCount(6);
  }
  </code></pre>
  
  <p>Calendar Class Code</p>
  
  <pre><code>public class MyCalendar{
  
      GregorianCalendar calendar;
      private static HashMap&lt;GregorianCalendar, Event&gt; myCalHash;
      GregorianCalendar dayCounter = new GregorianCalendar(); // capture today
      static MONTHS[] arrayOfMonths = MONTHS.values();
      static DAYS[] arrayOfDays = DAYS.values();
      static JButton btnPrev, btnNext;
      static JLabel lblMonth = new JLabel ("January");;
  
      MyCalendar(){
          calendar = new GregorianCalendar();
          myCalHash = new HashMap&lt;GregorianCalendar, Event&gt;();
      }
  
      public HashMap&lt;GregorianCalendar, Event&gt; getMyCalHash(){
          return myCalHash;
      }
  
      public void setMyCalHash(HashMap&lt;GregorianCalendar, Event&gt; myCalHash) {
          this.myCalHash = myCalHash;
      }
  
          public static void monthView(int month, int year){
              //print month calendar
  
              int nod, som; //Number Of Days, Start Of Month
  
              //Clear table
              for (int i = 0; i &lt; 6; i++){
                  for (int j = 0; j &lt; 7; j++){
                      MyCalendarTester.mtblCalendar.setValueAt(null, i, j);
                  }
              }
  
              btnPrev = new JButton ("&lt;&lt;");
              btnNext = new JButton ("&gt;&gt;");
  
              //Register action listeners
              btnPrev.addActionListener(new btnPrev_Action());
              btnNext.addActionListener(new btnNext_Action());
  
              btnPrev.setBounds(10, 25, 50, 25);
              btnNext.setBounds(260, 25, 50, 25);
  
              //Allow/disallow buttons
              btnPrev.setEnabled(true);
              btnNext.setEnabled(true);
              System.out.println(arrayOfMonths[month].toString());
              lblMonth.setText(arrayOfMonths[month].toString()); //Refresh the month label (at the top)
              lblMonth.setBounds(160-lblMonth.getPreferredSize().width/2, 25, 180, 25); //Re-align label with calendar
              MyCalendarTester.pnlCalendar.add(lblMonth);
              MyCalendarTester.pnlCalendar.add(btnPrev);
              MyCalendarTester.pnlCalendar.add(btnNext);
  
              lblMonth.setBounds(160-lblMonth.getPreferredSize().width/2, 25, 100, 25); //places the calendar in the center
  
              //Get first day of month and number of days
              GregorianCalendar cal = new GregorianCalendar(year, month, 1);
              nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
              som = cal.get(GregorianCalendar.DAY_OF_WEEK);
  
              //Draw calendar
              for (int i=1; i&lt;=nod; i++){
                  int row = new Integer((i+som-2)/7);
                  int column  =  (i+som-2)%7;
                  MyCalendarTester.mtblCalendar.setValueAt(i, row, column);
  
              }
  
              //Apply renderers
              MyCalendarTester.tblCalendar.setDefaultRenderer(MyCalendarTester.tblCalendar.getColumnClass(0), new tblCalendarRenderer());
  
          }
  
          static class tblCalendarRenderer extends DefaultTableCellRenderer{
              public Component getTableCellRendererComponent (JTable table, Object value, boolean selected, boolean focused, int row, int column){
                  super.getTableCellRendererComponent(table, value, selected, focused, row, column);
                  if (column == 0 || column == 6){ //Week-end
                      setBackground(new Color(255, 220, 220));
                  }
                  else{ //Week
                      setBackground(new Color(255, 255, 255));
                  }
                  if (value != null){
                      if (Integer.parseInt(value.toString()) == MyCalendarTester.realDay &amp;&amp; 
                              MyCalendarTester.currentMonth == MyCalendarTester.realMonth &amp;&amp; MyCalendarTester.currentYear == MyCalendarTester.realYear){ //Today
                          setBackground(new Color(220, 220, 255));
                      }
                  }
                  setBorder(null);
                  setForeground(Color.black);
                  return this;  
              }
          }
  
          static class btnPrev_Action implements ActionListener{
              public void actionPerformed (ActionEvent e){
                  if (MyCalendarTester.currentMonth == 0){ //Back one year
                      MyCalendarTester.currentMonth = 11;
                      MyCalendarTester.currentYear -= 1;
                  }
                  else{ //Back one month
                      MyCalendarTester.currentMonth -= 1;
                  }
                  monthView(MyCalendarTester.currentMonth, MyCalendarTester.currentYear);
              }
          }
          static class btnNext_Action implements ActionListener{
              public void actionPerformed (ActionEvent e){
                  if (MyCalendarTester.currentMonth == 11){ //Forward one year
                      MyCalendarTester.currentMonth = 0;
                      MyCalendarTester.currentYear += 1;
                  }
                  else{ //Foward one month
                      MyCalendarTester.currentMonth += 1;
                  }
                  monthView(MyCalendarTester.currentMonth, MyCalendarTester.currentYear);
              }
          }
  </code></pre>