hello everybody ^^
I have to make a calander ,and to let the user to insert any event !
the calander it self is working <<but I don't know how can I write the code to give the user the ability to enter a new event by entering some fields like (title,location,date ..),and when I click on any day >>it should show me what events are in that day !
I've tried different things but it doesn't work <<and I don't know how to solve that<<
plz help me :))
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class CalendarPro extends JFrame {
private JButton B[],B_Prevoius,B2_Next;
private String Names_Days[]={"Sat","Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};
private JLabel L_Days[],L1_HeaderMonth,L2_Time;
private GridLayout G1,G2;
private Container C;
private JPanel P1,P2;
private JTextArea T1;
private MyThread th;
private String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
private int nod, som;
private GregorianCalendar cal;
private int realYear, realMonth, realDay, currentYear, currentMonth;
//Variables
public CalendarPro (){
super ("Calendar");
setLayout (null);
// setBorder(BorderFactory.createTitledBorder("Calendar"));
cal = new GregorianCalendar(); //Create calendar
realDay = cal.get(GregorianCalendar.DAY_OF_MONTH); //Get day
realMonth = cal.get(GregorianCalendar.MONTH); //Get month
realYear = cal.get(GregorianCalendar.YEAR); //Get year
currentMonth = realMonth; //Match month and year
currentYear = realYear;
B=new JButton [43];
B_Prevoius=new JButton ("<<");
B2_Next=new JButton (">>");
L_Days=new JLabel[7];
L1_HeaderMonth=new JLabel("January");
L2_Time=new JLabel();
G1=new GridLayout(1,7);
G2=new GridLayout(6,7);
C=getContentPane ();
C.setBackground(Color.white);
P1=new JPanel();
P2=new JPanel();
P1.setLayout (G1);
P2.setLayout (G2);
T1=new JTextArea("Your Events in this day :");
th=new MyThread ();
Button Handler1=new Button ();
for (int i=0;i<L_Days.length;i++){
L_Days[i]=new JLabel(Names_Days[i]);
L_Days[i].setForeground(new Color (255, 175, 175));
L_Days[i].setFont(new Font ("Monospaced",Font.PLAIN +Font.BOLD,16));
P1.add(L_Days[i]);
}
for (int j=1;j<B.length;j++){
B[j]=new JButton ("");
B[j].setForeground(Color.BLACK);
B[j].setBackground(Color.lightGray);
B[j].setBorder(new LineBorder(Color.PINK));
B[j].addActionListener(Handler1);
P2.add(B[j]);
}
L2_Time.setSize(175, 30);
L2_Time.setLocation(5,5);
L2_Time.setBorder(new LineBorder(Color.BLACK));
L2_Time.setForeground(Color.PINK);
add(L2_Time);
B_Prevoius.setSize(50, 30);
B_Prevoius.setLocation(5,65);
B_Prevoius.setForeground(Color.BLACK);
B_Prevoius.setBackground(Color.PINK);
add (B_Prevoius);
B2_Next.setSize(50, 30);
B2_Next.setLocation(350,65);
B2_Next.setForeground(Color.black);
B2_Next.setBackground(Color.PINK);
add (B2_Next);
L1_HeaderMonth.setSize(165, 30);
L1_HeaderMonth.setLocation(155,65);
L1_HeaderMonth.setForeground(Color.PINK);
L1_HeaderMonth.setFont(new Font ("Monospaced",Font.PLAIN +Font.BOLD,18));
add (L1_HeaderMonth);
P1.setSize(350, 30);
P1.setLocation(35,100);
add (P1);
P2.setSize(350, 300);
P2.setLocation(35,135);
add (P2);
T1.setSize(350, 100);
T1.setLocation(35,440);
T1.setBorder(new LineBorder(Color.BLACK));
add (T1);
B_Prevoius.addActionListener(Handler1);
B2_Next.addActionListener(Handler1);
refreshCalendar (realMonth, realYear); //Refresh calendar
}//constructer
private class MyThread extends Thread {
public MyThread (){
super ();
start();
}
public void run (){
while (true){
try {
Thread .sleep(1000);
cal = new GregorianCalendar(); //Create calendar
L2_Time.setText(cal.getTime().toString());
}
catch (InterruptedException E){
System.err.println (E.toString());
}}}
}//innerClass
private class Button implements ActionListener{
public void actionPerformed (ActionEvent e){
String event = ((JButton) e.getSource()).getText();
if (e.getSource()== B2_Next){
if (currentMonth == 11){ //Foward one year
currentMonth = 0;
currentYear += 1;
L1_HeaderMonth.setText((months[currentMonth]+"/"+currentYear));
}
else{ //Foward one month
currentMonth += 1;
}
refreshCalendar(currentMonth, currentYear);
}
else if (e.getSource() == B_Prevoius ){
if (currentMonth == 0){ //Back one year
currentMonth = 11;
currentYear -= 1;
}
else{ //Back one month
currentMonth -= 1;
}
refreshCalendar(currentMonth, currentYear);
}//else if
else {
//Boolean Flag =true;
JButton object=(JButton)e.getSource();
Select(object);
}
}
}
public void refreshCalendar(int month, int year){
int lDay = cal.get(cal.DAY_OF_MONTH);
L1_HeaderMonth.setText((months[month]+"/"+year));
//Clear
for (int m=1;m<B.length;m++){
B[m].setBackground(Color.lightGray);
B[m].setText("");
}
//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);
if ((realMonth == month) && (realYear == year)) {//today
if ((realMonth== month) && (realYear == year) && (realDay == lDay)) {
B[realDay+4].setBackground(Color.DARK_GRAY);
}
}
for (int n=1;n<=nod; n++){
B[som+1].setBackground(new Color (220, 220, 255));
// B[].setBackground(Color.DARK_GRAY);
B[som+n].setText(String.valueOf(n));
}
}
public void Select (JButton o){
for (int m=1;m<B.length;m++){
B[m].setBackground(Color.lightGray);
int lDay = cal.get(cal.DAY_OF_MONTH);
B[som+1].setBackground(new Color (220, 220, 255));
if ((realMonth == currentMonth) && (realYear == currentYear)) {//today
//System.out.println(realMonth+" "+currentMonth+" "+realYear+" "+currentYear);
if ((realMonth== currentMonth) && (realYear == currentYear) && (realDay ==lDay)) {
B[realDay+4].setBackground(Color.DARK_GRAY);
}
}
}
o.setBackground(Color.YELLOW);
}
}
}//OuterClass
-----------------------------------
main code
------------------------------------
import java.awt.Color;
import javax.swing.JFrame;
public class MainTest {
public static void main(String[] args) {
CalendarPro v =new CalendarPro();
v.setSize(450,600);
v.setVisible(true);
v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}