Java Code Examples for javax.swing.text.DateFormatter

The following examples show how to use javax.swing.text.DateFormatter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * 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);
    }
}
 
Example 2
public void setDateFormat( final DateFormat timeFormat ) {
  if ( timeFormat == null ) {
    throw new NullPointerException();
  }

  dateField.setFormatterFactory( new JFormattedTextField.AbstractFormatterFactory() {
    public JFormattedTextField.AbstractFormatter getFormatter( final JFormattedTextField tf ) {
      return new DateFormatter( timeFormat ) {
        // allow to clear the field
        public Object stringToValue( final String text ) throws ParseException {
          return "".equals( text ) ? null : super.stringToValue( text );
        }
      };
    }
  } );

  if ( timeFormat instanceof SimpleDateFormat ) {
    final SimpleDateFormat dateFormat = (SimpleDateFormat) timeFormat;
    setToolTipText( dateFormat.toLocalizedPattern() );
  } else {
    setToolTipText( null );
  }
  if ( dateChooserPanel != null ) {
    setDate( getCellEditorValue() );
  }
}
 
Example 3
Source Project: java-swing-tips   Source File: MainPanel.java    License: MIT License 5 votes vote down vote up
private MainPanel() {
  super(new GridLayout(2, 1));

  Calendar c = Calendar.getInstance();
  // c.clear(Calendar.HOUR_OF_DAY);
  // c.clear(Calendar.AM_PM);
  // c.clear(Calendar.HOUR);
  c.set(Calendar.HOUR_OF_DAY, 0);
  c.clear(Calendar.MINUTE);
  c.clear(Calendar.SECOND);
  c.clear(Calendar.MILLISECOND);
  Date d = c.getTime();

  SimpleDateFormat format = new SimpleDateFormat("mm:ss", Locale.getDefault());
  DefaultFormatterFactory factory = new DefaultFormatterFactory(new DateFormatter(format));

  JSpinner spinner1 = new JSpinner(new SpinnerDateModel(d, null, null, Calendar.SECOND));
  ((JSpinner.DefaultEditor) spinner1.getEditor()).getTextField().setFormatterFactory(factory);

  JSpinner spinner2 = new JSpinner(new SpinnerDateModel(d, null, null, Calendar.SECOND) {
    @Override public void setCalendarField(int calendarField) {
      // https://docs.oracle.com/javase/8/docs/api/javax/swing/SpinnerDateModel.html#setCalendarField-int-
      // If you only want one field to spin you can subclass and ignore the setCalendarField calls.
    }
  });
  ((JSpinner.DefaultEditor) spinner2.getEditor()).getTextField().setFormatterFactory(factory);

  add(makeTitledPanel("Default SpinnerDateModel", spinner1));
  add(makeTitledPanel("Override SpinnerDateModel#setCalendarField(...)", spinner2));
  setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 4
Source Project: snap-desktop   Source File: DateTimePicker.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void updateTextFieldFormat() {
    if (timeSpinner == null) {
        return;
    }
    JFormattedTextField tf = ((JSpinner.DefaultEditor) timeSpinner.getEditor()).getTextField();
    DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
    DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();
    // Change the date format to only show the hours
    formatter.setFormat(timeFormat);
}
 
Example 5
Source Project: nosql4idea   Source File: DateTimePicker.java    License: Apache License 2.0 5 votes vote down vote up
private void updateTextFieldFormat() {
    if (timeSpinner == null) return;
    JFormattedTextField tf = ((JSpinner.DefaultEditor) timeSpinner.getEditor()).getTextField();
    DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
    DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();
    // Change the date format to only show the hours
    formatter.setFormat(timeFormat);
}
 
Example 6
Source Project: dragonwell8_jdk   Source File: Test6462562.java    License: GNU General Public License v2.0 4 votes vote down vote up
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 7
Source Project: TencentKona-8   Source File: Test6462562.java    License: GNU General Public License v2.0 4 votes vote down vote up
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 8
Source Project: jdk8u60   Source File: Test6462562.java    License: GNU General Public License v2.0 4 votes vote down vote up
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 9
Source Project: openjdk-jdk8u   Source File: Test6462562.java    License: GNU General Public License v2.0 4 votes vote down vote up
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 10
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 11
Source Project: openjdk-jdk9   Source File: Test6462562.java    License: GNU General Public License v2.0 4 votes vote down vote up
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 12
Source Project: jdk8u-jdk   Source File: Test6462562.java    License: GNU General Public License v2.0 4 votes vote down vote up
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 13
Source Project: hottub   Source File: Test6462562.java    License: GNU General Public License v2.0 4 votes vote down vote up
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 14
Source Project: openjdk-8-source   Source File: Test6462562.java    License: GNU General Public License v2.0 4 votes vote down vote up
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 15
Source Project: openjdk-8   Source File: Test6462562.java    License: GNU General Public License v2.0 4 votes vote down vote up
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 16
Source Project: jdk8u_jdk   Source File: Test6462562.java    License: GNU General Public License v2.0 4 votes vote down vote up
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 17
Source Project: jdk8u-jdk   Source File: Test6462562.java    License: GNU General Public License v2.0 4 votes vote down vote up
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 18
Source Project: jdk8u-dev-jdk   Source File: Test6462562.java    License: GNU General Public License v2.0 4 votes vote down vote up
TestFormattedTextField create(DateFormat format) {
    DateFormatter fmt = new DateFormatter(format);
    return new TestFormattedTextField(fmt);
}
 
Example 19
private DefaultFormatterFactory createFormatterFactory() {
	return new DefaultFormatterFactory(new DateFormatter(peer
			.getDateFormat()));
}