|
JDateChooser - календарь
#33551792
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
Участник
Откуда: Гостья из будущего
Сообщения: 441
|
|
Есть поле-клендарик, созданное на сонове JDateChooser
Часто бывает так, что пользователям надо сохранить в базе дату - null.
Как-то надо изменить код в JDateChooser, чтобы поле можно было оставлять пустым, и считывать с него null.
По-всякому пробывала, но если добиваюсь, того что надо, перестает что-то другое работать.
Может кто занимался с этим календариком и может что-нибудь подсказать.
Буду признательна за дельные советы.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54.
import jcalendar.src.com.toedter.calendar.JDateChooser;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
public class Calendar_new extends JFrame
{
public static void main (String[] args)
{
Calendar_new f = new Calendar_new();
f.setVisible(true);
}
public Calendar_new()
{
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
final JDateChooser dat = new JDateChooser(true);
JButton submit = new JButton("Ok");
submit.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.out.println( new SimpleDateFormat("dd.MM.yyyy").format(dat.getDate()));
//System.out.println("ftf="+dat.getFtfValue());
}
});
JButton cancel = new JButton("cancel");
cancel.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setVisible(false);
System.exit( 0 );
}
}
);
JPanel okPanel = new JPanel();
okPanel.add(submit);
okPanel.add(cancel);
getContentPane().add(dat, BorderLayout.NORTH);
getContentPane().add(okPanel, BorderLayout.SOUTH);
setTitle(" Календарик");
pack();
setLocation( 300 , 300 );
setVisible(true);
}
}
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271. 272. 273. 274. 275. 276. 277. 278. 279. 280. 281. 282. 283. 284. 285. 286. 287. 288. 289. 290. 291. 292. 293. 294. 295. 296. 297. 298. 299. 300. 301. 302. 303. 304. 305. 306. 307. 308. 309. 310. 311. 312. 313. 314. 315. 316. 317. 318. 319. 320. 321. 322. 323. 324. 325. 326. 327. 328. 329. 330. 331. 332. 333. 334. 335. 336. 337. 338. 339. 340. 341. 342. 343. 344. 345. 346. 347. 348. 349. 350. 351. 352. 353. 354. 355. 356. 357. 358. 359. 360. 361. 362. 363. 364. 365. 366. 367. 368. 369. 370. 371. 372. 373.
/*
* JDateChooser.java - A bean for choosing a date
* Copyright (C) 2004 Kai Toedter
* kai@toedter.com
* www.toedter.com
*/
package jcalendar.src.com.toedter.calendar;
import com.toedter.calendar.JCalendar;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* A date chooser containig a date spinner and a button, that makes a JCalendar visible for
* choosing a date.
*
* @author Kai Toedter
* @version 1.2.2
*/
public class JDateChooser extends JPanel implements ActionListener, PropertyChangeListener,
ChangeListener {
protected JButton calendarButton;
protected JSpinner dateSpinner;
protected JSpinner.DateEditor editor;
protected JCalendar jcalendar;
protected JPopupMenu popup;
protected SpinnerDateModel model;
protected String dateFormatString;
protected boolean dateSelected;
protected boolean isInitialized;
protected Date lastSelectedDate;
protected boolean startEmpty;
protected Locale locale;
protected boolean initialized;
// protected Locale l;
/**
* Creates a new JDateChooser object.
*/
public JDateChooser() {
this ( null , null , false, null );
}
/**
* Creates a new JDateChooser object.
*
* @param icon the new icon
*/
public JDateChooser(ImageIcon icon) {
this ( null , null , false, icon);
}
/**
* Creates a new JDateChooser object.
*
* @param startEmpty true, if the date field should be empty
*/
public JDateChooser( boolean startEmpty) {
this ( null , null , startEmpty, null );
}
/**
* Creates a new JDateChooser object with given date format string. The default date format
* string is "MMMMM d, yyyy".
*
* @param dateFormatString the date format string
* @param startEmpty true, if the date field should be empty
*/
public JDateChooser(String dateFormatString, boolean startEmpty) {
this ( null , dateFormatString, startEmpty, null );
}
/**
* Creates a new JDateChooser object from a given JCalendar.
*
* @param jcalendar the JCalendar
*/
public JDateChooser(JCalendar jcalendar) {
this (jcalendar, null , false, null );
}
/**
* Creates a new JDateChooser.
*
* @param jcalendar the jcalendar or null
* @param dateFormatString the date format string or null (then "MMMMM d, yyyy" is used)
* @param startEmpty true, if the date field should be empty
* @param icon the icon or null (then an internal icon is used)
*/
public JDateChooser(JCalendar jcalendar, String dateFormatString, boolean startEmpty,
ImageIcon icon) {
if (jcalendar == null ) {
jcalendar = new JCalendar();
}
this .jcalendar = jcalendar;
if (dateFormatString == null ) {
dateFormatString = "dd.MM.yyyy";
}
this .dateFormatString = dateFormatString;
this .startEmpty = startEmpty;
setLayout( new BorderLayout());
jcalendar.getDayChooser().addPropertyChangeListener( this );
jcalendar.getDayChooser().setAlwaysFireDayProperty(true); // always fire "day" property even if the user selects the already selected day again
model = new SpinnerDateModel();
/*
The 2 lines below were moved to the setModel method.
model.setCalendarField(java.util.Calendar.WEEK_OF_MONTH);
model.addChangeListener(this);
*/
// Begin Code change by Mark Brown on 24 Aug 2004
setModel(model);
dateSpinner = new JSpinner(model) {
public void setEnabled( boolean enabled) {
super .setEnabled(enabled);
calendarButton.setEnabled(enabled);
}
};
dateSpinner.setFont( new Font("VERNADA", Font.PLAIN, 12 ));
dateSpinner.setForeground( new Color( 0 , 0 , 102 ));
// End Code change by Mark Brown
String tempDateFortmatString = "";
if (!startEmpty) {
tempDateFortmatString = dateFormatString;
}
editor = new JSpinner.DateEditor(dateSpinner, tempDateFortmatString);
dateSpinner.setEditor(editor);
add(dateSpinner, BorderLayout.CENTER);
// Display a calendar button with an icon
if (icon == null ) {
URL iconURL = getClass().getResource("images/JDateChooserIcon.gif");
icon = new ImageIcon(iconURL);
}
calendarButton = new JButton(icon);
calendarButton.setMargin( new Insets( 0 , 0 , 0 , 0 ));
calendarButton.addActionListener( this );
// Alt + 'C' selects the calendar.
calendarButton.setMnemonic(KeyEvent.VK_C);
add(calendarButton, BorderLayout.EAST);
calendarButton.setMargin( new Insets( 0 , 0 , 0 , 0 ));
popup = new JPopupMenu() {
public void setVisible( boolean b) {
Boolean isCanceled = ( Boolean ) getClientProperty(
"JPopupMenu.firePopupMenuCanceled");
if (b || (!b && dateSelected) ||
((isCanceled != null ) && !b && isCanceled.booleanValue())) {
super .setVisible(b);
}
}
};
popup.setLightWeightPopupEnabled(true);
popup.add(jcalendar);
lastSelectedDate = model.getDate();
isInitialized = true;
}
/**
* Called when the jalendar button was pressed.
*
* @param e the action event
*/
public void actionPerformed(ActionEvent e) {
int x = calendarButton.getWidth() - ( int ) popup.getPreferredSize().getWidth();
int y = calendarButton.getY() + calendarButton.getHeight();
Calendar calendar = Calendar.getInstance();
calendar.setTime(model.getDate());
jcalendar.setCalendar(calendar);
popup.show(calendarButton, x, y);
dateSelected = false;
}
/**
* Listens for a "date" property change or a "day" property change event from the JCalendar.
* Updates the dateSpinner and closes the popup.
*
* @param evt the event
*/
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("day")) {
dateSelected = true;
popup.setVisible(false);
setDate(jcalendar.getCalendar().getTime());
setDateFormatString(dateFormatString);
} else if (evt.getPropertyName().equals("date")) {
setDate((Date) evt.getNewValue());
}
}
/**
* Updates the UI of itself and the popup.
*/
public void updateUI() {
super .updateUI();
if (jcalendar != null ) {
SwingUtilities.updateComponentTreeUI(popup);
}
}
/**
* Sets the locale.
*
* @param l The new locale value
*/
public void setLocale(Locale locale) {
if (!initialized) {
super .setLocale(locale);
} else {
this .locale = locale;
super .setLocale(locale);
//init();
}
}
/** public void setLocale(Locale l) {
dateSpinner.setLocale(l);
editor = new JSpinner.DateEditor(dateSpinner, dateFormatString);
dateSpinner.setEditor(editor);
jcalendar.setLocale(l);
}*/
/**
* Gets the date format string.
*
* @return Returns the dateFormatString.
*/
public String getDateFormatString() {
return dateFormatString;
}
/**
* Sets the date format string. E.g "MMMMM d, yyyy" will result in "July 21, 2004" if this is
* the selected date and locale is English.
*
* @param dateFormatString The dateFormatString to set.
*/
public void setDateFormatString(String dateFormatString) {
this .dateFormatString = dateFormatString;
editor.getFormat().applyPattern(dateFormatString);
invalidate();
}
/**
* Returns "JDateChooser".
*
* @return the name value
*/
public String getName() {
return "JDateChooser";
}
/**
* Returns the date.
*
* @return the current date
*/
public Date getDate() {
return model.getDate();
}
/**
* Sets the date. Fires the property change "date".
*
* @param date the new date.
*/
public void setDate(Date date) {
model.setValue(date);
if (getParent() != null ) {
getParent().validate();
}
}
/**
* Fires property "date" changes, recting on the spinner's state changes.
*
* @param e the change event
*/
public void stateChanged(ChangeEvent e) {
if (isInitialized) {
firePropertyChange("date", lastSelectedDate, model.getDate());
lastSelectedDate = model.getDate();
}
}
/*
* The methods:
* public JSpinner getSpinner()
* public SpinnerDateModel getModel()
* public void setModel(SpinnerDateModel mdl)
*
* were added by Mark Brown on 24 Aug 2004. They were added to allow the setting
* of the SpinnerDateModel from a source outside the JDateChooser control. This
* was necessary in order to allow the JDateChooser to be integrated with applications
* using persistence frameworks like Oracle's ADF/BC4J.
*/
/**
* Return this controls JSpinner control.
*
* @return the JSpinner control
*/
public JSpinner getSpinner() {
return dateSpinner;
}
/**
* Return the SpinnerDateModel associated with this control.
*
* @return the SpinnerDateModel
*/
public SpinnerDateModel getModel() {
return model;
}
/**
* Set the SpinnerDateModel for this control. This method allows the JDateChooser
* control to be used with some persistence frameworks (ie. Oracle ADF) to bind the
* control to the database Date value.
*
* @param mdl the SpinnerDateModel
*/
public void setModel(SpinnerDateModel mdl) {
model = mdl;
model.setCalendarField(java.util.Calendar.WEEK_OF_MONTH);
model.addChangeListener( this );
// Begin Code change by Martin Pietruschka on 16 Sep 2004
if (dateSpinner != null )
dateSpinner.setModel(model);
// End Code change by Mark Brown
}
}
|
|
|