Link to home
Start Free TrialLog in
Avatar of GoldStrike
GoldStrike

asked on

Modifying Look of JSpinner

I would like to modify the JSpinner so that the font is bigger and the up and down buttons are larger.
I would aslo like to make the JSpinner accept keyboard input. The Font statement I have below has no effect.

Does anyone know how to accomplish this?

private JSpinner secondsSpinner;
private SpinnerNumberModel secModel;

secondsSpinner = new javax.swing.JSpinner();

// Does not work
secondsSpinner.setFont(new java.awt.Font("Dialog", 1, 36));
ScriptPanel.add(secondsSpinner);
secondsSpinner.setBounds(760, 170, 160, 40);

secModel = new SpinnerNumberModel(1, 1, 3600, 1); //(value, min, max, step);
secondsSpinner.setModel(secModel);

Avatar of sciuriware
sciuriware

Just do:               UIManager.put("Spinner.font", <myfont>);
as soon as possible in your program.

;JOOP!
How to attach key listner

JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) jSpinner1.getEditor();
JComponent textField = editor.getTextField();
textField.addKeyListener(new Frame1_jSpinner1_keyAdapter(this));




You may be able to manupilate the buttons by doing. But not sure

((JButton)jSpinner1.getComponent(1))
((JButton)jSpinner1.getComponent(2))
I did it this way:

   JSpinner qlimit;
   SpinnerNumberModel qlimitModel;

      qlimitModel = new SpinnerNumberModel
      (
         Admin.threadQueueLimit,
         Admin.MINQUEUELIMIT,
         Admin.MAXQUEUELIMIT,
         1
      );
      qlimitModel.addChangeListener(this);
      qlimit = new JSpinner(qlimitModel);
// Wasting away the placement in a JPanel with JLabel etc.

   public void stateChanged(ChangeEvent e)
   {
   Object o = e.getSource();

      if(o == qlimitModel) // It's from the spinner!
      {
         .... = qlimitModel.getNumber().intValue();

;JOOP!
Avatar of GoldStrike

ASKER

Just do:               UIManager.put("Spinner.font", <myfont>);
as soon as possible in your program.

OICSDisplay.java [386:1] illegal start of expression
        UIManager.put("secondsSpinner.font", <myfont>);
                                             ^
OICSDisplay.java [386:1] ')' expected
        UIManager.put("secondsSpinner.font", <myfont>);
                                                      ^
2 errors
Errors compiling OICSDisplay.
I did it this way:

Your code doesn't address any of my questions.
You obviously can not even copy what I typed for you.
What the hell is secondSpinner.font ?

Now please create a font like:

   Font MyFont = new Font("Ms Sans Serif", Font.BOLD, 12);
    UIManager.put("Spinner.font", MyFont);

And do not add other things in it.
These 2 lines change the font inside all spinners in the whole application.

Did you try the other code? If it works fine for my customers, it will do for you.
;JOOP!
Can anyone point me to sample code where the JSpinner is modified?
sciurware
key - an Object specifying the retrieval key
Font MyFont = new Font("Ms Sans Serif", Font.BOLD, 12);
    UIManager.put("Spinner.font", MyFont);
Doesn't work, font int my spinner remains unchanged you must have short changed your customers :)

       // Spinner added to Script Panel
        secondsSpinner = new javax.swing.JSpinner();

        //secondsSpinner.setFont(new java.awt.Font("Dialog", 1, 48));
        ScriptPanel.add(secondsSpinner);
        secondsSpinner.setBounds(760, 170, 160, 40);
       
        Font myFont = new Font("Ms Sans Serif", Font.BOLD, 12);
        UIManager.put("Spinner.font", myFont);

        // Set Spinners in Script panel
        secModel = new SpinnerNumberModel(1, 1, 3600, 1); //(value, min, max, step);
        secondsSpinner.setModel(secModel);
       
try applying the font of the FormatedtextField as I mentioned

JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) jSpinner1.getEditor();
JComponent textField = editor.getTextField();
textField.addKeyListener(new Frame1_jSpinner1_keyAdapter(this));
textField.setFont(new Font("Ms Sans Serif", Font.BOLD, 99));


This code works
But you have to try it
You should apply UiManager calls BEFORE any component of that type is created!

And it is against the EE rules to provide you with a complete program.
You should understand the construct and apply it yourself.
;JOOP!
The only problem with
 UIManager.put("Spinner.font", myFont);
is that it will be applied to all the Spinners, if you want to apply to a single spinner try the way i described above,
i.e. getting the TextField out and applying font on it
armoghan

textField.addKeyListener(new Frame1_jSpinner1_keyAdapter(this));

I don't quite understand what defines this "Frame1_jSpinner1_keyAdapter"

Can you explain what this does and how I define it.

thanks
armoghan, I believe that you do want to have the same fonts in the same components.
If you apply different fonts everywhere your application will look like a Christmas tree
and your customers will not go to like it.

Just put yourself in their place with something like Word, Windows Explorer or whatever.

;JOOP!
ASKER CERTIFIED SOLUTION
Avatar of armoghan
armoghan
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sciuware
     Font myFont = new Font("Ms Sans Serif", Font.BOLD, 12);
     UIManager.put("Spinner.font", myFont);

Can you explain how your code applies the font to the spinner. How does the UIManager know your talking about the
Spinner? I'm trying to understand the construct, this part does not make sense.

I'm applying your code in the constructor of my GUI class and the Spinner font does not change. I know I missing  a
step here somewhere and I think it has to do with the "key" in the UIManager.put statement.

thanks
Can't you read?  "Spinner.font"  !!!!!
That's how JAVA works; you can control almost every kind of Component's properties.
This way, for instance, I told JAVA to make all buttons in Metal LOOK&FEEL to display as in MSWindows native mode.
From the above I see that you still can not follow instructions:

those lines must be applied BEFORE you are going to creat the components,
thus NOT in the constructor!!!!!
A place, to begin with, is directly in the main().
Later you can put things in a GUI-initialisation module.

;JOOP!
You have got to be kidding me, haven't you being reading the posting's, you need to look up the word "professional", I would rather not have sciuware's comments if he responds in such a rude and de-meaning way, he can keep his comments.

Go ahead an notify the admin, all they have to do is read the postings to realize you are way off base here.

There will be no apologies from me, none are needed, I responded accordingly.
I want to thank those who reponded in a "professional" manner.