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

    Default How to set current day (today) background color in Calendar?

    I dont know how to set colour current day's button in Calendar.

    Java Code:
          
          if (x==today) {
              button[x].setBackground(Color.blue);
          }
    The code above doesnt work correct. I browsed many webites but dont find any what would help.

    Full code of the class:

    Java Code:
    package lars;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    class DatePicker {
     
        Calendar cal = Calendar.getInstance();
        
        int month = cal.get(Calendar.MONTH);
      int year = cal.get(Calendar.YEAR);;
     int today = cal.get(Calendar.DAY_OF_MONTH);
      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==today) {
              button[x].setBackground(Color.blue);
          }
          
          
          
          
          
          
          
          
          
          
          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");
        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.set(year, month, 1);
        int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);
        int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
    
        for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++) {
          button[x].setText("" + day);
        }
      
        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");
    		
        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.set(year, month, Integer.parseInt(day));
        return sdf.format(cal.getTime());
      }
    }
    Thx for any advise.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: How to set current day (today) background color in Calendar?

    Can you make a SSCCE that compiles and shows the problem.
    Your posted code has no main.

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

    Default Re: How to set current day (today) background color in Calendar?

    There is no problem about it, cause program starts and everything is ok, expect for current day button is not blue higlighted. Other button is blue.

    This Calendar program has two .java files: main and class . The main code.


    Java Code:
    package  lars;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Lars{
      public static void main(String[] args) {
        JLabel label = new JLabel("Selected Date:");
        final JTextField text = new JTextField(20);
        JButton b = new JButton("popup");
        JPanel p = new JPanel();
        p.add(label);
        p.add(text);
        p.add(b);
        final JFrame f = new JFrame();
        f.getContentPane().add(p);
        f.pack();
        f.setVisible(true);
        b.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            text.setText(new DatePicker(f).setPickedDate());
          }
        });
      }
    }
    Last edited by Marcin; 12-12-2011 at 12:40 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: How to set current day (today) background color in Calendar?

    Put this in the action listener method:
    Java Code:
                System.out.println("selection=" + selection);
    and look at what it prints out when you click on the first day (the blue one)

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

    Default Re: How to set current day (today) background color in Calendar?

    then it shows 11, but i know that this code => if (x==today) is wrong cause it counts every button and compare that number with current date of month . The problem is that i dont know how to write that it shows blue today button on the calendar.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: How to set current day (today) background color in Calendar?

    Which button is showing blue? What is its index?

    Try hardcoding a number as index to the button array to set a button blue.
    button[<Test differentNbrshere>].setBackground(Color.blue);

    See what button is set blue. Compare which button is set blue with the date.
    x==today is not the test to determine which button to set.

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

    Default Re: How to set current day (today) background color in Calendar?

    ok thx i will try to do this

Similar Threads

  1. How to change background color
    By Jeffrey4u in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 10-22-2011, 08:32 AM
  2. Changing background color
    By nikkka in forum New To Java
    Replies: 4
    Last Post: 03-12-2011, 05:54 AM
  3. Why does google calendar have a BLUE background in jframe.
    By Mr.StevenFeldman in forum AWT / Swing
    Replies: 2
    Last Post: 03-09-2011, 05:14 AM
  4. background color with jpanel
    By hannerz06 in forum New To Java
    Replies: 6
    Last Post: 03-31-2010, 03:25 AM
  5. calendar with week numbers of a current month
    By sridharnr in forum Java Servlet
    Replies: 1
    Last Post: 03-27-2009, 11:02 AM

Posting Permissions

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