Swing calendar Control

Source: Internet
Author: User
Tags getcolor
Package com;/*** calendar control * @ author Lucifer. wang */import Java. AWT. borderlayout; import Java. AWT. color; import Java. AWT. component; import Java. AWT. dimension; import Java. AWT. flowlayout; import Java. AWT. toolkit; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. AWT. event. keyevent; import Java. AWT. event. windowadapter; import Java. AWT. event. using wevent; import Java. util. calendar; I Mport Java. util. date; import javax. swing. abstractaction; import javax. swing. inputmap; import javax. swing. jbutton; import javax. swing. jcombobox; import javax. swing. jcomponent; import javax. swing. jdialog; import javax. swing. jframe; import javax. swing. jpanel; import javax. swing. jspinner; import javax. swing. jtable; import javax. swing. keystroke; import javax. swing. listselectionmodel; import javax. swing. spinnernumbe Rmodel; import javax. swing. swingconstants; import javax. swing. uimanager; import javax. swing. event. changeevent; import javax. swing. event. changelistener; import javax. swing. event. listselectionevent; import javax. swing. event. listselectionlistener; import javax. swing. table. defaulttablecellrenderer; import javax. swing. table. defaulttablemodel; public class calendarpanel extends jdialog {private jcombobox combobo Xmonth; private jspinner spinneryear; private jtable2 table; private jtablemodel2 tablemodel; private calendar; private string [] days = {"sun", "mon", "Tur ", "wed", "Thu", "fri", "sat"}; private string [] months = {"January", "February", "March", "CMDL ", "may", "June", "July", "August", "September", "October", "November", "December"}; private int year =-1; private int month =-1; private int da Te =-1; private int oldyear =-1; private int oldmonth =-1; private int olddate =-1; private Boolean flag = true; private jbutton okbutton; private jbutton cancelbutton; public calendarpanel (jframe frame) {super (frame, true); this. settitle ("select date"); Calendar = calendar. getinstance (); initcomponent (); setsize (400,450); Center (); setvisible (true);} public calendarpanel (calendar) {super (); this. calenda R = calendar; initcomponent (); setsize (400,450); Center (); setvisible (true);} public calendarpanel (date) {super (); this. calendar = calendar. getinstance (); this. calendar. settime (date); initcomponent (); setsize (400,450); Center (); setvisible (true);} public calendarpanel (INT year, int month, int date) {super (); this. calendar = calendar. getinstance (); Calendar. set (calendar. year, year); Calendar. set (calendar. Month, month-1); Calendar. set (calendar. date, date); oldyear = calendar ar. get (calendar. year); oldmonth = calendar. get (calendar. month); olddate = calendar. get (calendar. date); initcomponent (); setsize (400,450); Center (); setvisible (true);} public void Center () {dimension screensize = toolkit. getdefatooltoolkit (). getscreensize (); dimension framesize = getsize (); If (framesize. height> screensize. height) {f Ramesize. height = screensize. height;} If (framesize. width> screensize. width) {framesize. width = screensize. width;} super. setlocation (screensize. width-framesize. width)/2, (screensize. height-framesize. height)/2);} public calendar getcalendar () {return calendar;} Public String getcalendarbydate (string tag) {stringbuffer strret = new stringbuffer (); If (calendar! = NULL) {oldyear = calendar. get (calendar. year); oldmonth = calendar. get (calendar. month); olddate = calendar. get (calendar. date); If (olddate <10) strret. append ("0" + olddate); elsestrret. append (olddate); strret. append (TAG); If (oldmonth <10) strret. append ("0" + oldmonth); elsestrret. append (oldmonth); strret. append (TAG + oldyear); Return strret. tostring ();} return NULL;} public date getcalendarbydate () {If (Cal Endar! = NULL) return calendar. gettime (); return NULL;} private void initcomponent () {oldyear = calendar. get (calendar. year); oldmonth = calendar. get (calendar. month); olddate = calendar. get (calendar. date); // This. setborder (New titledborder ("date"); this. getcontentpane (). setlayout (New borderlayout (); setnorthpanel (); setcenterpanel (); setsouthpanel (); eventlistenter (); setdata ();} private void setdata () {year = calen Dar. get (calendar. year); month = calendar. get (calendar. month); Date = calendar. get (calendar. date); comboboxmonth. setselectedindex (month); spinneryear. setvalue (New INTEGER (year);} private void eventlistenter () {spinneryear. addchangelistener (New changelistener () {public void statechanged (changeevent e) {year = integer. parseint (string. valueof (spinneryear. getvalue (); refreshtable () ;}}); comboboxmonth. addac Tionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {month = comboboxmonth. getselectedindex (); refreshtable () ;}}); table. getselectionmodel (). addlistselectionlistener (New listselectionlistener () {public void valuechanged (listselectionevent e) {If (table. getselectedrow ()>-1 & table. getselectedcolumn ()>-1) {date = integer. parseint (string. valueof (table. getvalueat (table. gets Electedrow (), table. getselectedcolumn (); Calendar. set (calendar. year, year); Calendar. set (calendar. month, month); Calendar. set (calendar. date, date) ;}}); abstractaction right = (New abstractaction () {/*****/Private Static final long serialversionuid =-342988%0%305492l; public void action=med (actionevent e) {If (table. getcolumncount () = table. getselectedcolumn () + 1) {table. changeselection (Table. getselectedrow () + 1, 0, false, false);} else {table. changeselection (table. getselectedrow (), table. getselectedcolumn () + 1, false, false) ;}}); abstractaction left = (New abstractaction () {/*****/Private Static final long serialversionuid =-2487040789339112435l; public void actionreceivmed (actionevent e) {If (0> table. getselectedcolumn ()-1) {table. changeselection (table. getselectedrow ()- 1, table. getcolumncount ()-1, false, false);} else {table. changeselection (table. getselectedrow (), table. getselectedcolumn ()-1, false, false) ;}}); inputmap = table. getinputmap (jcomponent. when_ancestor_of_focused_component); keystroke = keystroke. getkeystroke (keyevent. vk_right, 0); table. getactionmap (). put (inputmap. get (keystroke), right); keystroke = keystroke. getkeystroke (keyeven T. vk_left, 0); table. getactionmap (). put (inputmap. get (keystroke), left); okbutton. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {/** calendar. set (calendar. year, integer. parseint (string *. valueof (spinneryear. getvalue (); * Calendar. set (calendar. month, * comboboxmonth. getselectedindex (); * Calendar. set (calendar. date, integer. parseint (string *. valueof (table. get Valueat (table. getselectedrow (), table *. getselectedcolumn (); oldyear = * Calendar. get (calendar. year); oldmonth = * Calendar. get (calendar. month); olddate = * Calendar. get (calendar. date); * joptionpane. showmessagedialog (calendarpanel. this, "OK, select * date is" + calendarpanel. this. getcalendarbydate (); */setvisible (false) ;}}); cancelbutton. addactionlistener (New actionlistener () {public void Ctionreceivmed (actionevent e) {/** calendar. set (calendar. year, oldyear); * Calendar. set (calendar. month, oldmonth); * Calendar. set (calendar. date, olddate); * joptionpane. showmessagedialog (calendarpanel. this, "cancel, * date is" + calendarpanel. this. getcalendarbydate (); */Calendar = NULL; setvisible (false) ;}}); addwindowlistener (New windowadapter () {public void windowclosing (windowevent e) {calenda R = NULL; super. windowclosing (e) ;}});} private void refreshtable () {for (INT I = tablemodel. getrowcount ()-1; I> 0; I --) {tablemodel. removerow (I);} calendar. set (calendar. year, year); Calendar. set (calendar. month, month); int maxdate = calendar ar. getactualmaximum (calendar. date); Calendar. set (calendar. date, 1); int startday = calendar. get (calendar. day_of_week); For (INT I = 0; I <6; I ++) {tablemodel. addrow (New object [7]);} int selectrow =-1; int selectcolumn =-1; for (INT I = 0; I <maxdate; I ++) {int M = (INT) (startday-1)/7) + 1; int n = (startday-1) % 7; tablemodel. setvalueat (string. valueof (I + 1), m, n); startday = startday + 1; if (date! =-1 & date = I + 1) {selectrow = m; selectcolumn = n ;}} table. changeselection (selectrow, selectcolumn, false, false);} private void setsouthpanel () {jpanel Panel = new jpanel (); panel. setlayout (New flowlayout (flowlayout. right); okbutton = new jbutton ("OK"); panel. add (okbutton); cancelbutton = new jbutton ("cancel"); panel. add (cancelbutton); this. getcontentpane (). add (panel, borderlayout. south);} private void setcenterpanel () {table = new jtable2 (); tablemodel = new jtablemodel2 (); tablemodel. setcolumnidentifiers (days); tablemodel. addrow (days); table. setmodel (tablemodel); For (INT I = 0; I <7; I ++) {table. getcolumnmodel (). getcolumn (I ). setcellrenderer (New defaulttablecellrenderer2 ();} This. getcontentpane (). add (table, borderlayout. center);} private void setnorthpanel () {jpanel Panel = new jpanel (); panel. setlayout (New flowlayout (); comboboxmonth = new jcombobox (); For (INT I = 0; I <months. length; I ++) {comboboxmonth. additem (months [I]);} comboboxmonth. setselectedindex (-1); Panel. add (comboboxmonth); roloverspinnerlistmodel numbermodel = new roloverspinnerlistmodel (1980,198 0, 2099, 1); spinneryear = new jspinner (numbermodel); jspinner. numbereditor = new jspinner. numbereditor (spinneryear, "####"); spinneryear. seteditor (numbereditor); panel. add (spinneryear); this. getcontentpane (). add (panel, borderlayout. north);} public static void main (string [] ARGs) {jframe frame = new jframe (); calendarpanel = new calendarpanel (FRAME); // frame. getcontentpane (). add (calendarpanel, borderlayout. center); calendarpanel. setsize (400,450); // calendarpanel. setdefaclocloseoperation (jframe. exit_on_close); calendarpanel. setvisible (true);} protected void validatetree () {super. validatetree (); If (FLAG) {table. requestfocus (); flag = false ;}} class roloverspinnerlistmodel extends spinnernumbermodel {/*****/Private Static final long serialversionuid = random; Public roloverspinnerlistmodel (INT value, int minimum, int maximum, int stepsize) {super (value, minimum, maximum, stepsize);} public object getnextvalue () {object returnvalue = super. getnextvalue (); If (returnvalue = NULL) {returnvalue = super. getminimum ();} return returnvalue;} public object getpreviusvalue () {object returnvalue = super. getpreviusvalue (); If (returnvalue = NULL) {returnvalue = super. getmaximum () ;}return returnvalue ;}} class jtable2 extends jtable {/*****/Private Static final long serialversionuid = 2092799354481792316l; Public jtable2 () {This. gettableheader (). setreorderingallowed (false); this. gettableheader (). setresizingallowed (false); this. setselectionmode (listselectionmodel. single_selection); this. setcellselectionenabled (true); this. setgridcolor (color. white);} public void changeselection (INT rowindex, int columnindex, Boolean toggle, Boolean extend) {If (this. getvalueat (rowindex, columnindex) = NULL) {return;} If (rowindex = 0) {return;} super. changeselection (rowindex, columnindex, toggle, extend);} public void repaint (int x, int y, int width, int height) {super. repaint (X, Y, width, height); If (height> 0) {maketablerowheight () ;}} protected void validatetree () {super. validatetree (); maketablerowheight ();} private void maketablerowheight () {int rowheight = (INT) (this. getheight ()/7) * 0.6); If (rowheight> 0) {This. setrowheight (0, rowheight);} rowheight = (INT) (this. getheight ()-rowheight)/6); If (rowheight> 0) {for (INT I = 1; I <8; I ++) {This. setrowheight (I, rowheight) ;}}} class jtablemodel2 extends ululttablemodel {/***/Private Static final long serialversionuid = 3655520869200784738l; Public Boolean iscelleditable (INT row, int column) {return false ;}} class extends defaulttablecellrenderer {/*****/Private Static final long serialversionuid =-inline; public component gettablecellrenderercomponent (jtable table, object value, Boolean isselected, boolean hasfocus, int row, int column) {component Component = super. gettablecellrenderercomponent (table, value, isselected, hasfocus, row, column); this. sethorizontalalignment (swingconstants. center); If (ROW = 0) {component. setbackground (uimanager. getcolor ("table. focuscellforeground "); component. setforeground (uimanager. getcolor ("table. focuscellbackground "); return component;} else {If (isselected) {component. setbackground (uimanager. getcolor ("table. focuscellforeground "); component. setforeground (uimanager. getcolor ("table. focuscellbackground ");} else {component. setbackground (uimanager. getcolor ("table. focuscellbackground "); component. setforeground (uimanager. getcolor ("table. focuscellforeground ");} If (column = 0 | column = 6) {component. setforeground (color. red) ;}} return component ;}}

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.