How To Use Spinners: (HodgePodge) JSpinner & SpinnerNumberModel - Picking & Packaging The Widget To Change Infectivity
![]()
As part of the hodge-podge specification [link] a widget for changing the infectivity was specified. The initial spec was for a slider, however, a more appropriate choice would be to allow int values to be entered by the user as well as to increase/decrease values in steps. The Java JSpinner widget using the SpinnerNumberModel is ideal for the job.
The widget is placed on its own subclassed panel with a titled border to aid layout and minimize code complexity.
InfectivityPanel
// InfectivityPanel.java
// BlogPixel
//
// Created by .
// Copyright 2007 www.headwedge.com. All rights reserved.
//
import headwedge.game.*;
import javax.swing.event.*;
import javax.swing.*;
public class InfectivityPanel extends JPanel {
public InfectivityPanel(HodgePodgePixels pixels) {
setBorder(BorderFactory.createTitledBorder("Infectivity"));
spin = new JSpinner(new SpinnerNumberModel(pixels.getInfectivity(), 0, pixels.getStateMax(), 1));
this.add(spin);
}
public void setChanger(ChangeListener changer) {
spin.addChangeListener(changer);
}
JSpinner spin;
}
Filed under: Java, automata, code, hodge-podge, solutions