JSpinner: Increase length of editor box

前端 未结 5 463
一整个雨季
一整个雨季 2020-12-06 06:30

I have a JSpinner that displays decimal values from 0.0 to 999.0. It seems to work fine, except for when it displays a number in the editor box that is four-digits long, su

相关标签:
5条回答
  • 2020-12-06 07:05

    You can get to the text field which in fact is a JFormattedTextField by

    • First calling getEditor() on your JSpinner to get the spinner's editor
    • cast the returned object to JSpinner.DefaultEditor
    • Then call getTextField() on this. Then you can set it's preferredSize if desired.

    Edit: as noted by trashgod though, using a proper layout is paramount and being sure that the layouts you use are the best is probably the best way to solve this issue.

    Edit 2: The above is wrong as setting the textfield's preferred size does nothing. You can however set the preferred size of the editor itself, and that works. e.g .,

    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();
             }
          });
       }
    }
    
    0 讨论(0)
  • 2020-12-06 07:05

    The first Hovercraft answers is not bad at all. You can not change the size directly, but you can do something like this:

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

    Where you can define the columns numbers showed by the editor. It will change the size of the spinner.

    0 讨论(0)
  • 2020-12-06 07:12

    As FontMetrics vary from one platform to the next, it's better to rely on the component's own calculation of preferred size. This example shows a spectrum of JSpinner sizes for various min and max values. Note in particular that FlowLayout "lets each component assume 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();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-06 07:13

    Changing the max value of a spinner will increase the size of the text box to accommodate the large number. If you do not wish to make the max value larger, i would recommend what @JorgeHortelano suggested...

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

    0 讨论(0)
  • 2020-12-06 07:24
    JSpinner spn=new JSpinner();
    spn.setPreferredSize(new Dimension(100,25));
    

    Here,look This is the easy Answer.

    0 讨论(0)
提交回复
热议问题