Results 1 to 6 of 6
  1. #1
    Marcin is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Current Day in Calendar

    I have a problem with setting a colorful background in "current day of month" button in Calendar. I used many ways to do this but the result is always the same. It's really irritating. I tried to get an effect by this code below:

    Java Code:
      GregorianCalendar cal = new GregorianCalendar(year, month, 1);
     
        int dayOfWeek = cal.get(java.util.GregorianCalendar.DAY_OF_WEEK);
        int today = cal.get(java.util.GregorianCalendar.DAY_OF_MONTH);
        int mies = month;
    		
    	
            int rok = year;
            
        
        int daysInMonth = cal.getActualMaximum(java.util.GregorianCalendar.DAY_OF_MONTH);
    
        for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++) {
          button[x].setText("" + day);
        if (button[x]!=null && mies==month && rok==year && Integer.parseInt(button[x].toString())==today) {
       
         
           button[x].setBackground(Color.blue);
       
       
        }}

    But there is something wrong. I also tried many many other ways but i cant get what i want - colorful current day button. Could someone give me the solution?

    Full class code:

    Java Code:
    class DatePicker {
     
        GregorianCalendar cal = new GregorianCalendar();
        
        int month = cal.get(GregorianCalendar.MONTH);
     int da = cal.get(java.util.GregorianCalendar.DAY_OF_MONTH);
        int year = java.util.GregorianCalendar.getInstance().get(java.util.GregorianCalendar.YEAR);;
      JLabel l = new JLabel("", JLabel.CENTER);
      String day = "";
      JDialog d;
      JButton[] button = new JButton[49];
    
      public DatePicker(JFrame parent) {
        d = new JDialog();
        d.setModal(true);
        String[] header = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };
        JPanel p1 = new JPanel(new GridLayout(7, 7));
        p1.setPreferredSize(new Dimension(430, 120));
    
        for (int x = 0; x < button.length; x++) {
          final int selection = x;
          button[x] = new JButton();
          button[x].setFocusPainted(false);
          button[x].setBackground(Color.white);
          if (x > 6) {
            button[x].addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent ae) {
                day = button[selection].getActionCommand();
                d.dispose();
              }
            });
          }
          if (x < 7) {
            button[x].setText(header[x]);
            button[x].setForeground(Color.red);
          }
          p1.add(button[x]);
        }
        JPanel p2 = new JPanel(new GridLayout(1, 3));
        
        // Previous month button
        JButton previous = new JButton("<< Previous");
        previous.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            month--;
            displayDate();
          }
        });
        p2.add(previous);
        
        // Current month label between the previous and next buttons
        p2.add(l);
        
        // Next month button
        JButton next = new JButton("Next >>");
        next.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            month++;
            displayDate();
          }
        });
        p2.add(next);
        
        d.add(p1, BorderLayout.CENTER);
        d.add(p2, BorderLayout.SOUTH);
        d.pack();
        d.setLocationRelativeTo(parent);
        displayDate();
        d.setVisible(true);
      }
    
      public void displayDate() {
        for (int x = 7; x < button.length; x++) {
          button[x].setText("");
        }
    	
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMMM yyyy");
        GregorianCalendar cal = new GregorianCalendar(year, month, 1);
     
        int dayOfWeek = cal.get(java.util.GregorianCalendar.DAY_OF_WEEK);
        int today = cal.get(java.util.GregorianCalendar.DAY_OF_MONTH);
        int mies = month;
    		
    	
            int rok = year;
            
        
        int daysInMonth = cal.getActualMaximum(java.util.GregorianCalendar.DAY_OF_MONTH);
    
        for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++) {
          button[x].setText("" + day);
        if (button[x]!=null && mies==month && rok==year && Integer.parseInt(button[x].toString())==today) {
       
         
           button[x].setBackground(Color.blue);
       
       
        }}
      
        l.setText(sdf.format(cal.getTime()));
        d.setTitle("Date Picker");
      }
    
      public String setPickedDate() {
        if (day.equals("")) {
          return day;
        }
      
        // Set the return date format
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy");
    		
        GregorianCalendar cal = new GregorianCalendar();
        cal.set(year, month, Integer.parseInt(day));
        return sdf.format(cal.getTime());
      }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Current Day in Calendar

    i cant get what i want - colorful current day button
    So, how do you run that code? And what happens when you run it?

  3. #3
    Marcin is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default Re: Current Day in Calendar

    I have Main File also. When it runs i have normal calendar and everything is ok, i can change month/year by next/prev buttons . Every day button is created thanks to array button[], but when i want to make "today button" background colorful, then nothing happens. I'm sure that my "IF instruction" isnt correct but i dont know what instruction I should write to have e.g blue "today button".
    Last edited by Marcin; 12-14-2011 at 10:56 PM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Current Day in Calendar


  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Current Day in Calendar

    I'll leave the discussion to the other thread.

    @OP: You were asked there how the program was being run. It's the same deal if you ask again, "I have a Main File" isn't enough. We really need a precise idea of how this code is being run.

    I suggest you post your latest effort there.

    Java Code:
    if (button[x]!=null && mies==month && rok==year && Integer.parseInt(button[x].toString())==today) {
        
          
           button[x].setBackground(Color.blue);
        
        
        }}
    But to get the best help describe things precisely and accurately. It just isn't true that "nothing happens" when you run this code. Something rather important happens and it might be the starting point for your finding a solution to the problem.
    Last edited by pbrockway2; 12-14-2011 at 11:16 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Current Day in Calendar

    The logic and values of variables makes no sense when you look at lines 84 and 87 and then at line 94.

Similar Threads

  1. Replies: 6
    Last Post: 12-12-2011, 02:23 AM
  2. Calendar now = Calendar.getInstance()
    By volkvanmyn25 in forum New To Java
    Replies: 3
    Last Post: 10-31-2011, 11:25 PM
  3. Replies: 4
    Last Post: 09-21-2011, 05:39 PM
  4. Applet - get current URL
    By Dennis in forum Advanced Java
    Replies: 4
    Last Post: 06-13-2010, 02:26 PM
  5. calendar with week numbers of a current month
    By sridharnr in forum Java Servlet
    Replies: 1
    Last Post: 03-27-2009, 12:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •