Hi everyone,
I can't obtain that this below code display the Calendar. Can someone help me please ascertain where I'm doing wrong?
Many thanks.
There is the code below.
Some line has been obtain from the Net...
PS: Sorry for the "System.out.println..." it's my Debugging method.
/*
* Java Core Packages
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.image.*;
import java.awt.print.*;
import java.io.Serializable;
import java.util.regex.PatternSyntaxException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.net.*;
import java.util.*;
import java.text.NumberFormat;
import java.text.ParseException;
import java.beans.*;
import java.awt.image.*;
import java.awt.Color;
import java.awt.Container;
import java.util.Calendar;
import java.util.Date;
/*
* Java Extension Packages
*/
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.UIManager;
import javax.swing.ImageIcon;
import javax.swing.BorderFactory;
import javax.swing.border.*;
import javax.swing.border.TitledBorder;
/*---------------CalendarComponent Outer Class--------------*
*
*/
public class CalendarComponent extends JInternalFrame
implements Serializable, ActionListener
{
private Label_Handler dayListener;
private JPanel spinerPanel, date_timePanel, gridButtonsPanel,
buttonsPanel, calendar_SpinerPanel,
finalCalendarPanel;
private JButton okButton, cancelButton, helpButton, closeBtn,
submitBtn, clearButton;
private JComboBox monthList;
private static JTabbedPane calendarTabbedPane;
private static JSpinner spinnerYEAR_List;
private SpinnerListModel monthModel = null;
private JFormattedTextField formatedTField = null;
private SpinnerModel yearModel;
private Border myColour, panelEdge;
private TitledBorder panelTitle;
private Font textFont;
private Calendar my_Calendar;
private Icon frameIcon;
private static final int DAY_IN_WEEK = 7;
private static final int MAX_WEEK = 6;
private int currentYear = 0;
private int myIntYear = 0;
private int ctr = 1;
private int ctrLabel = 0;
String[] monthStrings = { "January", "February", "Marsh", "April", "May",
"June", "July", "August", "September", "October",
"November", "December" };
private String monthName = "";
private String yearName = "";
private String dayNumber = "";
private String [] daysLabel = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
private JLabel myDAYS = null;
private JLabel myDayOFWEEK = null;
private JLabel [ ] dayOFWEEK;// = new JLabel [ DAY_IN_WEEK * MAX_WEEK ];
private boolean DEBUG = false;
//----------------------------------
/**
* Intitialisation of CalendarComponent's Constructor.
*/
protected CalendarComponent ( )
{
/**
* Creates a JInternalFrame with the specified title, resizability,
* closability, maximizability, and iconifiability.
* @param " Date & Time Properties " The String to display in the title bar.
*/
super ( " Date & Time Properties ", true, true, true, true );
/**
*
*/
setupCalendarComponent ();
setupCallendarMonth_Year ();
} // End for the CalendarComponent's Constructor.
/**
*
*/
public void setupCalendarComponent ()
{
/**
*
*/
frameIcon = new ImageIcon ( "general/TrayIcon.gif" );
/**
* Instantiates the object Font.
* @param Verdana The Font's name.
* @param 1 The Font's type (Bold).
* @param 12 The Font's size.
*/
textFont = new Font ( "Verdana", 1, 12 );
/**
* Create the combo box, select the item at index 5
*/
monthList = new JComboBox ( monthStrings );
monthList.setSelectedIndex ( 5 );
monthList.setFont ( textFont );
monthList.setForeground ( Colors.MEDIUM_RED );
monthList.addActionListener ( this );
/**
*
*/
my_Calendar = Calendar.getInstance ();
/**
*
*/
currentYear = my_Calendar.get ( Calendar.YEAR );
/**
*
*/
yearModel = new SpinnerNumberModel ( currentYear,
currentYear - 100,
currentYear + 100,
1 );
/**
*
*/
ChangeListener myYearListener = new ChangeListener ( )
{
public void stateChanged ( ChangeEvent ce )
{
yearModel = ( SpinnerModel ) ce.getSource ( ) ;
yearName = ( formatedTField.getText () );
System.out.println ( "First time Initial..." + yearName + "\n" );
/**
*
*/
updateCalendar_Display ( );
}
} ;
yearModel.addChangeListener ( myYearListener );
/**
*
*/
if ( monthModel instanceof CyclingSpinnerListModel )
{
((CyclingSpinnerListModel)monthModel).setLinkedModel(yearModel);
}
/**
*
*/
spinnerYEAR_List = new JSpinner ( yearModel );
/**
*
*/
spinnerYEAR_List.setEditor ( new JSpinner.NumberEditor ( spinnerYEAR_List, "#" ) );
/**
* Tweak the spinner's formatted text field.
*/
formatedTField = getTextField ( spinnerYEAR_List );
/**
*
*/
if ( formatedTField != null )
{
/**
* Specify more width than we need
*/
formatedTField.setColumns ( 8 );
formatedTField.setFont ( textFont );
formatedTField.setForeground ( Colors.MEDIUM_RED );
formatedTField.setHorizontalAlignment ( JTextField.LEFT );
}
/**
*
*/
spinerPanel = new JPanel ();
spinerPanel.setLayout ( new GridLayout ( 1, 2, 6, 6 ) );
spinerPanel.setBackground ( Colors.LIGHT_YELLOW );
spinerPanel.add ( monthList );
spinerPanel.add ( spinnerYEAR_List );
/**
* A border that puts 10 extra pixels at the sides and bottom of each pane.
*/
panelEdge = BorderFactory.createEmptyBorder ( 16,16,16,16 );
/**
*
*/
myColour = BorderFactory.createLineBorder ( Color.blue );
/**
*
*/
panelTitle = BorderFactory.createTitledBorder ( myColour, " Date " );
spinerPanel.setBorder ( panelEdge );
spinerPanel.setBorder ( panelTitle );
//---------------------------------------------
/**
*
*/
JPanel myDayLabel_Panel = new JPanel ();
myDayLabel_Panel.setLayout ( new GridLayout ( 1, 7 ) );
/**
*
*/
JPanel myDay_Panel = new JPanel ();
myDay_Panel.setLayout ( new GridLayout ( MAX_WEEK, DAY_IN_WEEK ) );
myDay_Panel.setBackground ( Colors.LIGHT_YELLOW );
textFont = new Font ( "Verdana", 0, 14 );
/**
*
*/
for ( ctrLabel = 0; ctrLabel <= 6; ctrLabel++ )
{
myDAYS = new JLabel ( );
myDAYS.setText ( daysLabel [ ctrLabel ] );
myDAYS.setFont ( textFont );
myDAYS.setForeground ( Colors.MEDIUM_RED );
myDAYS.setHorizontalAlignment ( SwingConstants.CENTER );
myDayLabel_Panel.add ( myDAYS );
}
//---------------------------------------------
/**
*
*/
Label_Handler dayListener = new Label_Handler ();
dayOFWEEK = new JLabel [ DAY_IN_WEEK * MAX_WEEK ];
/**
*
*/
for ( ctr = 1; ctr <= 31; ctr++ )
{
myDayOFWEEK = new JLabel ( "--" );
dayOFWEEK [ ctr ] = myDayOFWEEK;
dayOFWEEK [ ctr ].setHorizontalAlignment ( SwingConstants.CENTER );
dayOFWEEK [ ctr ].addMouseListener ( dayListener );
myDay_Panel.add ( dayOFWEEK [ ctr ] );
}
/**
*
*/
updateCalendar_Display ( );
/**
*
*/
date_timePanel = new JPanel ();
date_timePanel.setLayout ( new BorderLayout () );
date_timePanel.add ( myDayLabel_Panel, BorderLayout.NORTH );
date_timePanel.add ( myDay_Panel, BorderLayout.CENTER );
//---------------------------------------------
/**
*
*/
okButton = new JButton ( " OK ", new ImageIcon ( "general/ConfirmButton.gif" ) );
okButton.setMargin ( new Insets ( 2, 2, 2, 2 ) );
okButton.setName ( "okButton" );
okButton.setToolTipText ( " Validate the User Choice " );
okButton.addActionListener (
new ActionListener ()
{
public void actionPerformed ( ActionEvent ae )
{
/**
*
*/
}
}
);
//-------------------------------
cancelButton = new JButton ( " Cancel ", new ImageIcon ( "general/CancelButton.gif" ) );
cancelButton.setMargin ( new Insets ( 2, 2, 2, 2 ) );
cancelButton.setToolTipText ( " Exit from the Application " );
cancelButton.setName ( "cancelButton" );
cancelButton.addActionListener (
new ActionListener () {
public void actionPerformed ( ActionEvent ae )
{
/**
* Makes cryptoCAM invisible.
* @param false False to makes cryptoCAM invisible.
*/
setVisible ( false );
dispose ( );
/**
* Runs the garbage collector.
*/
System.gc ();
}
}
);
/**
* Creates a new JPanel with a double buffer and a Grid layout.
*/
gridButtonsPanel = new JPanel ();
/**
* Creates a grid layout with the specified number of rows and columns.
* In addition, the horizontal and vertical gaps are set to the specified values.
* Horizontal gaps are placed between each of the columns.
* Vertical gaps are placed between each of the rows.
* @param 1 the rows, with the value one meaning one number of rows.
* @param 2 the columns, with the value three meaning three number of columns.
* @param 5 the horizontal gap.
* @param 5 the vertical gap.
*/
gridButtonsPanel.setLayout ( new GridLayout ( 1, 2, 5, 5 ) );
gridButtonsPanel.add ( okButton );
gridButtonsPanel.add ( cancelButton );
/**
* Creates a new JPanel with a double buffer and a Grid layout.
*/
buttonsPanel = new JPanel ();
buttonsPanel.setLayout ( new FlowLayout ( 2 ) );
buttonsPanel.add ( gridButtonsPanel );
/**
*
*/
calendar_SpinerPanel = new JPanel ();
calendar_SpinerPanel.setLayout ( new BorderLayout () );
calendar_SpinerPanel.add ( BorderLayout.NORTH, spinerPanel );
calendar_SpinerPanel.add ( BorderLayout.CENTER, date_timePanel );
/**
*
*/
Icon tabbedTabIcon = new ImageIcon ( "general/Calendar16.gif" );
/**
* Creates an empty TabbedPane with the specified tab placement
* @param caesarTabbedPane
*/
calendarTabbedPane = new JTabbedPane ( );
calendarTabbedPane.addTab ( " Date Properties ", tabbedTabIcon,
calendar_SpinerPanel, " Calendar Properties " );
finalCalendarPanel = new JPanel ();
finalCalendarPanel.setLayout ( new BorderLayout () );
finalCalendarPanel.add ( BorderLayout.CENTER, calendarTabbedPane );
finalCalendarPanel.add ( BorderLayout.SOUTH, buttonsPanel );
/**
* Appends a component to the end of this container. Returns the component added.
* @param gridLayoutPanel The Component to add.
*/
getContentPane ().add ( BorderLayout.CENTER, finalCalendarPanel );
} // End for the Method setupCalendarComponent.
//----------------------------------------------
/**
*
*/
public void setupCallendarMonth_Year ()
{
monthName = ( String )monthList.getSelectedItem();
System.out.println ( "First time Initial..." + monthName + "\n" );
yearName = ( formatedTField.getText () );
System.out.println ( "First time Initial..." + yearName + "\n" );
} // End for the Method setupCallendarMonth_Year.
//----------------------------------------------
/**
* Return the formatted text field used by the editor, or
* null if the editor doesn't descend from JSpinner.DefaultEditor.
*/
public JFormattedTextField getTextField(JSpinner spinner) {
JComponent editor = spinner.getEditor();
if (editor instanceof JSpinner.DefaultEditor) {
return ((JSpinner.DefaultEditor)editor).getTextField();
} else {
System.err.println("Unexpected editor type: "
+ spinner.getEditor().getClass()
+ " isn't a descendant of DefaultEditor");
return null;
}
}
//----------------------------------------------
/**
*
*/
public void updateCalendar_Display ( )
{
setVisible ( false );
int month = my_Calendar.get ( Calendar.MONTH );
int year = my_Calendar.get ( Calendar.YEAR );
System.out.println ( " The Month is: " + month + "\n" );
System.out.println ( " The year is: " + year + "\n" );
my_Calendar.set ( year, month, 1 );
int first_Day_Ofset = my_Calendar.get ( Calendar.DAY_OF_WEEK );
System.out.println ( " The Day of Week is: " + first_Day_Ofset + "\n" );
assert Calendar.SUNDAY == 0;
assert first_Day_Ofset < 31;
int ctr = 0;
while ( ctr < first_Day_Ofset-1 )
dayOFWEEK [ ctr ].setText ( "" );
int day_OF_Month = 1;
System.out.println ( " The Day of Month is: " + day_OF_Month + "\n" );
for (; ctr < 31; ++ctr )
{
// Can't get calendar.equals(today) to work, so do it manually
if ( my_Calendar.get( Calendar.MONTH )== my_Calendar.get( Calendar.MONTH )
&& my_Calendar.get( Calendar.YEAR )== my_Calendar.get( Calendar.YEAR )
&& my_Calendar.get( Calendar.DATE )== my_Calendar.get( Calendar.DATE ) )
{
System.out.println ( " The highlight is done here" + "\n" );
// highlight( days[i] );
}
dayNumber = Integer.toString ( day_OF_Month );
System.out.println ( " The dayNumber is: " + dayNumber + "\n" );
myDayOFWEEK = new JLabel ( dayNumber );
dayOFWEEK [ ctr ] = myDayOFWEEK;
my_Calendar.roll ( Calendar.DATE, true );
day_OF_Month = my_Calendar.get ( Calendar.DATE );
System.out.println ( " The Day of Month is: " + day_OF_Month + "\n" );
if ( day_OF_Month == 1 )
break;
}
while ( ++ctr < 31 )
dayOFWEEK [ ctr ].setText ( "" );
setVisible ( true );
} // End for Method updateCalendar_Display.
//----------------------------------------------
/**
*
*/
public void roll(int field, boolean up)
{ my_Calendar.roll(field,up);
updateCalendar_Display ( );
}
/**
*
*/
public void roll(int field, int amount)
{ my_Calendar.roll(field,amount);
updateCalendar_Display ( );
}
//----------------------------------------------
/**
* Listens to the combo box.
*/
public void actionPerformed ( ActionEvent e )
{
JComboBox myCB = ( JComboBox )e.getSource ();
monthName = ( String )myCB.getSelectedItem ();
System.out.println ( monthName + "\n" );
/**
*
*/
updateCalendar_Display ( );
} // End for Method ActionPerformed
//--------Label_Handler-------------------------------------
/**
*
*/
private class Label_Handler implements MouseListener
{
public void mouseClicked ( MouseEvent me )
{
System.out.println ( "mouseClicked Done...\n" );
}
public void mouseEntered ( MouseEvent me )
{
//System.out.println ( "mouseEntered Done...\n" );
}
public void mouseExited ( MouseEvent me )
{
//System.out.println ( "mouseExited Done...\n" );
}
public void mousePressed ( MouseEvent me )
{
//System.out.println ( "mousePressed Done...\n" );
}
public void mouseReleased ( MouseEvent me )
{
//System.out.println ( "mouseReleased Done...\n" );
}
/**
* The class fingerprint that is set to indicate serialization compatibility with a previous version of the class.
*/
private static final long serialVersionUID = 259629918;
} // End for Inner Class Label_Handler.
//---------------------------------------------
/**
* The class fingerprint that is set to indicate serialization compatibility with a previous version of the class.
*/
private static final long serialVersionUID = 243629918;
} // End for Outer Main Class CalendarComponent