本文整理汇总了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();
}
}
}