MontyMax
MontyMax

Reputation: 105

Java calendar grid layout

I'm trying to get the month of April calendar to get the day count started. So far all I have is a pretty generic calendar that starts with one at the beggining of the grid. How do I set it up so that the calendar matches the first day of the April (Tuesday). Here is my code so far:

package Exercise15_5;
import java.awt.*;
import javax.swing.*;
import java.util.*;

public class Exercise15_5 extends JFrame {
    public Exercise15_5(){
        //Create panel with gridlayout
        JPanel calendar = new JPanel(new BorderLayout());
        calendar.setLayout(new GridLayout(6,7));
        //Add headers
        String[] headers = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        for(int i = 0; i <7; i++){
            calendar.add(new JLabel("" + headers[i]));
        }
        // Add days to calendar, use String.valueOf
        Calendar calendar1 = new GregorianCalendar(2014,4, 13);
        for (int i = 1; i < 31; i++) {
          calendar.add(new JLabel(String.valueOf(i)));
        }

        JPanel monthHeader = new JPanel(new BorderLayout());
        monthHeader.add(new JTextField("\t\t\t04/2014"),    BorderLayout.NORTH);

        monthHeader.add(calendar, BorderLayout.CENTER);



        add(monthHeader);
    }

    public static void main(String[] args) {
        Exercise15_5 frame = new Exercise15_5();
        frame.setTitle("Exercise 15_5");
        frame.setSize(600,300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


    }

}

Upvotes: 1

Views: 1963

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201399

Here is one approach, it uses the this call in a constructor (to default the date), it uses Calendar and it uses the correct GridLayout to be dynamic -

public Exercise15_5() {
  this(new java.util.Date()); // <-- default to the current Date.
}

public Exercise15_5(java.util.Date date) {
  // Create panel with gridlayout
  JPanel calendar = new JPanel(new GridLayout(0, 7)); // <-- Use the Layout ONCE
  // Add headers
  String[] headers = { "Sunday", "Monday",
      "Tuesday", "Wednesday", "Thursday", "Friday",
      "Saturday" };
  // Just use for-each on the headers.
  for (String header : headers) {
    calendar.add(new JLabel(header));
  }

  Calendar c = Calendar.getInstance();
  c.setTime(date); // <-- EDIT: Set the calendar to the passed in date!
  c.set(Calendar.DAY_OF_MONTH, 1);
  // EDIT: fixes flaw with start date.
  int skipDays = 0;
  switch (c.get(Calendar.DAY_OF_WEEK)) {
  case Calendar.MONDAY:
    skipDays = 1;
    break;
  case Calendar.TUESDAY:
    skipDays = 2;
    break;
  case Calendar.WEDNESDAY:
    skipDays = 3;
    break;
  case Calendar.THURSDAY:
    skipDays = 4;
    break;
  case Calendar.FRIDAY:
    skipDays = 5;
    break;
  case Calendar.SATURDAY:
    skipDays = 6;
    break;
  default:
  }
  for (int i = 0; i < skipDays; i++) {
    calendar.add(new JLabel(""));
  }
  int maxDay = c.getMaximum(Calendar.DAY_OF_MONTH);
  for (int i = 1; i < maxDay; i++) {
    calendar.add(new JLabel(String.valueOf(i)));
  }

  JPanel monthHeader = new JPanel(new BorderLayout());
  int mm = 1 + c.get(Calendar.MONTH);
  int year = c.get(Calendar.YEAR);
  monthHeader.add(
      new JTextField(String.format("\t\t\t%d/%d",
          mm, year)), BorderLayout.NORTH);

  monthHeader.add(calendar, BorderLayout.CENTER);

  add(monthHeader);
}

Upvotes: 2

Braj
Braj

Reputation: 46841

Try this code to find out the week day of 1st of current month.

    SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.US);

    Calendar calendar12 = Calendar.getInstance();
    calendar12.set(Calendar.DAY_OF_MONTH, 1);
    String weekDay = dayFormat.format(calendar12.getTime());
    System.out.println(weekDay);

Output for this month

Tuesday

Now on the basis of weekDay you can place your JLabels.


Sample code:

add below code after adding header week names but before adding actual dates .

    for(String header:headers){
        if(header.equals(weekDay)){
           break; 
        }else{
            calendar.add(new JLabel());
        }
    }

Upvotes: 0

Related Questions