本文整理匯總了Java中javax.swing.text.DateFormatter類的典型用法代碼示例。如果您正苦於以下問題:Java DateFormatter類的具體用法?Java DateFormatter怎麽用?Java DateFormatter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DateFormatter類屬於javax.swing.text包,在下文中一共展示了DateFormatter類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: DateEditor
import javax.swing.text.DateFormatter; //導入依賴的package包/類
public DateEditor() {
super( new JFormattedTextField() );
textField = (JFormattedTextField) getComponent();
textField.setFormatterFactory( new DefaultFormatterFactory( new DateFormatter( Entry.LOAD_FORMAT ) ) );
textField.setFocusLostBehavior( JFormattedTextField.PERSIST );
textField.getInputMap().put( KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ), "check" );
textField.getActionMap().put( "check", new AbstractAction() {
public void actionPerformed( ActionEvent e ) {
if ( !textField.isEditValid() ) { //The text is invalid.
if ( userSaysRevert() ) { //reverted
textField.postActionEvent(); //inform the editor
}
}
else {
try { //The text is valid,
textField.commitEdit(); //so use it.
textField.postActionEvent(); //stop editing
}
catch ( java.text.ParseException exc ) {
}
}
}
} );
}
示例2: SimulationParameters
import javax.swing.text.DateFormatter; //導入依賴的package包/類
/**
* Creates new form FileSelection
*/
public SimulationParameters() {
initComponents();
this.setResizable(false);
this.setTitle("File Selection");
fileReader = new FileRead();
initialiseDateComboBox(fileReader);
JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(SpinnerTime, "HH:mm:ss");
DateFormatter formatter = (DateFormatter) timeEditor.getTextField().getFormatter();
formatter.setAllowsInvalid(false); // Make sure no invalid input is allowed
SpinnerTime.setEditor(timeEditor);
String StartTime = "08:30:00";
Date time;
try {
time = new SimpleDateFormat("HH:mm:ss").parse(StartTime);
SpinnerTime.setValue(time); // will only show the current time
} catch (ParseException ex) {
Logger.getLogger(SimulationParameters.class.getName()).log(Level.SEVERE, null, ex);
}
}
開發者ID:NicolasGriere,項目名稱:High-Frequency-Data-Order-Book-Analyser,代碼行數:24,代碼來源:SimulationParameters.java
示例3: initDefaultBehaviour
import javax.swing.text.DateFormatter; //導入依賴的package包/類
private void initDefaultBehaviour(Locale locale) {
setRenderer(new FixedDayRenderer());
setHeaderRenderer(new TranslatedHeaderRenderer(locale));
new FormatSymbols((DateFormatter) getFormattedTextField().getFormatter(),
locale);
getFormattedTextField().setBorder(
BorderFactory.createEmptyBorder(1, 5, 1, 5));
SwingUtil.enableSelectionOnFocusGained(getFormattedTextField());
addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
if (!e.isTemporary()) {
JTextField tf = ((DateField) e.getSource()).getFormattedTextField();
tf.requestFocusInWindow();
}
}
});
setValue(null);
}
示例4: setupWorkspace
import javax.swing.text.DateFormatter; //導入依賴的package包/類
@Deprecated
//not used yet
private File setupWorkspace() {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
DateFormatter dateFormatter = new DateFormatter(dateFormat);
String dateString = "not_set_yet";
File workspace = null;
try {
dateString = dateFormatter.valueToString(new Date());
workspace = new File(WORKSPACE_NAME + File.separator + dateString);
workspace.mkdirs();
} catch (ParseException ex) {
logger.error("Date String parsing went wrong", ex);
}
if (workspace != null && !(workspace.exists() && workspace.canRead() && workspace.canWrite())) {
logger.error("Problem with Workspace folder '{}'", WORKSPACE_NAME + File.separator + dateString);
System.exit(1);
}
return workspace;
}
示例5: testCreateFormattersFactory
import javax.swing.text.DateFormatter; //導入依賴的package包/類
public void testCreateFormattersFactory() {
DefaultFormatterFactory factory;
tf.setValue(new Integer(34));
factory = getFactoryIfDefault(tf.getFormatterFactory());
assertTrue(factory.getDefaultFormatter() instanceof NumberFormatter);
//TODO: check if factory.getDefaultFormatter() should be same to factory.getDisplayFormatter()
// or factory.getEditFormatter().
assertNull(factory.getNullFormatter());
tf.setFormatterFactory(null);
tf.setValue(new Date());
factory = getFactoryIfDefault(tf.getFormatterFactory());
assertTrue(factory.getDefaultFormatter() instanceof DateFormatter);
assertNull(factory.getDisplayFormatter());
assertNull(factory.getEditFormatter());
assertNull(factory.getNullFormatter());
tf.setFormatterFactory(null);
tf.setValue("sdffsdf");
factory = getFactoryIfDefault(tf.getFormatterFactory());
checkDefaultFormatter(factory);
tf.setFormatterFactory(null);
tf.setValue(Color.RED);
factory = getFactoryIfDefault(tf.getFormatterFactory());
checkDefaultFormatter(factory);
}
示例6: testDateEditor_DateEditor
import javax.swing.text.DateFormatter; //導入依賴的package包/類
public void testDateEditor_DateEditor() {
spinner.setModel(new SpinnerDateModel());
DateEditor dateEditor = (DateEditor) spinner.getEditor();
spinner.setEditor(dateEditor);
assertTrue(dateEditor.getTextField().isEditable());
assertTrue(dateEditor.getTextField().getFormatter() instanceof DateFormatter);
assertNull(((DateFormatter) dateEditor.getTextField().getFormatter()).getMinimum());
assertNull(((DateFormatter) dateEditor.getTextField().getFormatter()).getMaximum());
assertTrue(dateEditor.getFormat().equals(new SimpleDateFormat()));
spinner.setModel(abstractModel);
testExceptionalCase(new IllegalArgumentCase() {
@Override
public void exceptionalAction() throws Exception {
new JSpinner.DateEditor(spinner);
}
});
}
示例7: testDateEditor_formatter
import javax.swing.text.DateFormatter; //導入依賴的package包/類
public void testDateEditor_formatter() {
spinner.setModel(new SpinnerDateModel());
DateEditor dateEditor = new DateEditor(spinner);
spinner.setEditor(dateEditor);
final Date date1 = new Date(777);
DateFormatter dateFormatter = ((DateFormatter) dateEditor.getTextField().getFormatter());
dateFormatter.setMaximum(date1);
assertSame(dateFormatter.getMaximum(), date1);
assertSame(dateEditor.getModel().getEnd(), date1);
final Date date2 = new Date(555);
dateEditor.getModel().setEnd(date2);
assertSame(dateFormatter.getMaximum(), date2);
assertSame(dateEditor.getModel().getEnd(), date2);
SpinnerDateModel old = (SpinnerDateModel) spinner.getModel();
spinner.setModel(abstractModel);
final Date date3 = new Date(555);
old.setEnd(date3);
assertEquals(((DateFormatter) ((DateEditor) spinner.getEditor()).getTextField()
.getFormatter()).getMaximum(), date3);
}
示例8: createDateFormatterDisplay
import javax.swing.text.DateFormatter; //導入依賴的package包/類
/**
* Creates a {@link DateFormatter} for display-mode from a given {@link DateFormat}.
*
* @param format for the formatter
* @return created {@link DateFormatter}
* @see DateFormat
* @see DateFormatter
* @since 0.0.1
*/
public static DateFormatter createDateFormatterDisplay(final DateFormat format) {
if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(format));
if (null == format) {
throw new RuntimeExceptionIsNull("format"); //$NON-NLS-1$
}
final DateFormatter result = new DateFormatter(format) {
private static final long serialVersionUID = 8705680761187261160L;
{
setCommitsOnValidEdit(true);
}
};
if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
return result;
}
示例9: createDateFormatterEdit
import javax.swing.text.DateFormatter; //導入依賴的package包/類
/**
* Creates a {@link DateFormatter} for edit-mode from a given {@link DateFormat}.
*
* @param format for the formatter
* @return created {@link DateFormatter}
* @see DateFormat
* @see DateFormatter
* @since 0.0.1
*/
public static DateFormatter createDateFormatterEdit(final DateFormat format) {
if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(format));
if (null == format) {
throw new RuntimeExceptionIsNull("format"); //$NON-NLS-1$
}
final DateFormatter result = new DateFormatter(format) {
private static final long serialVersionUID = 8705680761187261160L;
{
setCommitsOnValidEdit(true);
}
};
if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
return result;
}
示例10: MonthDateSpinner
import javax.swing.text.DateFormatter; //導入依賴的package包/類
public MonthDateSpinner(MonthDate value, MonthDate min, MonthDate max) {
super(new SpinnerDateModel(MDF.toDate(value), toDate(min), toDate(max), STEP_UNIT));
SpinnerDateModel model = (SpinnerDateModel) super.getModel();
minDate = (Date) model.getStart();
maxDate = (Date) model.getEnd();
JFormattedTextField text = ((JSpinner.DateEditor) getEditor()).getTextField();
DateFormatter df = new DateFormatter(new SimpleDateFormat(PATTERN));
text.setFormatterFactory(new DefaultFormatterFactory(df));
text.setColumns(PATTERN.length());
text.setEditable(false);
text.setHorizontalAlignment(JTextField.RIGHT);
revalidate();
repaint();
}
示例11: DateCellEditor
import javax.swing.text.DateFormatter; //導入依賴的package包/類
/**
* Constructor.
*/
public DateCellEditor(DateFormat dateFormat) {
super(new JFormattedTextField());
textField = (JFormattedTextField) getComponent();
this.dateFormat = dateFormat;
DateFormatter dateFormatter = new DateFormatter(dateFormat);
textField.setFormatterFactory(new DefaultFormatterFactory(dateFormatter));
textField.setHorizontalAlignment(JTextField.TRAILING);
textField.setFocusLostBehavior(JFormattedTextField.PERSIST);
// React when the user presses Enter while the editor is
// active. (Tab is handled as specified by
// JFormattedTextField's focusLostBehavior property.)
textField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
textField.getActionMap().put("check", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (!textField.isEditValid()) { //The text is invalid.
Toolkit.getDefaultToolkit().beep();
textField.selectAll();
} else {
try { //The text is valid,
textField.commitEdit(); //so use it.
textField.postActionEvent(); //stop editing
} catch (java.text.ParseException ex) {
}
}
}
});
}
示例12: getDefaultFormatterFactory
import javax.swing.text.DateFormatter; //導入依賴的package包/類
/**
* Returns an AbstractFormatterFactory suitable for the passed in Object
* type.
*/
private AbstractFormatterFactory getDefaultFormatterFactory(Object type) {
if (type instanceof DateFormat) {
return new DefaultFormatterFactory(new DateFormatter(
(DateFormat) type));
}
if (type instanceof NumberFormat) {
return new DefaultFormatterFactory(new NumberFormatter(
(NumberFormat) type));
}
if (type instanceof Format) {
return new DefaultFormatterFactory(new InternationalFormatter(
(Format) type));
}
if (type instanceof Date) {
return new DefaultFormatterFactory(new DateFormatter());
}
if (type instanceof Number) {
AbstractFormatter displayFormatter = new NumberFormatter();
((NumberFormatter) displayFormatter).setValueClass(type.getClass());
AbstractFormatter editFormatter = new NumberFormatter(
new DecimalFormat("#.#"));
((NumberFormatter) editFormatter).setValueClass(type.getClass());
return new DefaultFormatterFactory(displayFormatter,
displayFormatter, editFormatter);
}
return new DefaultFormatterFactory(new DefaultFormatter());
}
示例13: refreshTicks
import javax.swing.text.DateFormatter; //導入依賴的package包/類
@Override
public List refreshTicks(Graphics2D g2, AxisState state,
Rectangle2D dataArea, RectangleEdge edge) {
try {
long l1 = this.getMaximumDate().getTime();
long l2 = this.getMinimumDate().getTime();
int diasExibidos = (int) ((l1 - l2) / ref);
if (diasExibidos == diasExibidosAnterior && ticksAnteriores != null) {
return ticksAnteriores; // otimizacao
} else {
List result = new ArrayList();
int j = 0;
for (int i = acao.getNumeroRegistros() - 1; i >= 0; i--) {
int n = (int) (diasExibidos / QUANTIDADE_DATAS);
if (j++ % n == 0) {
result.add(new DateTick(acao.getRegistro(i).getDataCorrida().getStart(),
new DateFormatter(new SimpleDateFormat("dd/MM/yy")).valueToString(acao.getRegistro(i).getData().getStart()),
TextAnchor.TOP_CENTER,
TextAnchor.TOP_RIGHT, 0));
} else {
result.add(new DateTick(acao.getRegistro(i).getDataCorrida().getStart(),
"",
TextAnchor.TOP_CENTER,
TextAnchor.TOP_RIGHT, 0));
}
}
ticksAnteriores = result;
diasExibidosAnterior = diasExibidos;
return result;
}
} catch (Exception e) {
return new ArrayList();
}
}
示例14: interactiveTimeZoneFormat
import javax.swing.text.DateFormatter; //導入依賴的package包/類
/**
* From forum: have spinner with the same timezone.
*/
public void interactiveTimeZoneFormat() {
final JSpinner spinner = new JSpinner(new SpinnerDateModel());
final JXDatePicker picker = new JXDatePicker();
picker.setTimeZone(TimeZone.getTimeZone("GMT-10"));
picker.setDate(new Date());
spinner.setValue(picker.getDate());
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss", picker.getLocale());
format.setTimeZone(picker.getTimeZone());
JFormattedTextField textField = ((DefaultEditor) spinner.getEditor()).getTextField();
textField.setFormatterFactory(
new DefaultFormatterFactory(new DateFormatter(format)));
PropertyChangeListener l = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("date".equals(evt.getPropertyName())) {
if (picker.getDate() != null)
spinner.setValue(picker.getDate());
}
}
};
picker.addPropertyChangeListener(l);
JXPanel panel = new JXPanel();
panel.add(picker);
panel.add(spinner);
showInFrame(panel, "formats in picker and spinner");
}
示例15: testCustomFormatterCore
import javax.swing.text.DateFormatter; //導入依賴的package包/類
/**
* Issue #1144-swingx: JXDatePicker must accept custom formatter.
* Use core DateFormatter - issue manifests in throwing classcastEx
*/
@Test
public void testCustomFormatterCore() {
JXDatePicker picker = new JXDatePicker();
DefaultFormatterFactory factory = new DefaultFormatterFactory(new DateFormatter());
picker.getEditor().setFormatterFactory(factory);
picker.updateUI();
}