Spinner button size

If the button size is really important, there is a dirty hack:

Add a custom method ‘setWidth’ with a parameter ‘w’ to your spinner component:

def setWidth(self, w):
	"""
	Arguments:
		self: A reference to the component instance this method is invoked on. This argument
		  is automatic and should not be specified when invoking this method.
		width: The button width
	# First component of the JSpinner should be the 'up' button
	btn1 = self.getComponent(0)

	# the field 'c' is returned by getPreferredSize() of the button
	c = btn1.getClass()
	sizeField = c.getDeclaredField("c")
	sizeField.setAccessible(1);
	
	# set the new button size. only the width is used, the height is determined by the text editor
	from java.awt import Dimension
	newSize = Dimension(w,w)
	sizeField.set(btn1, newSize)
	self.doLayout()
	self.getComponent(2).doLayout()

Call the new method in the spinner’s propertyChange script:

if event.propertyName in ('editor', 'componentRunning' ):
	# editor: designer preview mode
	# componentRunning: client
	event.source.setWidth(30)

In the designer, the width will only be updated when switching to preview node.

4 Likes