當前位置: 首頁>>代碼示例>>Java>>正文


Java DateFormatter類代碼示例

本文整理匯總了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 ) {
                }
            }
        }
    } );
}
 
開發者ID:mleoking,項目名稱:PhET,代碼行數:27,代碼來源:DateEditor.java

示例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);
}
 
開發者ID:jspresso,項目名稱:jspresso-ce,代碼行數:21,代碼來源:JDateField.java

示例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;
}
 
開發者ID:opennms-forge,項目名稱:poc-opennms-spreadsheet-category-manager,代碼行數:21,代碼來源:Starter.java

示例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);
}
 
開發者ID:shannah,項目名稱:cn1,代碼行數:25,代碼來源:JFormattedTextFieldTest.java

示例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);
        }
    });
}
 
開發者ID:shannah,項目名稱:cn1,代碼行數:18,代碼來源:JSpinnerTest.java

示例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);
}
 
開發者ID:shannah,項目名稱:cn1,代碼行數:21,代碼來源:JSpinnerTest.java

示例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;
}
 
開發者ID:slaubenberger,項目名稱:wichtel,代碼行數:27,代碼來源:FormatFactory.java

示例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;
}
 
開發者ID:slaubenberger,項目名稱:wichtel,代碼行數:27,代碼來源:FormatFactory.java

示例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();
}
 
開發者ID:Depter,項目名稱:JRLib,代碼行數:17,代碼來源:MonthDateSpinner.java

示例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) {
                }
            }
        }
    });
}
 
開發者ID:takun2s,項目名稱:smile_1.5.0_java7,代碼行數:36,代碼來源:DateCellEditor.java

示例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());
}
 
開發者ID:javalovercn,項目名稱:j2se_for_android,代碼行數:33,代碼來源:JFormattedTextField.java

示例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();
    }
}
 
開發者ID:wrbraga,項目名稱:JGrafix,代碼行數:35,代碼來源:EixoHorizontalComLabels.java

示例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");
}
 
開發者ID:RockManJoe64,項目名稱:swingx,代碼行數:32,代碼來源:JXDatePickerVisualCheck.java

示例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();
}
 
開發者ID:RockManJoe64,項目名稱:swingx,代碼行數:12,代碼來源:JXDatePickerTest.java


注:本文中的javax.swing.text.DateFormatter類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。