格式化输入日期时间控件
可以用JieFormattedDateBox box = new JieFormattedDateBox("yyyy年MM月dd日HH点mm分ss秒");输入
也可以用JieFormattedDateBox box = new JieFormattedDateBox("HH:mm:ss");来输入时间
还可以这样用JSPinner spinner = new JSPinner();JieFormattedDateBox fdb = new JieFormattedDateBox(format);spinner.setModel(fdb);spinner.setEditor(fdb);
/////////////////////////////////////////////////package org.jie.ui.formattedbox;
import java.sql.Timestamp;import java.text.SimpleDateFormat;
import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import javax.swing.JFormattedTextField;import javax.swing.SpinnerModel;import javax.swing.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.event.ChangeListener;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.text.DefaultFormatterFactory;import javax.swing.text.MaskFormatter;
public class JieFormattedDateBox extends JFormattedTextField implements SpinnerModel{
private String format; private SimpleDateFormat timeFormat; private Timestamp time;
public void setTime(Timestamp time){ if(time != null) setValue(time); } public Timestamp getTime(){ return time; }
public static String replace(String src,String replaceSrc,String replaceWith){ if(src == null) return null; StringBuffer sb = new StringBuffer(); int start = 0; int end = src.indexOf(replaceSrc); while(end >= 0){ sb.append(src.substring(start,end)); start = end+replaceSrc.length(); end = src.indexOf(replaceSrc,start); sb.append(replaceWith); } sb.append(src.substring(start)); return sb.toString(); }
public JieFormattedDateBox(String format){ super(); this.format = format; timeFormat = new SimpleDateFormat(format); time = new Timestamp(System.currentTimeMillis());
try{ String mask = format; for(int i=0;i<alais.length;i++){ mask = replace(mask,alais[i],formats[i]); } MaskFormatter mf = new MaskFormatter(mask); mf.setPlaceholderCharacter('_'); setFormatterFactory(new DefaultFormatterFactory(mf)); }catch(Exception e){ e.printStackTrace(); }
setText(timeFormat.format(time)); this.getDocument().addDocumentListener(new DocumentListener(){ public void insertUpdate(DocumentEvent e){ checkTime(); } public void removeUpdate(DocumentEvent e){ checkTime(); } public void changedUpdate(DocumentEvent e){} }); this.setHorizontalAlignment(SwingConstants.RIGHT); this.addFocusListener(new FocusListener(){ public void focusGained(FocusEvent e){} public void focusLost(FocusEvent e){ setValue(time); } }); }
void checkTime(){ try{ String text = getText(); if(text == null || text.equals("")) return; time = new Timestamp(timeFormat.parse(text).getTime()); }catch(Exception ex){ try{ SwingUtilities.invokeLater(new Runnable() { public void run() { setValue(time); } }); }catch(Exception e){} }
}
public Object getValue(){ return this.getText(); } public void setValue(Object value){ time = (Timestamp)value; int index = this.getCaretPosition(); if(time == null){ this.setText(""); }else this.setText(timeFormat.format(time)); this.setCaretPosition(index);//keep Caret Position } public Object getNextValue(){ return new Timestamp(time.getTime()+getIncrease(this.getCaretPosition(),format,time)); } public Object getPreviousValue(){ return new Timestamp(time.getTime()-getDecrease(this.getCaretPosition(),format,time)); } public void addChangeListener(ChangeListener l){} public void removeChangeListener(ChangeListener l){}
static String[] alais = {"yyyy","MM","dd",
