本文整理汇总了Java中javax.swing.JFormattedTextField.setFormatterFactory方法的典型用法代码示例。如果您正苦于以下问题:Java JFormattedTextField.setFormatterFactory方法的具体用法?Java JFormattedTextField.setFormatterFactory怎么用?Java JFormattedTextField.setFormatterFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.JFormattedTextField
的用法示例。
在下文中一共展示了JFormattedTextField.setFormatterFactory方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: IntegerEditor
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Construct a <code>JSpinner</code> editor that supports displaying and
* editing the value of a <code>SpinnerIntModel</code> with a
* <code>JFormattedTextField</code>. <code>This</code>
* <code>IntegerEditor</code> becomes both a <code>ChangeListener</code>
* on the spinner and a <code>PropertyChangeListener</code> on the new
* <code>JFormattedTextField</code>.
*
* @param spinner the spinner whose model <code>this</code> editor will
* monitor
* @param format the <code>NumberFormat</code> object that's used to
* display and parse the value of the text field.
* @exception IllegalArgumentException if the spinners model is not an
* instance of <code>SpinnerIntModel</code>
*
* @see #getTextField
* @see SpinnerIntModel
* @see java.text.DecimalFormat
*/
private IntegerEditor(JSpinner spinner, NumberFormat format) {
super(spinner);
if (!(spinner.getModel() instanceof SpinnerIntModel)) {
throw new IllegalArgumentException("model not a SpinnerIntModel");
}
format.setGroupingUsed(false);
format.setMaximumFractionDigits(0);
SpinnerIntModel model = (SpinnerIntModel) spinner.getModel();
NumberFormatter formatter = new IntegerEditorFormatter(model, format);
DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
JFormattedTextField ftf = getTextField();
ftf.setEditable(true);
ftf.setFormatterFactory(factory);
ftf.setHorizontalAlignment(JTextField.RIGHT);
try {
String minString = formatter.valueToString(model.getMinimum());
String maxString = formatter.valueToString(model.getMaximum());
// Trying to approximate the width difference between "m" and "0" by multiplying with 0.7
ftf.setColumns((int) Math.round(0.7 * Math.max(maxString.length(), minString.length())));
} catch (ParseException e) {
// Nothing to do, the component width will simply be the default
}
}
示例2: setupIntegerFieldTrackingValue
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Sets integer number format to JFormattedTextField instance,
* sets value of JFormattedTextField instance to object's field value,
* synchronizes object's field value with the value of JFormattedTextField instance.
*
* @param textField JFormattedTextField instance
* @param owner an object whose field is synchronized with {@code textField}
* @param property object's field name for synchronization
*/
public static void setupIntegerFieldTrackingValue(final JFormattedTextField textField,
final InspectionProfileEntry owner,
final String property) {
NumberFormat formatter = NumberFormat.getIntegerInstance();
formatter.setParseIntegerOnly(true);
textField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(formatter)));
textField.setValue(getPropertyValue(owner, property));
final Document document = textField.getDocument();
document.addDocumentListener(new DocumentAdapter() {
@Override
public void textChanged(DocumentEvent e) {
try {
textField.commitEdit();
setPropertyValue(owner, property,
((Number) textField.getValue()).intValue());
} catch (ParseException e1) {
// No luck this time
}
}
});
}
示例3: IntegerEditor
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
public IntegerEditor(int min, int max) {
super(new JFormattedTextField());
ftf = (JFormattedTextField) getComponent();
minimum = new Integer(min);
maximum = new Integer(max);
// Set up the editor for the integer cells.
integerFormat = NumberFormat.getIntegerInstance();
NumberFormatter intFormatter = new NumberFormatter(integerFormat);
intFormatter.setFormat(integerFormat);
intFormatter.setMinimum(minimum);
intFormatter.setMaximum(maximum);
ftf.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
ftf.setValue(minimum);
ftf.setHorizontalAlignment(JTextField.TRAILING);
ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
// React when the user presses Enter while the editor is
// active. (Tab is handled as specified by
// JFormattedTextField's focusLostBehavior property.)
ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
ftf.getActionMap().put("check", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (!ftf.isEditValid()) { // The text is invalid.
if (userSaysRevert()) { // reverted
ftf.postActionEvent(); // inform the editor
}
} else
try { // The text is valid,
ftf.commitEdit(); // so use it.
ftf.postActionEvent(); // stop editing
} catch (java.text.ParseException exc) {
}
}
});
}
示例4: DateCellEditor
import javax.swing.JFormattedTextField; //导入方法依赖的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) {
}
}
}
});
}
示例5: init
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
static void init(int length, boolean hex, JFormattedTextField text) {
ValueFormatter formatter = new ValueFormatter(length, hex);
text.setColumns(length);
text.setFormatterFactory(new DefaultFormatterFactory(formatter));
text.setHorizontalAlignment(SwingConstants.RIGHT);
text.setMinimumSize(text.getPreferredSize());
text.addFocusListener(formatter);
}
示例6: HexaEditor
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
HexaEditor (JSpinner spinner)
{
super(spinner);
JFormattedTextField ftf = getTextField();
ftf.setEditable(true);
ftf.setFormatterFactory(new HexaFormatterFactory());
}
示例7: CellTypeTextFieldDefaultImpl
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Creates a {@link JFormattedTextField} for the specified cell. If a formatter is given, will
* apply it to the field. Does not validate the model, so make sure this call works!
*
* @param model
* @param rowIndex
* @param columnIndex
* @param cellClass
* @param formatter
* the formatter or <code>null</code> if none is required
* @param hideUnavailableContentAssist
* @return
*/
public CellTypeTextFieldDefaultImpl(final TablePanelModel model, final int rowIndex, final int columnIndex,
final Class<? extends CellType> cellClass, AbstractFormatter formatter, boolean hideUnavailableContentAssist) {
super();
final JFormattedTextField field = CellTypeImplHelper.createFormattedTextField(model, rowIndex, columnIndex);
setLayout(new BorderLayout());
add(field, BorderLayout.CENTER);
// otherwise 'null' would be restored
Object value = model.getValueAt(rowIndex, columnIndex);
String text = value != null ? String.valueOf(value) : "";
// specical handling when formatter is given
if (formatter != null) {
field.setFormatterFactory(new DefaultFormatterFactory(formatter));
}
field.setText(text);
// set syntax assist if available
String syntaxHelp = model.getSyntaxHelpAt(rowIndex, columnIndex);
if (syntaxHelp != null && !"".equals(syntaxHelp.trim())) {
PromptSupport.setForeground(Color.LIGHT_GRAY, field);
PromptSupport.setPrompt(syntaxHelp, field);
PromptSupport.setFontStyle(Font.ITALIC, field);
PromptSupport.setFocusBehavior(FocusBehavior.SHOW_PROMPT, field);
}
// see if content assist is possible for this field, if so add it
ImageIcon icon = SwingTools.createIcon("16/"
+ I18N.getMessageOrNull(I18N.getGUIBundle(), "gui.action.content_assist.icon"));
JButton contentAssistButton = new JButton();
contentAssistButton.setIcon(icon);
if (field.isEnabled() && model.isContentAssistPossibleForCell(rowIndex, columnIndex)) {
contentAssistButton.setToolTipText(I18N.getMessageOrNull(I18N.getGUIBundle(),
"gui.action.content_assist_enabled.tip"));
CellTypeImplHelper.addContentAssist(model, rowIndex, columnIndex, field, contentAssistButton, cellClass);
} else {
contentAssistButton.setToolTipText(I18N.getMessageOrNull(I18N.getGUIBundle(),
"gui.action.content_assist_disabled.tip"));
contentAssistButton.setEnabled(false);
}
if (contentAssistButton.isEnabled() || (!contentAssistButton.isEnabled() && !hideUnavailableContentAssist)) {
add(contentAssistButton, BorderLayout.EAST);
}
// set size so panels don't grow larger when they get the chance
setPreferredSize(new Dimension(300, 20));
setMinimumSize(new Dimension(100, 15));
setMaximumSize(new Dimension(1600, 30));
}
示例8: IntegerCellEditor
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Constructor.
* @param min the minimum of valid values.
* @param max the maximum of valid values.
*/
public IntegerCellEditor(int min, int max) {
super(new JFormattedTextField());
textField = (JFormattedTextField) getComponent();
minimum = new Integer(min);
maximum = new Integer(max);
// Set up the editor for the integer cells.
integerFormat = NumberFormat.getIntegerInstance();
NumberFormatter intFormatter = new NumberFormatter(integerFormat);
intFormatter.setFormat(integerFormat);
intFormatter.setOverwriteMode(false);
intFormatter.setMinimum(minimum);
intFormatter.setMaximum(maximum);
textField.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
textField.setValue(minimum);
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) {
}
}
}
});
}
示例9: DoubleCellEditor
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
/**
* Constructor.
* @param min the minimum of valid values.
* @param max the maximum of valid values.
*/
public DoubleCellEditor(double min, double max) {
super(new JFormattedTextField());
textField = (JFormattedTextField) getComponent();
minimum = new Double(min);
maximum = new Double(max);
// Set up the editor for the double cells.
doubleFormat = NumberFormat.getNumberInstance();
NumberFormatter doubleFormatter = new NumberFormatter(doubleFormat);
doubleFormatter.setFormat(doubleFormat);
doubleFormatter.setOverwriteMode(false);
doubleFormatter.setMinimum(minimum);
doubleFormatter.setMaximum(maximum);
textField.setFormatterFactory(new DefaultFormatterFactory(doubleFormatter));
textField.setValue(minimum);
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) {
}
}
}
});
}
示例10: CameraOptionsPanel
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
public CameraOptionsPanel(JSONObject jo) {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.weighty = 1;
c.gridx = 0;
c.gridy = 0;
JPanel radioPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
radioPanel.add(new JLabel("View", JLabel.RIGHT));
for (CameraMode mode : CameraMode.values()) {
JRadioButton radio = mode.radio;
if (mode == CameraMode.Observer)
radio.setSelected(true);
radio.addItemListener(e -> {
if (radio.isSelected())
changeCamera(mode);
});
radioPanel.add(radio);
modeGroup.add(radio);
}
add(radioPanel, c);
JideButton info = new JideButton(Buttons.info);
info.setToolTipText("Show viewpoint info");
info.addActionListener(e -> new TextDialog("Viewpoint options information", explanation, false).showDialog());
c.gridx = 1;
c.weightx = 0;
add(info, c);
// create panels before potential camera change
JSONObject joExpert = null;
JSONObject joEquatorial = null;
if (jo != null) {
joExpert = jo.optJSONObject("expert");
joEquatorial = jo.optJSONObject("equatorial");
}
expertOptionPanel = new CameraOptionPanelExpert(joExpert, UpdateViewpoint.expert, "HEEQ", true);
equatorialOptionPanel = new CameraOptionPanelExpert(joEquatorial, UpdateViewpoint.equatorial, "HEEQ", false);
double fovMin = 0, fovMax = 180;
if (jo != null) {
fovAngle = MathUtils.clip(jo.optDouble("fovAngle", fovAngle), fovMin, fovMax);
try {
CameraMode.valueOf(jo.optString("mode")).radio.setSelected(true);
} catch (Exception ignore) {
}
JSONObject jc = jo.optJSONObject("camera");
if (jc != null)
Displayer.getCamera().fromJson(jc);
}
JPanel fovPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 0));
fovPanel.add(new JLabel("FOV angle", JLabel.RIGHT));
JSpinner fovSpinner = new JSpinner(new SpinnerNumberModel(Double.valueOf(fovAngle), Double.valueOf(fovMin), Double.valueOf(fovMax), Double.valueOf(0.01)));
fovSpinner.setMaximumSize(new Dimension(6, 22));
fovSpinner.addChangeListener(e -> {
fovAngle = (Double) fovSpinner.getValue();
Displayer.display();
});
JFormattedTextField f = ((JSpinner.DefaultEditor) fovSpinner.getEditor()).getTextField();
f.setFormatterFactory(new TerminatedFormatterFactory("%.2f", "\u00B0", fovMin, fovMax));
WheelSupport.installMouseWheelSupport(fovSpinner);
fovPanel.add(fovSpinner);
c.weightx = 1;
c.gridx = 0;
c.gridy = 1;
add(fovPanel, c);
ComponentUtils.smallVariant(this);
}
示例11: GridLayerOptions
import javax.swing.JFormattedTextField; //导入方法依赖的package包/类
GridLayerOptions(GridLayer _grid) {
grid = _grid;
createGridResolutionX();
createGridResolutionY();
setLayout(new GridBagLayout());
GridBagConstraints c0 = new GridBagConstraints();
c0.fill = GridBagConstraints.HORIZONTAL;
c0.weightx = 1.;
c0.weighty = 1.;
c0.gridy = 0;
c0.gridx = 1;
c0.anchor = GridBagConstraints.EAST;
JCheckBox axis = new JCheckBox("Solar axis", grid.getShowAxis());
axis.setHorizontalTextPosition(SwingConstants.LEFT);
axis.addActionListener(e -> {
grid.showAxis(axis.isSelected());
Displayer.display();
});
add(axis, c0);
c0.gridx = 3;
c0.anchor = GridBagConstraints.EAST;
JCheckBox labels = new JCheckBox("Grid labels", grid.getShowLabels());
labels.setHorizontalTextPosition(SwingConstants.LEFT);
labels.addActionListener(e -> {
grid.showLabels(labels.isSelected());
Displayer.display();
});
add(labels, c0);
c0.gridy = 1;
c0.gridx = 1;
c0.anchor = GridBagConstraints.EAST;
JCheckBox radial = new JCheckBox("Radial grid", grid.getShowRadial());
radial.setHorizontalTextPosition(SwingConstants.LEFT);
radial.addActionListener(e -> {
grid.showRadial(radial.isSelected());
Displayer.display();
});
add(radial, c0);
c0.gridx = 2;
c0.anchor = GridBagConstraints.EAST;
add(new JLabel("Grid type", JLabel.RIGHT), c0);
c0.gridx = 3;
c0.anchor = GridBagConstraints.WEST;
createGridTypeBox();
add(gridTypeBox, c0);
c0.gridy = 2;
c0.gridx = 0;
c0.anchor = GridBagConstraints.EAST;
add(new JLabel("Longitude", JLabel.RIGHT), c0);
JFormattedTextField fx = ((JSpinner.DefaultEditor) gridResolutionXSpinner.getEditor()).getTextField();
fx.setFormatterFactory(new TerminatedFormatterFactory("%.1f", "\u00B0", min, max));
c0.gridx = 1;
c0.anchor = GridBagConstraints.WEST;
add(gridResolutionXSpinner, c0);
c0.gridx = 2;
c0.anchor = GridBagConstraints.EAST;
add(new JLabel("Latitude", JLabel.RIGHT), c0);
JFormattedTextField fy = ((JSpinner.DefaultEditor) gridResolutionYSpinner.getEditor()).getTextField();
fy.setFormatterFactory(new TerminatedFormatterFactory("%.1f", "\u00B0", min, max));
c0.gridx = 3;
c0.anchor = GridBagConstraints.WEST;
add(gridResolutionYSpinner, c0);
ComponentUtils.smallVariant(this);
}