JSpinner: increasing editor window length

I have a JSpinner that shows decimal values ​​from 0.0 to 999.0. It seems to work fine, except when it displays a number in the editor field four digits long, for example 123.4; he then cuts off part of the last digit because it is not long enough.

So my question is: does anyone know how to increase the window length of the JSpinner editor?

Thanks!

+6
source share
4 answers

You can get a text field that is actually a JFormattedTextField on

  • First call getEditor() on your JSpinner to get the spinner editor
  • returns the returned object in JSpinner.DefaultEditor
  • Then call getTextField() . Then you can set it to preferredSize if you want.

Edit: as trashgod noted, however, using the correct layout is of utmost importance and make sure that the most suitable layouts are the best way to solve this problem.

Edit 2: The above is incorrect, since setting the preferred field size does nothing. However, you can set the preferred size of the editor itself, and it works. eg,

 import java.awt.Dimension; import javax.swing.*; public class SpinnerBigTextField { private static void createAndShowGui() { JSpinner spinner = new JSpinner(new SpinnerNumberModel(0.0, 0.0, 999.0, 0.5)); JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(300, 100)); panel.add(spinner); JComponent field = ((JSpinner.DefaultEditor) spinner.getEditor()); Dimension prefSize = field.getPreferredSize(); prefSize = new Dimension(200, prefSize.height); field.setPreferredSize(prefSize); JFrame frame = new JFrame("SpinnerBigTextField"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(panel); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } 
+10
source

Since FontMetrics varies from one platform to another, it is best to rely on your own calculation of the preferred component size. This example shows a range of JSpinner sizes for various min and max values. Note in particular that FlowLayout "allows each component to take its natural (preferred) size."

enter image description here

 import java.awt.EventQueue; import java.awt.FlowLayout; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.SpinnerNumberModel; /** @see http://stackoverflow.com/questions/7374659 */ public class SpinnerTest extends Box { private static final double STEP = 0.1d; private static final String FORMAT = "0.0000000000"; public SpinnerTest(int axis) { super(axis); for (int i = 0; i < 8; i++) { int v = (int) Math.pow(10, i); this.add(genParamPanel((i + 1) + ":", -v, v)); } } private JPanel genParamPanel(String name, double min, double max) { JPanel panel = new JPanel(new FlowLayout(FlowLayout.TRAILING)); JLabel label = new JLabel(name, JLabel.TRAILING); JSpinner js = new JSpinner(new SpinnerNumberModel(min, min, max, STEP)); js.setEditor(new JSpinner.NumberEditor(js, FORMAT)); panel.add(label); panel.add(js); return panel; } private void display() { JFrame f = new JFrame("SpinnerTest"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new SpinnerTest(BoxLayout.Y_AXIS).display(); } }); } } 
+7
source

The first answers to Hovercraft are pretty good. You cannot resize directly, but you can do something like this:

 JComponent editor = mySpinner.getEditor(); JFormattedTextField tf = ((JSpinner.DefaultEditor) editor).getTextField(); tf.setColumns(4); 

Here you can define the column numbers displayed by the editor. It will resize the counter.

+7
source

Changing the maximum counter value will increase the size of the text field to accommodate a large number. If you don't want the maximum value to be greater, I would recommend what @JorgeHortelano suggested ...

JComponent editor = mySpinner.getEditor (); JFormattedTextField tf = ((JSpinner.DefaultEditor)). GetTextField (); tf.setColumns (4);

0
source

Source: https://habr.com/ru/post/897057/


All Articles