How to set custom editor for JSpinner?
Haven't you been vexed of using that mini text field like editor (the default editor) for JSpinner? Well, I am. So I thought of changing it and started searching for the method and hurray! I got it.
The setEditor() method. This is taking a JComponent and replacing the default editor with the given component. Fine, I got a problem here, the value in the JComponent (the custom editor) isn't updated!!
How will it update? So ridiculous, isn't it? Because we've got a custom a component set as the editor and we need to look after value changes and update the component accordingly. How could we do it then? We need to add a ChangeListener for that!
In this example, I'll set a JButton as editor and the value in the JSpinner is updated on the JButton.
class JSpinnerCustomEditor extends JFrame
private void createAndShowGUI()
jb=new JButton("Current value "+s1.getValue());
public void stateChanged(ChangeEvent ce)
jb.setText("Current value "+s1.getValue());
// Size of jb is automatically adjusted!!
public static void main(String args)
The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)