Java basics-controls for handling date issues in Java

Source: Internet
Author: User

In a project R & D, you need to select the date and time controls. There are not many items available on the Internet and the quality is average,
So I had to do it myself. I referred to the DateChooser control for a file Jave on the Internet.

The current Code encapsulates the Date and Time Selection in a button component,
It should be easy to use and support Application and Applet

Import java. util. Date;
Import java. util. Calendar;
Import java. text. DateFormat;
Import java. text. SimpleDateFormat;
Import java. text. ParseException;
Import java. awt. Color;
Import java. awt. Font;
Import java. awt. Point;
Import java. awt. Dimension;
Import java. awt. BorderLayout;
Import java. awt. FlowLayout;
Import java. awt. GridLayout;
Import java. awt. Component;
Import java. awt. Cursor;
Import java. awt. Frame;

Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. awt. event. MouseListener;
Import java. awt. event. MouseAdapter;
Import java. awt. event. MouseEvent;
// Import javax. swing. JFrame;
Import javax. swing. JButton;
Import javax. swing. JDialog;
Import javax. swing. JPanel;
Import javax. swing. JLabel;
Import javax. swing. JSpinner;
Import javax. swing. JSpinner. NumberEditor;
Import javax. swing. SpinnerNumberModel;
Import javax. swing. SwingUtilities;
Import javax. swing. SwingConstants;
Import javax. swing. event. ChangeListener;
Import javax. swing. event. ChangeEvent;
Import javax. swing. border. LineBorder;



Public class DateChooserJButton extends JButton {


Private DateChooser dateChooser = null;

Private String preLabel = "";


Public DateChooserJButton (){
This (getNowDate ());
}


Public DateChooserJButton (SimpleDateFormat df, String dateString ){
This ();
SetText (df, dateString );
}


Public DateChooserJButton (Date date ){
This ("", date );
}

Public DateChooserJButton (String preLabel, Date date ){
If (preLabel! = Null) this. preLabel = preLabel;
SetDate (date );
SetBorder (null );
SetCursor (new Cursor (Cursor. HAND_CURSOR ));
Super. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
If (dateChooser = null) dateChooser = new DateChooser ();
Point p = getLocationOnScreen ();
P. y = p. y + 30;
DateChooser. showDateChooser (p );
}
});
}


Private static Date getNowDate (){
Return Calendar. getInstance (). getTime ();
}

Private static SimpleDateFormat getdefadatedateformat (){
Return new SimpleDateFormat ("MM dd, yyyy ");
}

// Override the parent class Method
Public void setText (String s ){
Date date;
Try {
Date = getdefadatedateformat (). parse (s );
} Catch (ParseException e ){
Date = getNowDate ();
}
SetDate (date );
}


Public void setText (SimpleDateFormat df, String s ){
Date date;
Try {
Date = df. parse (s );
} Catch (ParseException e ){
Date = getNowDate ();
}
SetDate (date );
}



Public void setDate (Date date ){
Super. setText (preLabel + getdefadatedateformat (). format (date ));
}

Public Date getDate (){
String dateString = getText (). substring (preLabel. length ());
Try {
Return getdefadatedateformat (). parse (dateString );
} Catch (ParseException e ){
Return getNowDate ();
}

}



// Override the parent class method to make it invalid
Public void addActionListener (ActionListener listener ){
}




Private class DateChooser extends JPanel implements ActionListener, ChangeListener {
Int startYear = 1980; // The default value "minimum" indicates the year.
Int lastYear = 2050; // The default [maximum] displays the year.
Int width = 200; // interface width
Int height = 200; // The page height.

Color backGroundColor = Color. gray; // background Color
// Calendar table color ----------------//
Color palletTableColor = Color. white; // calendar base Color
Color todayBackColor = Color. orange; // Today's background Color
Color weekFontColor = Color. blue; // weekly text Color
Color dateFontColor = Color. black; // Date text Color
Color weekendFontColor = Color. red; // weekend text Color

// Control bar color ------------------//
Color controlLineColor = Color. pink; // control Bar background Color
Color controlTextColor = Color. white; // control bar label text Color


Color rbFontColor = Color. white; // RoundBox text Color
Color rbBorderColor = Color. red; // RoundBox border Color
Color rbButtonColor = Color. pink; // RoundBox button Color
Color rbBtFontColor = Color. red; // RoundBox button text Color

JDialog dialog;
JSpinner yearSpin;
JSpinner monthSpin;
JSpinner hourSpin;
JButton [] [] daysButton = new JButton [6] [7];


DateChooser (){

SetLayout (new BorderLayout ());
SetBorder (new LineBorder (backGroundColor, 2 ));
SetBackground (backGroundColor );

JPanel topYearAndMonth = createYearAndMonthPanal ();
Add (topYearAndMonth, BorderLayout. NORTH );
JPanel centerWeekAndDay = createWeekAndDayPanal ();
Add (centerWeekAndDay, BorderLayout. CENTER );

}


Private JPanel createYearAndMonthPanal (){
Calendar c = getCalendar ();
Int currentYear = c. get (Calendar. YEAR );
Int currentMonth = c. get (Calendar. MONTH) + 1;
Int currentHour = c. get (Calendar. HOUR_OF_DAY );

JPanel result = new JPanel ();
Result. setLayout (new FlowLayout ());
Result. setBackground (controlLineColor );

YearSpin = new JSpinner (new SpinnerNumberModel (currentYear, startYear, lastYear, 1 ));
YearSpin. setPreferredSize (new Dimension (48, 20 ));
YearSpin. setName ("Year ");
YearSpin. setEditor (new JSpinner. NumberEditor (yearSpin ,"####"));
YearSpin. addChangeListener (this );
Result. add (yearSpin );

JLabel yearLabel = new JLabel ("year ");
YearLabel. setForeground (controlTextColor );
Result. add (yearLabel );

MonthSpin = new JSpinner (new SpinnerNumberModel (currentMonth, 1, 12, 1 ));
MonthSpin. setPreferredSize (new Dimension (35,20 ));
MonthSpin. setName ("Month ");
MonthSpin. addChangeListener (this );
Result. add (monthSpin );

JLabel monthLabel = new JLabel ("month ");
MonthLabel. setForeground (controlTextColor );
Result. add (monthLabel );

HourSpin = new JSpinner (new SpinnerNumberModel (currentHour, 1 ));
HourSpin. setPreferredSize (new Dimension (35,20 ));
HourSpin. setName ("Hour ");
HourSpin. addChangeListener (this );
Result. add (hourSpin );

JLabel hourLabel = new JLabel ("Hour ");
HourLabel. setForeground (controlTextColor );
Result. add (hourLabel );

Return result;
}



Private JPanel createWeekAndDayPanal (){
String colname [] = {"day", "1", "2", "3", "4", "5", "6 "};
JPanel result = new JPanel ();
// Set a fixed font to avoid calling environment changes affecting the appearance of the interface
Result. setFont (new Font ("", Font. PLAIN, 12 ));
Result. setLayout (new GridLayout (7, 7 ));
Result. setBackground (Color. white );
JLabel cell;

For (int I = 0; I <7; I ++ ){
Cell = new JLabel (colname [I]);
Cell. setHorizontalAlignment (JLabel. RIGHT );
If (I = 0 | I = 6) cell. setForeground (weekendFontColor );
Else cell. setForeground (weekFontColor );
Result. add (cell );
}

Int actionCommandId = 0;
For (int I = 0; I <6; I ++)
For (int j = 0; j <7; j ++ ){
JButton numberButton = new JButton ();
NumberButton. setBorder (null );
NumberButton. setHorizontalAlignment (SwingConstants. RIGHT );
NumberButton. setActionCommand (String. valueOf (actionCommandId ));
NumberButton. addActionListener (this );
NumberButton. setBackground (palletTableColor );
NumberButton. setForeground (dateFontColor );
If (j = 0 | j = 6) numberButton. setForeground (weekendFontColor );
Else numberButton. setForeground (dateFontCol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.