Home » Source Code » » JDatePicker.java

JDatePicker.java ( File view )

From:
  • By 2010-08-11
  • View(s):10
  • Download(s):0
  • Point(s): 1
			package com.sunking.swing;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.text.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;
import javax.swing.border.*;
import com.sun.java.swing.plaf.motif.*;
import com.sun.java.swing.plaf.windows.*;

//import java.net.*;

/**
 * <p>Title:OpenSwing </p>
 * <p>Description: JDatePicker 日期选择框<BR>
 * 履历:<BR>
 * 2004/03/26   根据网友caiyj的建议引入了recoon写的关于JDateDocument的校验方法<BR>
 * 2004/04/02   根据网友caiyj提交的BUG,修正了做为TableCellEditor时日期选择面板弹不出问题<BR>
 * </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author <a href="mailto:sunkingxie@hotmail.com"'>Sunking</a>
 * @version 1.0
 */
public class JDatePicker
    extends JComboBox
    implements Serializable {

    /**
     * 年月日格式
     */
    public static final SimpleDateFormat dateFormat
        = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 年月格式
     */
    public static final SimpleDateFormat monthFormat
        = new SimpleDateFormat("yyyy-MM");

    /**
     * 日格式
     */
    public static final SimpleDateFormat dayFormat
        = new SimpleDateFormat("d");

    /**
     * 构造式
     */
    public JDatePicker() {

        setEditable(true);
        JTextField textField = ( (JTextField)this.getEditor().
                                getEditorComponent());
        textField.setDocument(new JDateDocument(textField));
        setSelectedItem(dateFormat.format(new Date()));
    
}

    /**
     * 取得当前选择的日期
     * @return Date
     */
    public Date getSelectedDate() {

        try {

            return dateFormat.parse(this.getSelectedItem().toString());
        
}
        catch (Exception ex) {

            return new Date();
        
}
    
}

    /**
     * 设置当前选择的日期
     * @return Date
     */
    public void setSelectedDate(Date date) {

        setSelectedItem(date);
    
}

    /**
     * 设置当前选取的日期
     * @param item Object
     */
    public void setSelectedItem(Object item) {

        removeAllItems();
        addItem(item);
        super.setSelectedItem(item);
    
}

    /**
     * 更新UI
     */
    public void updateUI() {

        ComboBoxUI cui = (ComboBoxUI) UIManager.getUI(this);
        if (cui instanceof MetalComboBoxUI) {

            cui = new MetalDateComboBoxUI();
        
}
        else if (cui instanceof MotifComboBoxUI) {

            cui = new MotifDateComboBoxUI();
        
}
        else if (cui instanceof WindowsComboBoxUI) {

            cui = new WindowsDateComboBoxUI();
        
}
        setUI(cui);
    
}

    // UI Inner classes -- one for each supported Look and Feel
    /**
     * <p>Title: OpenSwing</p>
     * <p>Description: MetalDateComboBoxUI</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
     * @version 1.0
     */
    class MetalDateComboBoxUI
        extends MetalComboBoxUI {

        protected ComboPopup createPopup() {

            return new DatePopup(comboBox);
        
}
    
}

    /**
     *
     * <p>Title: OpenSwing</p>
     * <p>Description: WindowsDateComboBoxUI</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
     * @version 1.0
     */
    class WindowsDateComboBoxUI
        extends WindowsComboBoxUI {

        protected ComboPopup createPopup() {

            return new DatePopup(comboBox);
        
}
    
}

    /**
     *
     * <p>Title: OpenSwing</p>
     * <p>Description: MotifDateComboBoxUI</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
     * @version 1.0
     */
    class MotifDateComboBoxUI
        extends MotifComboBoxUI {

        protected ComboPopup createPopup() {

            return new DatePopup(comboBox);
        
}
    
}

    /**
     * <p>Title:JDateDocument </p>
     * <p>Description: JDateDocument 实现日期的输入格式限制</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author <a href="mailto:sunkingxie@hotmail.com"'>Sunking</a>,recoon
     * @version 1.0
     */
    public static class JDateDocument
        extends PlainDocument {

        private JTextComponent textComponent; //日期输入文本框
        private int newOffset; //新的插入位置

        /******************************************************************
         ** 函数名称:public JDateDocument(JTextComponent tc)
         ** 功能描述:设置此文本框显示的默认值和格式限制
         ** 入口参数:tc : JTextComponent类型,当前操作的文本框
         ** 返回值:无
         ** 调用者:类JDateDocument
         *******************************************************************/
        public JDateDocument(JTextComponent tc) {

            //保存操作的文本框
            textComponent = tc;
            //获取当前日期
            String strCurrentDate = getCurrentDate();
            //设置显示为当前日期,同时完成显示的格式化
            try {

                insertString(0, strCurrentDate, null);
            
}
            catch (Exception ex) {

                System.out.println(ex);
            
}
        
}

        /******************************************************************
         ** 函数名称:public void insertString(int offset, String s,
         **             AttributeSet attributeSet) throws BadLocationException
         ** 功能描述:重载原方法,限制用户插入格式为日期格式
         ** 入口参数:offset: int型,插入位置
         **            s: String型,插入字符串
         **            attributeSet: AttributeSet型,属性集
         ** 返回值:无
         ** 调用者:类JDateDocument
         *******************************************************************/
        public void insertString(int offset, String s,
                                 AttributeSet attributeSet) throws
            BadLocationException {

            String toTest; //用于测试输入合法性的字符串
            //判断插入字符串长度
            if (s.length() == 1) {

                //长度为1
                try {

                    //限制输入为整数
                    Integer.parseInt(s);
                
}
                catch (Exception ex) {

                    //错误则提示并返回
                    Toolkit.getDefaultToolkit().beep();
                    return;
                
}
                //取得原始插入位置
                newOffset = offset;
                //如果插入位置为"-"符号的前面,则移动到其后面插入(改变newOffset的值)
                if (offset == 4 || offset == 7) {

                    newOffset++;
                    textComponent.setCaretPosition(newOffset);
                
}
                //如果插入位置为最后,则不插入
                if (offset == 10)return;
                //取得显示的时间,处理后得到要显示的字符串
                toTest = textComponent.getText();
                toTest = toTest.substring(0, newOffset) + s +
                    toTest.substring(newOffset + 1, 10);
                //如果要显示的字符串合法,则显示,否则给出提示并退出
                if (!isLegalDate(toTest)) {

                    Toolkit.getDefaultToolkit().beep();
                    return;
                
}
                //插入字符串
                super.remove(newOffset, 1);
                super.insertString(newOffset, s, attributeSet);
            
}
            //如果插入长度10
            else if (s.length() == 10) {

                //合法则显示,否则给出提示退出
                if (!isLegalDate(s)) {

                    Toolkit.getDefaultToolkit().beep();
                    return;
                
}
                //插入字符串
                super.remove(0, getLength());
                super.insertString(0, s, attributeSet);
            
}
        
}

        /**********************************************************************************
         ** 函数名称:public void remove(int offset, int length) throws BadLocationException
         ** 功能描述:重载原方法,删除合适位置的字符串
         ** 入口参数:offset: int型,插入位置
         **            length: int型,删除长度
         ** 返回值:无
         ** 调用者:insertString(int, String,AttributeSet)
         ***********************************************************************************/
        public void remove(int offset, int length) throws BadLocationException {

            //如果插入位置在"-"前,则回退一个光标位置
            if (offset == 4 || offset == 7)
                textComponent.setCaretPosition(offset - 1);
            else
                textComponent.setCaretPosition(offset);
        
}

        /**********************************************************************************
         ** 函数名称:public boolean isLegalDate(String strDate)
         ** 功能描述:判断插入的长度为10的字符串是否合法
         ** 入口参数:intY: int型,年的值
         **            intM: int型,月的值
         **            intD: int型,日的值
         ** 返回值:boolean型,真,表示是合法的,假,表示不合法
         ** 调用者:insertString(int, String,AttributeSet)
         ***********************************************************************************/
        public boolean isLegalDate(String strDate) {

            int intY, intM, intD; //年,月,日的值
           
...
...
(Not finished, please download and read the complete file)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
源码文件 <OpenSwing><OpenSwing>0.00 B01-06-04 20:45
源码文件 OpenSwing.jpxOpenSwing.jpx9.62 kB25-07-04 19:17
源码文件 OpenSwing.jarOpenSwing.jar343.60 kB25-07-04 20:19
code file test.battest.bat98.00 B17-06-04 14:56
源码文件 <image><image>0.00 B01-06-04 20:45
源码文件 today.giftoday.gif94.00 B06-11-03 18:11
源码文件 page.gifpage.gif146.00 B02-12-02 10:22
源码文件 preview.gifpreview.gif142.00 B02-12-02 10:23
源码文件 print.gifprint.gif134.00 B02-12-02 10:23
源码文件 zoom.gifzoom.gif148.00 B02-12-02 10:24
源码文件 multipage.gifmultipage.gif146.00 B02-12-02 10:23
源码文件 singlepage.gifsinglepage.gif138.00 B02-12-02 10:23
源码文件 <src><src>0.00 B01-06-04 20:45
源码文件 <com><com>0.00 B01-06-04 20:45
源码文件 <sunking><sunking>0.00 B01-06-04 20:45
源码文件 <swing><swing>0.00 B01-06-04 20:45
code file JNumberField.javaJNumberField.java5.09 kB25-07-04 17:51
code file JIpAddressField.javaJIpAddressField.java6.17 kB25-07-04 17:51
code file JFontDialog.javaJFontDialog.java10.24 kB25-07-04 17:50
code file JStatusBar.javaJStatusBar.java4.45 kB25-07-04 17:52
code file JDatePicker.javaJDatePicker.java27.92 kB25-07-04 17:51
code file JStringField.javaJStringField.java2.01 kB25-07-04 17:52
code file OpenSwingUtil.javaOpenSwingUtil.java2.31 kB25-07-04 17:54
code file JDateField.javaJDateField.java2.16 kB25-07-04 17:50
源码文件 <print><print>0.00 B01-06-04 20:45
code file VirtualPrint.javaVirtualPrint.java343.00 B10-05-04 10:43
code file PrintContent.javaPrintContent.java3.29 kB10-05-04 10:45
code file PreviewCanvas.javaPreviewCanvas.java1.83 kB10-05-04 10:43
code file BookOfPrint.javaBookOfPrint.java368.00 B10-05-04 10:43
源码文件 <content><content>0.00 B01-06-04 20:45
code file PrintTabString.javaPrintTabString.java3.10 kB10-05-04 10:44
code file PrintString.javaPrintString.java2.65 kB10-05-04 10:44
code file PrintSpace.javaPrintSpace.java642.00 B10-05-04 10:44
code file PrintRightString.javaPrintRightString.java810.00 B10-05-04 10:44
code file PrintLine.javaPrintLine.java687.00 B10-05-04 10:44
code file PrintImage.javaPrintImage.java834.00 B10-05-04 10:44
code file PrintGridString.javaPrintGridString.java1.27 kB10-05-04 10:44
code file PrintContentInterface.javaPrintContentInterface.java411.00 B10-05-04 10:44
code file PrintComponent.javaPrintComponent.java821.00 B10-05-04 10:44
code file PrintCenterString.javaPrintCenterString.java1.20 kB10-05-04 10:44
code file DrawOut.javaDrawOut.java2.72 kB17-06-04 11:01
code file MultiPagePopup.javaMultiPagePopup.java4.24 kB24-07-04 16:04
code file PrintAction.javaPrintAction.java4.87 kB24-07-04 16:09
code file PrintOptionDialog.javaPrintOptionDialog.java10.16 kB24-07-04 16:15
code file PrintDemo.javaPrintDemo.java2.82 kB25-07-04 17:59
code file JPopupButton.javaJPopupButton.java12.92 kB25-07-04 17:52
code file JGroupPanel.javaJGroupPanel.java19.11 kB25-07-04 17:51
源码文件 JFontComboBox.jbxJFontComboBox.jbx131.00 B25-07-04 16:58
源码文件 OpenSwingUtil.jbxOpenSwingUtil.jbx88.00 B25-07-04 17:04
源码文件 JDateFieldBeanInfo.jbxJDateFieldBeanInfo.jbx409.00 B25-07-04 18:15
源码文件 TEMP_Color16.gifTEMP_Color16.gif911.00 B07-11-03 20:08
源码文件 TEMP_Color32.gifTEMP_Color32.gif1.18 kB07-11-03 20:08
源码文件 JDateFieldColor16.gifJDateFieldColor16.gif125.00 B19-08-03 17:14
源码文件 JDateFieldColor32.gifJDateFieldColor32.gif192.00 B19-08-03 17:14
源码文件 JDateFieldMono16.gifJDateFieldMono16.gif62.00 B19-08-03 17:14
源码文件 JDateFieldMono32.gifJDateFieldMono32.gif106.00 B19-08-03 17:14
code file JDateFieldBeanInfo.javaJDateFieldBeanInfo.java1.64 kB25-07-04 18:25
code file JGroupPanelBeanInfo.javaJGroupPanelBeanInfo.java1.99 kB25-07-04 19:16
code file JPopupButtonBeanInfo.javaJPopupButtonBeanInfo.java2.48 kB25-07-04 19:16
code file JStringFieldBeanInfo.javaJStringFieldBeanInfo.java1.63 kB25-07-04 19:16
code file JNumberFieldBeanInfo.javaJNumberFieldBeanInfo.java1.63 kB25-07-04 19:16
code file JIpAddressFieldBeanInfo.javaJIpAddressFieldBeanInfo.java1.99 kB25-07-04 19:16
code file JStatusBarBeanInfo.javaJStatusBarBeanInfo.java1.86 kB25-07-04 19:16
code file JDatePickerBeanInfo.javaJDatePickerBeanInfo.java2.13 kB25-07-04 19:16
code file JFontDialogBeanInfo.javaJFontDialogBeanInfo.java1.93 kB25-07-04 19:16
源码文件 JStringField.jbxJStringField.jbx101.00 B25-07-04 19:16
源码文件 JGroupPanel.jbxJGroupPanel.jbx622.00 B25-07-04 19:16
源码文件 JFontDialog.jbxJFontDialog.jbx1.25 kB25-07-04 19:16
源码文件 JDateField.jbxJDateField.jbx250.00 B25-07-04 19:16
源码文件 JDatePicker.jbxJDatePicker.jbx225.00 B25-07-04 19:16
源码文件 JIpAddressField.jbxJIpAddressField.jbx394.00 B25-07-04 19:16
源码文件 JNumberField.jbxJNumberField.jbx101.00 B25-07-04 19:16
源码文件 JPopupButton.jbxJPopupButton.jbx643.00 B25-07-04 19:16
源码文件 JStatusBar.jbxJStatusBar.jbx200.00 B25-07-04 19:16
源码文件 OpenSwing_zh_CN.propertiesOpenSwing_zh_CN.properties512.00 B24-07-04 16:20
源码文件 OpenSwing.propertiesOpenSwing.properties337.00 B24-07-04 16:27
源码文件 OpenSwing_zh_TW.propertiesOpenSwing_zh_TW.properties512.00 B24-07-04 16:29
源码文件 OpenSwing_ja_JP.propertiesOpenSwing_ja_JP.properties793.00 B24-07-04 16:32
源码文件 <META-INF><META-INF>0.00 B25-07-04 14:14
源码文件 MANIFEST.mfMANIFEST.mf501.00 B25-07-04 17:33
code file TestMain.javaTestMain.java4.09 kB25-07-04 17:56
源码文件 OpenSwing.jpx.localOpenSwing.jpx.local10.16 kB25-07-04 19:17
...
Sponsored links
×

JDatePicker.java (143.02 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

×

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com
×

切换到中文版?

CodeForge Chinese Version
CodeForge English Version
×

Where are you going?

×

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK
×

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D