Java – jspinner: increase the length of the edit box

I have a jspinner that displays decimal values from 0.0 to 999.0 It seems to work properly, except that it displays a four digit number in the editor box, such as 123.4; Then it will cut off the last digit of the part because it is not long enough

So my question is: who knows how to increase the length of the jspinner editor window?

thank you!

Solution

You can access the text field that is actually jformattedtextfield

>First, call geteditor () on the jspinner to get the spinner's editor > cast the returned object to jspinner Defaulteditor > then call gettextfield() here Then, if necessary, you can set its preferredSize

Edit: as trashgod said, it is important to use the appropriate layout and ensure that the layout you use is the best, which may be the best way to solve this problem

Edit 2: the above is wrong because nothing is done to set the preferred size of the text field However, you can set the preferred size of the editor itself, which is valid For example,

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();
         }
      });
   }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>