I cant get the code to get The previous month (I am trying to get the previous month first, then I can do the same for the year and for getting the next month / year. I cant use JTable nor can I use Drag and Drop.

I have tried to add an action listener to the button, and making the month -1 but it did not work.

public CalendarLayout () {
    monthm = new JButton("<");
    monthm.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {

              cal.add(Calendar.MONTH, -1);
                updateMonth();
            //    monthPanel(cal);

          }
        });
    monthp = new JButton(">");
    monthp.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            cal.add(Calendar.MONTH, +1);
          }
        });
    yearm = new JButton("<<");
    yearp = new JButton(">>");

    day_name = new JLabel [7];
    dayss = new JButton [42];
    this.monthPanel((GregorianCalendar) cal);   

}

    int getDay(int m)
    {
        GregorianCalendar g1=new GregorianCalendar();
        g1.set(cal.get(cal.YEAR),m, 1);
        return g1.get(g1.DAY_OF_WEEK);

    }
    boolean is31(int m)

    {


        boolean b=false;
        if(m>=0&&m<=6)
        {
            for(int i=1;i<8;i++)
            {
                if(m%2==0)
                {
                    b= true;
                }
            }
        }
        else  {
            for(int i=7;i<12;i++)
            {
                if(m%2==1)
                {
                    b= true;
                }

            }
        }
        return b;
    }
    void monthPanel(GregorianCalendar cl)
    {
        pCal=new JPanel();
        pCal.setLayout(new GridLayout(7,7));
        int month=cl.MONTH+1;
        //System.out.println("left");
        int fd=getDay(month)-1;

        for(int i=0;i<7;i++)
        {
            day_name[i]=new JLabel (days[i]);
            pCal.add(day_name[i]);
        }
        boolean b=is31(month);
        if(month==1)
        {
            int count=0;
            for(int i=fd-1 ;i>=0;i--,count++)
            {
                dayss[count]=new JButton(31-i+"");
                dayss[count].setEnabled(false);

            }
            if(((GregorianCalendar) cal).isLeapYear(cl.YEAR))
            {
                for(int i=1;i<=29;i++,count++)
                {
                    dayss[count]=new JButton(i+"");
                }
            }
            else
            {
                for(int i=1;i<=28;i++,count++)
                {
                    dayss[count]=new JButton(i+"");
                }
            }
            for(int i=count,c=1;i<42;i++,c++,count++)
            {
                dayss[count]=new JButton(c+"");
                dayss[count].setEnabled(false);


            }
        } 
        else if(month==2)
        {

            int count=0;
            for(int i=fd-1 ;i>=0;i--,count++)
            {
                if(((GregorianCalendar) cal).isLeapYear(cl.YEAR))
                {
                    dayss[count]=new JButton(29-i+"");
                    dayss[count].setEnabled(false);
                }
                else
                {
                    dayss[count]=new JButton(28-i+"");
                    dayss[count].setEnabled(false);
                }

            }
            for(int i=1;i<32;i++,count++)
            {
                dayss[count]=new JButton(i+"");
            }
            for(int i=count,c=1;i<42;i++,c++,count++)
            {
                dayss[count]=new JButton(c+"");
                dayss[count].setEnabled(false);

            }

        }
        else if(!b||month==0||month==7||month==0)
        {

                int count=0;
                for(int i=fd-1 ;i>=0;i--,count++)
                {

                    dayss[count]=new JButton(31-i+"");
                    dayss[count].setEnabled(false);
                }
                if(month==7||month==0)
                    for(int i=1;i<32;i++,count++)
                    {
                        dayss[count]=new JButton(i+"");
                    }
                else
                {   
                    for(int i=1;i<31;i++,count++)
                    {
                        dayss[count]=new JButton(i+"");
                    }
                }
                for(int i=count,c=1;i<42;i++,c++,count++)
                {
                    dayss[count]=new JButton(c+"");
                    dayss[count].setEnabled(false);

                }
        }

        else if(month!=1)
        {
            {

                int count=0;
                for(int i=fd-1 ;i>=0;i--,count++)
                {
                    dayss[count]=new JButton(30-i+"");
                    dayss[count].setEnabled(false);

                }
                for(int i=1;i<32;i++,count++)
                {
                    dayss[count]=new JButton(i+"");
                }
                for(int i=count,c=1;i<42;i++,c++,count++)
                {
                    dayss[count]=new JButton(c+"");
                    dayss[count].setEnabled(false);

                }
            }

        }

        for(int i=0;i<42;i++)
        {
            pCal.add(dayss[i]);
        }
        PCalendar.add(pCal,BorderLayout.CENTER);    

}

      void updateMonth() {
            cal.set(Calendar.DAY_OF_MONTH, 1);


            String month = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US);
            int year = cal.get(Calendar.YEAR);
            monthYear.setText(month + " " + year);

         //   int startDay = cal.get(Calendar.DAY_OF_WEEK);
          //  int numberOfDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);


           monthPanel(cal);

      }





          }

I am expecting to get the right GUI for the previous month when pressing the button.

0 Answers