Member Avatar for hfx642

I would like my JSpinner to show up as having a Layout of GridLayout (3, 1), with;
Panel.add (JTextField);
Panel.add (Up_JButton);
Panel.add (Down_JButton);
and all 3 (contained) GUI components to be the same size.
Is this possible?
if (so) How?

(My work around is to build it myself, which I'm already half way there.)

TIA!

I'm not sure if I understood the language of your trunk ...

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.text.*;

public class TestDigitsOnlySpinner {

    public static void main(String... args) {
        SwingUtilities.invokeLater((Runnable) new Runnable() {

            @Override
            public void run() {
                JSpinner jspinner = makeDigitsOnlySpinnerUsingDocumentFilter();
                jspinner.setFont(new Font("Dialog", Font.BOLD | Font.PLAIN, 18));
                JPanel pnl = new JPanel();
                pnl.setLayout(new GridLayout(3, 0, 10, 10));
                //pnl.setLayout(new GridLayout(0, 3, 10, 10));
                pnl.add(new JTextField(" just another widget "));
                pnl.add(new JButton("vote up"));
                pnl.add(new JButton("vote down"));
                JFrame frame = new JFrame("enter digit");
                frame.setLayout(new BorderLayout(10, 10));
                JPanel pane = (JPanel) frame.getContentPane();
                pane.setBorder(new EmptyBorder(4, 4, 4, 4));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(jspinner, BorderLayout.CENTER);
                frame.add(pnl, BorderLayout.SOUTH);
                frame.pack();
                frame.setVisible(true);
                SwingUtilities.invokeLater((Runnable) new Runnable() {

                    @Override
                    public void run() {

                    }
                });
            }

            private JSpinner makeDigitsOnlySpinner_BasicAttempt() {
                JSpinner spinner = new JSpinner(new SpinnerNumberModel());
                return spinner;
            }

            private JSpinner makeDigitsOnlySpinnerUsingDocumentFilter() {
                JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 20, 1));
                JSpinner.NumberEditor jsEditor = (JSpinner.NumberEditor) spinner.getEditor();
                final Document jsDoc = jsEditor.getTextField().getDocument();
                if (jsDoc instanceof PlainDocument) {
                    AbstractDocument doc = new PlainDocument() {

                        private static final long serialVersionUID = 1L;

                        @Override
                        public void setDocumentFilter(DocumentFilter filter) {
                            if (filter instanceof MyDocumentFilter) {
                                super.setDocumentFilter(filter);
                            }
                        }
                    };
                    doc.setDocumentFilter(new MyDocumentFilter());
                    jsEditor.getTextField().setDocument(doc);
                }
                return spinner;
            }
        });
    }

    private static class MyDocumentFilter extends DocumentFilter {

        @Override
        public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
            if (stringContainsOnlyDigits(string)) {
                super.insertString(fb, offset, string, attr);
            }
        }

        @Override
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
            super.remove(fb, offset, length);
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            if (stringContainsOnlyDigits(text)) {
                super.replace(fb, offset, length, text, attrs);
            }
        }

        private boolean stringContainsOnlyDigits(String text) {
            for (int i = 0; i < text.length(); i++) {
                if (!Character.isDigit(text.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
    }

    private TestDigitsOnlySpinner() {
    }
}
Member Avatar for hfx642

Nope! What I really want is my JSpinner to look/work like this...

// Import Java Packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//*****************************************************************************
// My_Spinner Class

public class My_Spinner extends JFrame
{
//-----------------------------------------------------------------------------
// Fonts

   public Font Spinner_Font = new Font ("MonoSpaced", Font.BOLD, 85);

//-----------------------------------------------------------------------------
// Colours

   public Color Spinner_FG = Color.BLACK;
   public Color Spinner_BG = Color.WHITE;

//-----------------------------------------------------------------------------
// Directories and Files


//-----------------------------------------------------------------------------
// GUI

   public JTextField Spinner_TF = new JTextField ();
   public JButton Button_Up = new JButton ();
   public JButton Button_Down = new JButton ();
   public JPanel Spinner_Pnl_Grid = new JPanel ();

//-----------------------------------------------------------------------------
// Variables

   public int Value = 0;
   public int Limit_Up = 9;
   public int Limit_Down = 0;

//*****************************************************************************
// My_Spinner Constructor

   public My_Spinner ()
   {

      Build ();

      Container Cntainr = getContentPane ();
      Cntainr.setLayout (new FlowLayout ());
      Cntainr.add (Spinner_Pnl_Grid);

      setSize (100, 425);
      setVisible (true);

   }  // End of My_Spinner Constructor

//=============================================================================
// main Method

   public static void main (String args [])
   {
      My_Spinner App = new My_Spinner ();
      App.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
   }  // End of main Method

//*****************************************************************************
// Build Method

   private void Build ()
   {
//=============================================================================
// GUI Definitions

      Button_Up = new JButton ("Up");
      Button_Down = new JButton ("Down");

      Spinner_TF = new JTextField ();
      Spinner_TF.setForeground (Spinner_FG);
      Spinner_TF.setBackground (Spinner_BG);
      Spinner_TF.setFont (Spinner_Font);
      Spinner_TF.setHorizontalAlignment (JLabel.CENTER);
      Spinner_TF.setEditable (false);

//=============================================================================
// Panel Definitions

      Spinner_Pnl_Grid = new JPanel (new GridLayout (3, 1, 0, 2));
      Spinner_Pnl_Grid.setBackground (Spinner_FG);

//=============================================================================
// Assembly

      Spinner_Pnl_Grid.add (Spinner_TF);
      Spinner_Pnl_Grid.add (Button_Up);
      Spinner_Pnl_Grid.add (Button_Down);

//=============================================================================
// Final Assembly


//=============================================================================
// Initializations

      Spinner_TF.setText (Integer.toString (Value));

//=============================================================================
// Actions

      Button_Up.addActionListener
      (
         new ActionListener ()
         {
            public void actionPerformed (ActionEvent AE)
            {
               if (Value < Limit_Up) Value++;
               Spinner_TF.setText (Integer.toString (Value));
            }  // End of actionPerformed
         }  // End of ActionListener
      );  // End of addActionListener

      Button_Down.addActionListener
      (
         new ActionListener ()
         {
            public void actionPerformed (ActionEvent AE)
            {
               if (Value > Limit_Down) Value--;
               Spinner_TF.setText (Integer.toString (Value));
            }  // End of actionPerformed
         }  // End of ActionListener
      );  // End of addActionListener

//=============================================================================
   }  // End of Build Method

//*****************************************************************************
}  // End of My_Spinner Class

there are

1) JFormattedTextField, then you can pretty to forgot for non-editable JComponent(s), or change that to the JLabel, if you set JLabel#setOpaque(true), then you'll be able to put there Border or BackGroung

2) both JButtons (together) should be occupate same area as JTextField, output is ugly ???

3) ActionListener replace with Action, avoids against futures problems/issues with MultiThreading or Concurency in Swing

4) EDIT ignore that I didn't see that

you forgot to set frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); , then your present JVM instace(s) are sticked in the Native OS untill its power-of or restarted

5) don't set setSize(100, 425);, check my shot to the dark

6) set EmtpyBorder

7) is contra .... blablabla set for ContentPane, from Java5 were replaced with aka JPanel with BorderLayout as default

8) I'm going to sleep

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.