下面列出了javax.swing.JSpinner#DefaultEditor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Workaround for a swing bug : when the user enters an illegal value, the
* text is forced to the last value.
*
* @param spinner the spinner to update
*/
public static void fixIntegerList (final JSpinner spinner)
{
JSpinner.DefaultEditor editor;
editor = (JSpinner.DefaultEditor) spinner.getEditor();
final JFormattedTextField ftf = editor.getTextField();
ftf.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "enterAction");
ftf.getActionMap().put("enterAction", new AbstractAction()
{
@Override
public void actionPerformed (ActionEvent e)
{
try {
spinner.setValue(Integer.parseInt(ftf.getText()));
} catch (NumberFormatException ex) {
// Reset to last value
ftf.setText(ftf.getValue().toString());
}
}
});
}
public CgSpinnerDouble(double start, double min, double max, double step) {
super();
this.min = min;
this.max = max;
this.step = step;
model = new SpinnerNumberModel(start, // initial value
min, // min
max, // max
step);
this.setModel(model);
addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent mwe) {
MouseWheelAction(mwe.getWheelRotation());
}
});
// Center
JSpinner.DefaultEditor spinnerEditor = (JSpinner.DefaultEditor) this.getEditor();
spinnerEditor.getTextField().setHorizontalAlignment(JTextField.CENTER);
}
public CgSpinner(int start, int min, int max, int step) {
super();
this.min = min;
this.max = max;
this.step = step;
model = new SpinnerNumberModel(start, // initial value
min, // min
max, // max
step); // step
setModel(model);
addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent mwe) {
MouseWheelAction(mwe.getWheelRotation());
}
});
// Center
JSpinner.DefaultEditor spinnerEditor = (JSpinner.DefaultEditor) this.getEditor();
spinnerEditor.getTextField().setHorizontalAlignment(JTextField.CENTER);
}
/**
* Workaround for a swing bug : when the user enters an illegal value, the
* text is forced to the last value.
*
* @param spinner the spinner to update
*/
public static void fixIntegerList (final JSpinner spinner)
{
JSpinner.DefaultEditor editor;
editor = (JSpinner.DefaultEditor) spinner.getEditor();
final JFormattedTextField ftf = editor.getTextField();
ftf.getInputMap()
.put(KeyStroke.getKeyStroke("ENTER"), "enterAction");
ftf.getActionMap()
.put(
"enterAction",
new AbstractAction()
{
@Override
public void actionPerformed (ActionEvent e)
{
try {
spinner.setValue(Integer.parseInt(ftf.getText()));
} catch (Exception ex) {
// Reset to last value
ftf.setText(ftf.getValue().toString());
}
}
});
}
protected boolean isFocused( Component c ) {
if( c instanceof JScrollPane ) {
JViewport viewport = ((JScrollPane)c).getViewport();
Component view = (viewport != null) ? viewport.getView() : null;
if( view != null ) {
if( FlatUIUtils.isPermanentFocusOwner( view ) )
return true;
if( (view instanceof JTable && ((JTable)view).isEditing()) ||
(view instanceof JTree && ((JTree)view).isEditing()) )
{
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if( focusOwner != null )
return SwingUtilities.isDescendingFrom( focusOwner, view );
}
}
return false;
} else if( c instanceof JComboBox && ((JComboBox<?>)c).isEditable() ) {
Component editorComponent = ((JComboBox<?>)c).getEditor().getEditorComponent();
return (editorComponent != null) ? FlatUIUtils.isPermanentFocusOwner( editorComponent ) : false;
} else if( c instanceof JSpinner ) {
if( FlatUIUtils.isPermanentFocusOwner( c ) )
return true;
JComponent editor = ((JSpinner)c).getEditor();
if( editor instanceof JSpinner.DefaultEditor ) {
JTextField textField = ((JSpinner.DefaultEditor)editor).getTextField();
if( textField != null )
return FlatUIUtils.isPermanentFocusOwner( textField );
}
return false;
} else
return FlatUIUtils.isPermanentFocusOwner( c );
}
private Component getEditor() {
JComponent editorComponent = ((JSpinner) component).getEditor();
if (editorComponent == null) {
throw new JavaAgentException("Null value returned by getEditor() on spinner", null);
}
if (editorComponent instanceof JSpinner.DefaultEditor) {
editorComponent = ((JSpinner.DefaultEditor) editorComponent).getTextField();
}
return editorComponent;
}
private String getSpinnerText() {
JComponent editor = ((JSpinner) component).getEditor();
if (editor == null) {
} else {
RComponentFactory finder = new RComponentFactory(omapConfig);
if (editor instanceof JSpinner.DefaultEditor) {
RComponent rComponent = finder.findRawRComponent(editor, null, recorder);
return rComponent.getText();
}
}
return null;
}
@Override
public String getText() {
if (component instanceof JSpinner.DefaultEditor) {
String text = ((JSpinner.DefaultEditor) component).getTextField().getText();
return text;
}
return null;
}
/**
* Creates a new IntegerListSpinner object.
*/
public IntegerListSpinner ()
{
setModel(new SpinnerListModel());
// Right alignment
JSpinner.DefaultEditor editor;
editor = (JSpinner.DefaultEditor) getEditor();
editor.getTextField().setHorizontalAlignment(JTextField.RIGHT);
}
/**
* Make the spinner text field editable, or not
*
* @param spinner the spinner to update
* @param bool true if editable, false otherwise
*/
public static void setEditable (JSpinner spinner,
boolean bool)
{
JSpinner.DefaultEditor editor;
editor = (JSpinner.DefaultEditor) spinner.getEditor();
editor.getTextField().setEditable(bool);
}
/**
* Align the spinner display to the right
*
* @param spinner the spinner to update
*/
public static void setRightAlignment (JSpinner spinner)
{
JSpinner.DefaultEditor editor;
editor = (JSpinner.DefaultEditor) spinner.getEditor();
editor.getTextField().setHorizontalAlignment(JTextField.RIGHT);
}
@Override
public JComponent getEditorComponent() {
final JSpinner field = new JSpinner(new SpinnerNumberModel(value, min, max, 1.0));
// NB: Workaround for JSpinner default sizing algorithm when min/max values have very large
// magnitudes
// (see: https://implementsblog.com/2012/11/26/java-gotcha-jspinner-preferred-size/)
final JComponent fieldEditor = field.getEditor();
if (fieldEditor instanceof JSpinner.DefaultEditor) {
((JSpinner.DefaultEditor) fieldEditor).getTextField().setColumns(10);
}
field.addChangeListener(e -> value = (double) field.getValue());
return field;
}
/**
* Get the editable text field component of the spinner.
*
* @return text field
*/
private JTextField getTextField() {
// There really seems to be no simpler way to do this
JComponent editor = spinner.getEditor();
if (editor instanceof JSpinner.DefaultEditor) {
return ((JSpinner.DefaultEditor) editor).getTextField();
} else {
Logger.getLogger(DropAmountChooser.class).error("Unknown editor type", new Throwable());
// This will not work, but at least it won't crash the client
return new JTextField();
}
}
/**
* Creates a new IntegerListSpinner object.
*/
public IntegerListSpinner ()
{
setModel(new SpinnerListModel());
// Right alignment
JSpinner.DefaultEditor editor;
editor = (JSpinner.DefaultEditor) getEditor();
editor.getTextField()
.setHorizontalAlignment(JTextField.RIGHT);
}
/**
* Make the spinner text field editable, or not
*
* @param spinner the spinner to update
* @param bool true if editable, false otherwise
*/
public static void setEditable (JSpinner spinner,
boolean bool)
{
JSpinner.DefaultEditor editor;
editor = (JSpinner.DefaultEditor) spinner.getEditor();
editor.getTextField()
.setEditable(bool);
}
/**
* Align the spinner display to the right
*
* @param spinner the spinner to update
*/
public static void setRightAlignment (JSpinner spinner)
{
JSpinner.DefaultEditor editor;
editor = (JSpinner.DefaultEditor) spinner.getEditor();
editor.getTextField()
.setHorizontalAlignment(JTextField.RIGHT);
}
private void initUI() {
SnapApp.getDefault().getSelectionSupport(ProductSceneView.class).addHandler(sceneViewListener);
dateLabel = new JLabel(String.format(DATE_PREFIX + " %s", getStartDateString()));
matrixSizeSpinner = new JSpinner(new SpinnerNumberModel(MATRIX_DEFAULT_VALUE,
MATRIX_MINIMUM, MATRIX_MAXIMUM,
MATRIX_STEP_SIZE));
final JComponent editor = matrixSizeSpinner.getEditor();
if (editor instanceof JSpinner.DefaultEditor) {
((JSpinner.DefaultEditor) editor).getTextField().setEditable(false);
}
matrixSizeSpinner.addChangeListener(e -> matrixModel.setMatrixSize((Integer) matrixSizeSpinner.getModel().getValue()));
final TableLayout tableLayout = new TableLayout(2);
tableLayout.setTablePadding(4, 4);
tableLayout.setTableFill(TableLayout.Fill.BOTH);
tableLayout.setTableAnchor(TableLayout.Anchor.NORTHWEST);
tableLayout.setTableWeightX(0.0);
tableLayout.setTableWeightY(0.0);
tableLayout.setColumnWeightX(0, 1.0);
tableLayout.setRowWeightY(1, 1.0);
tableLayout.setCellColspan(0, 0, 2);
setLayout(tableLayout);
setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
JPanel buttonPanel = createButtonPanel();
JPanel tablePanel = createTablePanel();
add(dateLabel);
add(tablePanel);
add(buttonPanel);
setCurrentView(SnapApp.getDefault().getSelectedProductSceneView());
setDisplayName(Bundle.CTL_TimeSeriesMatrixTopComponentName());
}
private static JTextField getEditorTextField( JComponent editor ) {
return editor instanceof JSpinner.DefaultEditor
? ((JSpinner.DefaultEditor)editor).getTextField()
: null;
}