Wednesday, May 4, 2011

[discussion_vu] Fwd: )))))))Vu & Company((((((( IDEA Solution of cs506


import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class CustomCalendar extends JFrame{

private JButton btnPrevMonth;
private JButton btnNextMonth;
private JButton btnPrevYear;
private JButton btnNextYear;
private JButton btnOk;
private JButton btnCancel;
private JComboBox cmbMonth;
private JComboBox cmbYear;
private JTable tblCalendar;
private DefaultTableModel mtblCalendar;
private JScrollPane stblCalendar;
private JSpinner spinner;
private SpinnerDateModel dateModel;

private JPanel panel;
private int realYear, realMonth, realDay, currentYear, currentMonth;

public CustomCalendar(){
initComponents();
}

private void initComponents(){

cmbMonth = new JComboBox();
cmbYear = new JComboBox();
btnPrevMonth = new JButton ("<");
btnNextMonth = new JButton (">");
btnPrevYear = new JButton("<<");
btnNextYear = new JButton(">>");
btnOk = new JButton("Ok");
btnCancel = new JButton("Cancel");
panel = new JPanel(null);
mtblCalendar = new DefaultTableModel(){public boolean isCellEditable(int rowIndex, int mColIndex){return false;}};
tblCalendar = new JTable(mtblCalendar);
stblCalendar = new JScrollPane(tblCalendar);

btnPrevMonth.addActionListener(new BtnPrevMonth_Action());
btnNextMonth.addActionListener(new BtnNextMonth_Action());
btnPrevYear.addActionListener(new BtnPrevYear_Action());
btnNextYear.addActionListener(new BtnNextYear_Action());
btnOk.addActionListener(new BtnOk_Action());
btnCancel.addActionListener(new BtnCancel_Action());
cmbMonth.addActionListener(new CmbMonth_Action());
cmbYear.addActionListener(new CmbYear_Action());

GregorianCalendar cal = new GregorianCalendar();
realDay = cal.get(GregorianCalendar.DAY_OF_MONTH);
realMonth = cal.get(GregorianCalendar.MONTH);
realYear = cal.get(GregorianCalendar.YEAR);
currentMonth = realMonth;
currentYear = realYear;

String[] headers = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
for (int i=0; i<7; i++){
mtblCalendar.addColumn(headers[i]);
}

Date date = new Date();
dateModel = new SpinnerDateModel(date,null,null,GregorianCalendar.HOUR_OF_DAY);
spinner = new JSpinner(dateModel);
JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(spinner,"HH:MM:SS");
spinner.setEditor(dateEditor);

tblCalendar.getParent().setBackground(tblCalendar.getBackground());

tblCalendar.getTableHeader().setResizingAllowed(false);
tblCalendar.getTableHeader().setReorderingAllowed(false);

tblCalendar.setColumnSelectionAllowed(true);
tblCalendar.setRowSelectionAllowed(true);
tblCalendar.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

tblCalendar.setRowHeight(38);
mtblCalendar.setColumnCount(7);
mtblCalendar.setRowCount(6);

String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
for(int i = 0; i < 12; i++){
cmbMonth.addItem(months[i]);
}

for (int i=realYear-100; i<=realYear+100; i++){
cmbYear.addItem(String.valueOf(i));
}

panel.setBorder(BorderFactory.createTitledBorder("Calendar"));

panel.add(btnPrevMonth);
panel.add(btnNextMonth);
panel.add(btnPrevYear);
panel.add(btnNextYear);
panel.add(btnOk);
panel.add(btnCancel);
panel.add(cmbMonth);
panel.add(cmbYear);
panel.add(stblCalendar);
panel.add(spinner);

panel.setBounds(0, 0, 395, 375);
btnPrevMonth.setBounds(10, 25, 45, 25);
cmbMonth.setBounds(55, 25, 90, 25);
btnNextMonth.setBounds(145, 25, 45, 25);
btnPrevYear.setBounds(195, 25, 50, 25);
cmbYear.setBounds(245, 25, 90, 25);
btnNextYear.setBounds(335, 25, 50, 25);
stblCalendar.setBounds(10, 55, 377, 250);
spinner.setBounds(160, 310, 100, 25);
btnOk.setBounds(215,345,85,25);
btnCancel.setBounds(300,345,85,25);

refreshCalendar (realMonth, realYear);

getContentPane().setLayout(null);
getContentPane().add(panel);
//setUndecorated(true);
setResizable(false);
setSize(400,400);
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CustomCalendar().setVisible(true);
}
});
}

public void refreshCalendar(int month, int year){

int nod, som;

btnPrevMonth.setEnabled(true);
btnNextMonth.setEnabled(true);
btnPrevYear.setEnabled(true);
btnNextYear.setEnabled(true);

if (month == 0 && year <= realYear-100){
btnPrevMonth.setEnabled(false);
}
if(year <= realYear-100){
btnPrevYear.setEnabled(false);
}
if (month == 11 && year >= realYear+100){
btnNextMonth.setEnabled(false);
}
if(year >= realYear+100){
btnNextYear.setEnabled(false);
}


cmbMonth.setSelectedIndex(month);
cmbYear.setSelectedItem(String.valueOf(year));

for (int i=0; i<6; i++){
for (int j=0; j<7; j++){
mtblCalendar.setValueAt(null, i, j);
}
}

GregorianCalendar cal = new GregorianCalendar(year, month, 1);
nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
som = cal.get(GregorianCalendar.DAY_OF_WEEK);

for (int i=1; i<=nod; i++){
int row = new Integer((i+som-2)/7);
int column = (i+som-2)%7;
mtblCalendar.setValueAt(i, row, column);
}

tblCalendar.setDefaultRenderer(tblCalendar.getColumnClass(0), new tblCalendarRenderer());
}

private class tblCalendarRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent (JTable table, Object value, boolean selected, boolean focused, int row, int column){
super.getTableCellRendererComponent(table, value, selected, focused, row, column);
if (column == 0 || column == 6){
setBackground(new Color(255, 220, 220));
}
else{
setBackground(new Color(255, 255, 255));
}
if (value != null){
if (Integer.parseInt(value.toString()) == realDay && currentMonth == realMonth && currentYear == realYear){
setBackground(new Color(220, 220, 255));
}
}
setBorder(null);
setForeground(Color.black);
return this;
}
}

private class BtnPrevMonth_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
if (currentMonth == 0){
currentMonth = 11;
currentYear -= 1;
}
else{
currentMonth -= 1;
}
refreshCalendar(currentMonth, currentYear);
}
}

private class BtnNextMonth_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
if (currentMonth == 11){
currentMonth = 0;
currentYear += 1;
}
else{
currentMonth += 1;
}
refreshCalendar(currentMonth, currentYear);
}
}

private class BtnPrevYear_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
if(currentYear > realYear-100){
currentYear -= 1;
}
refreshCalendar(currentMonth, currentYear);
}
}

private class BtnNextYear_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
if(currentYear < realYear+100){
currentYear += 1;
}
refreshCalendar(currentMonth, currentYear);
}
}

private class BtnOk_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
int day = 0;
try{
day = Integer.parseInt(tblCalendar.getValueAt(tblCalendar.getSelectedRow(), tblCalendar.getSelectedColumn()).toString());
} catch (NullPointerException exp){
exp.printStackTrace();
}
if (day > 0){
int month = cmbMonth.getSelectedIndex();
int year = Integer.parseInt(cmbYear.getSelectedItem().toString());
Formatter fmt = new Formatter();
String display = (month+1) + "/" + day + "/" + year + " "+ fmt.format("%tr",spinner.getValue());
JOptionPane.showMessageDialog(null, display);
} else {
JOptionPane.showMessageDialog(null, "Invalid Selection");
}

}
}

private class BtnCancel_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
System.exit(0);
}
}

private class CmbMonth_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
if (cmbMonth.getSelectedItem() != null){
currentMonth = cmbMonth.getSelectedIndex();
refreshCalendar(currentMonth, currentYear);
}
}
}

private class CmbYear_Action implements ActionListener{
public void actionPerformed (ActionEvent e){
if (cmbYear.getSelectedItem() != null){
String b = cmbYear.getSelectedItem().toString();
currentYear = Integer.parseInt(b);
refreshCalendar(currentMonth, currentYear);
}
}
}
}





---------- Forwarded message ----------
From: Raja Safdar Iqbal <mc090406836@vu.edu.pk>
Date: Wed, May 4, 2011 at 2:07 AM
Subject: )))))))Vu & Company((((((( IDEA Solution of cs506
To: vu-and-company@googlegroups.com


see attached file
--
<<< LOVE IS LIFE>>>>

--
You received this message because you are subscribed to the Google Groups "Vu and Company" group.
To post to this group, send email to vu-and-company@googlegroups.com.
To unsubscribe from this group, send email to vu-and-company+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/vu-and-company?hl=en.


--
You received this message because you are subscribed to the Google Groups "Virtual University of Pakistan" group.
To post to this group, send email to discussion_vu@googlegroups.com.
To unsubscribe from this group, send email to discussion_vu+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/discussion_vu?hl=en.

No comments:

Post a Comment

PAID CONTENT