本文整理匯總了Java中javax.swing.JFormattedTextField.getFormatter方法的典型用法代碼示例。如果您正苦於以下問題:Java JFormattedTextField.getFormatter方法的具體用法?Java JFormattedTextField.getFormatter怎麽用?Java JFormattedTextField.getFormatter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.JFormattedTextField
的用法示例。
在下文中一共展示了JFormattedTextField.getFormatter方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getCalendarField
import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
/**
* Returns the calendarField under the start of the selection, or -1 if
* there is no valid calendar field under the selection (or the spinner
* isn't editing dates.
*/
private int getCalendarField(JSpinner spinner)
{
JComponent editor = spinner.getEditor();
if( editor instanceof JSpinner.DateEditor )
{
JSpinner.DateEditor dateEditor = (JSpinner.DateEditor) editor;
JFormattedTextField ftf = dateEditor.getTextField();
int start = ftf.getSelectionStart();
JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter();
if( formatter instanceof InternationalFormatter )
{
Format.Field[] fields = ((InternationalFormatter) formatter).getFields(start);
for( int counter = 0; counter < fields.length; counter++ )
{
if( fields[counter] instanceof DateFormat.Field )
{
int calendarField;
if( fields[counter] == DateFormat.Field.HOUR1 )
{
calendarField = Calendar.HOUR;
}
else
{
calendarField = ((DateFormat.Field) fields[counter]).getCalendarField();
}
if( calendarField != -1 )
{
return calendarField;
}
}
}
}
}
return -1;
}
示例2: IntegerSpinner
import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
/**
* Instantiates a new value spinner.
*/
public IntegerSpinner(int min, int max, int stepSize) {
SpinnerNumberModel model = new SpinnerNumberModel(min, min, max, stepSize);
setModel(model);
JComponent comp = getEditor();
final JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
formatter.setCommitsOnValidEdit(true);
addChangeListener(new ChangeListener() {
private double oldValue = Double.MAX_VALUE;
@Override
public void stateChanged(ChangeEvent e) {
Double doubleValue = IntegerSpinner.this.getDoubleValue();
if (doubleValue != oldValue) {
double oldValueCopy = oldValue;
oldValue = doubleValue;
if (minIsZero) {
if (doubleValue < 0.0) {
doubleValue = 0.0;
field.setValue(doubleValue);
}
}
notifyListeners(oldValueCopy, doubleValue);
}
}
});
}
示例3: createUI
import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
/**
* Creates the ui.
*
* @param initialValue the initial value
* @param min the min
* @param max the max
* @param stepSize the step size
* @param noOfDecimalPlaces the no of decimal places
*/
private void createUI(Double initialValue, Double min, Double max, Double stepSize,
double noOfDecimalPlaces) {
SpinnerNumberModel model = new SpinnerNumberModel(initialValue, min, max, stepSize);
setModel(model);
JSpinner.NumberEditor editor = (JSpinner.NumberEditor) getEditor();
DecimalFormat format = editor.getFormat();
format.setMinimumFractionDigits(3);
final JFormattedTextField field = (JFormattedTextField) editor.getTextField();
DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
formatter.setCommitsOnValidEdit(true);
addChangeListener(new ChangeListener() {
private double oldValue = Double.MAX_VALUE;
@Override
public void stateChanged(ChangeEvent e) {
Double doubleValue = DecimalSpinner.this.getDoubleValue();
if (doubleValue != oldValue) {
double oldValueCopy = oldValue;
oldValue = doubleValue;
if (minIsZero) {
if (doubleValue < 0.0) {
doubleValue = 0.0;
field.setValue(doubleValue);
}
}
notifyListeners(oldValueCopy, doubleValue);
}
}
});
}
示例4: setFormat
import javax.swing.JFormattedTextField; //導入方法依賴的package包/類
/**
* Sets the format used to display the value of this spinner.
*/
public void setFormat(Format format)
{
JComponent editor = getEditor();
if (editor instanceof JSpinner.DefaultEditor)
{
JFormattedTextField textField = ((JSpinner.DefaultEditor) editor).getTextField();
AbstractFormatter formatter = textField.getFormatter();
if (formatter instanceof NumberFormatter)
{
((NumberFormatter) formatter).setFormat(format);
fireStateChanged();
}
}
}