下面列出了javax.swing.JSpinner#getEditor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void testDefaultFont(final JFrame frame) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSpinner spinner = new JSpinner();
frame.add(spinner);
frame.setSize(300, 100);
frame.setVisible(true);
final DefaultEditor editor = (DefaultEditor) spinner.getEditor();
final Font editorFont = editor.getTextField().getFont();
/*
* Validate that the font of the text field is changed to the
* font of JSpinner if the font of text field was not set by the
* user.
*/
if (!(editorFont instanceof UIResource)) {
throw new RuntimeException("Font must be UIResource");
}
if (!editorFont.equals(spinner.getFont())) {
throw new RuntimeException("Wrong FONT");
}
}
/**
* 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());
}
}
});
}
/**
* 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());
}
}
});
}
private JSpinner createSpinner(int value)
{
SpinnerModel model = new SpinnerNumberModel(value, Integer.MIN_VALUE, Integer.MAX_VALUE, 1);
JSpinner spinner = new JSpinner(model);
Component editor = spinner.getEditor();
JFormattedTextField spinnerTextField = ((JSpinner.DefaultEditor) editor).getTextField();
spinnerTextField.setColumns(4);
return spinner;
}
private JTextField findEditorForSpinner(Container container, String spinnerName) {
Component comp = findComponentByName(container, spinnerName);
if (comp != null) {
JSpinner lowerSpinner = (JSpinner) comp;
NumberEditor numberEditor = (NumberEditor) lowerSpinner.getEditor();
return numberEditor.getTextField();
}
return null;
}
private JTextField findEditorForSpinner(Container container, String spinnerName) {
Component comp = findComponentByName(container, spinnerName);
if (comp != null) {
JSpinner lowerSpinner = (JSpinner) comp;
NumberEditor numberEditor = (NumberEditor) lowerSpinner.getEditor();
return numberEditor.getTextField();
}
return null;
}
private JTextField findEditorForSpinner(Container container, String spinnerName) {
Component comp = findComponentByName(container, spinnerName);
if (comp != null) {
JSpinner spinner = (JSpinner) comp;
container = spinner.getEditor();
return (JTextField) findComponentByName(container, "date.spinner.editor");
}
return null;
}
private void transparentSpinner(JSpinner spTo2) {
JComponent c = spTo2.getEditor();
for (int i = 0; i < c.getComponentCount(); i++) {
Component ct = c.getComponent(i);
if (ct instanceof JTextField) {
ct.setForeground(Color.WHITE);
ct.setBackground(ColorResource.getDarkBtnColor());
}
}
spTo2.setForeground(Color.WHITE);
spTo2.setBackground(ColorResource.getDarkBgColor());
spTo2.setBorder(null);
}
/**
* 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;
}
private void paintButton(Graphics2D g, JComponent c, int width, int height) {
boolean useToolBarColors = isInToolBar(c);
Shape s;
JSpinner spinner = (JSpinner) c.getParent();
boolean editorFocused = false;
JComponent editor = spinner.getEditor();
if (editor instanceof DefaultEditor) {
editorFocused = ((DefaultEditor)editor).getTextField().isFocusOwner();
}
if (focused || editorFocused) {
s = createButtonShape(0, 0, width, height, CornerSize.OUTER_FOCUS);
g.setPaint(getFocusPaint(s, FocusType.OUTER_FOCUS, useToolBarColors));
g.fill(s);
s = createButtonShape(0, 0, width - 1, height - 1, CornerSize.INNER_FOCUS);
g.setPaint(getFocusPaint(s, FocusType.INNER_FOCUS, useToolBarColors));
g.fill(s);
}
s = createButtonShape(0, 0, width - 2, height - 2, CornerSize.BORDER);
g.setPaint(getSpinnerPrevBorderPaint(s, type));
g.fill(s);
s = createButtonShape(1, 1, width - 4, height - 4, CornerSize.INTERIOR);
g.setPaint(getSpinnerPrevInteriorPaint(s, type));
g.fill(s);
s = shapeGenerator.createRectangle(1, 0, width - 4, 1);
g.setPaint(getSpinnerPrevTopLinePaint(s, type));
g.fill(s);
}
private void paintButton(Graphics2D g, JComponent c, int width, int height) {
boolean useToolBarColors = isInToolBar(c);
Shape s;
JSpinner spinner = (JSpinner) c.getParent();
boolean editorFocused = false;
JComponent editor = spinner.getEditor();
if (editor instanceof DefaultEditor) {
editorFocused = ((DefaultEditor)editor).getTextField().isFocusOwner();
}
if (focused || editorFocused) {
s = createButtonShape(0, 0, width, height, CornerSize.OUTER_FOCUS);
g.setPaint(getFocusPaint(s, FocusType.OUTER_FOCUS, useToolBarColors));
g.fill(s);
s = createButtonShape(0, 1, width - 1, height - 1, CornerSize.INNER_FOCUS);
g.setPaint(getFocusPaint(s, FocusType.INNER_FOCUS, useToolBarColors));
g.fill(s);
}
s = createButtonShape(0, 2, width - 2, height - 2, CornerSize.BORDER);
g.setPaint(getSpinnerNextBorderPaint(s, type));
g.fill(s);
s = createButtonShape(1, 3, width - 4, height - 4, CornerSize.INTERIOR);
g.setPaint(getSpinnerNextInteriorPaint(s, type));
g.fill(s);
}
/**
* 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 void testDefaultFont(final JFrame frame) {
final JSpinner spinner = new JSpinner();
final JSpinner spinner_u = new JSpinner();
frame.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 50));
frame.getContentPane().add(spinner);
frame.getContentPane().add(spinner_u);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
final DefaultEditor ed = (DefaultEditor) spinner.getEditor();
final DefaultEditor ed_u = (DefaultEditor) spinner_u.getEditor();
ed_u.getTextField().setFont(USERS_FONT);
for (int i = 5; i < 40; i += 5) {
/*
* Validate that the font of the text field is changed to the
* font of JSpinner if the font of text field was not set by the
* user.
*/
final Font tff = ed.getTextField().getFont();
if (!(tff instanceof UIResource)) {
throw new RuntimeException("Font must be UIResource");
}
if (spinner.getFont().getSize() != tff.getSize()) {
throw new RuntimeException("Rrong size");
}
spinner.setFont(new Font("dialog", Font.BOLD, i));
/*
* Validate that the font of the text field is NOT changed to the
* font of JSpinner if the font of text field was set by the user.
*/
final Font tff_u = ed_u.getTextField().getFont();
if (tff_u instanceof UIResource || !tff_u.equals(USERS_FONT)) {
throw new RuntimeException("Font must NOT be UIResource");
}
if (spinner_u.getFont().getSize() == tff_u.getSize()) {
throw new RuntimeException("Wrong size");
}
spinner_u.setFont(new Font("dialog", Font.BOLD, i));
}
}