I'd like to create JSpinners with support for non-integer values such as 2.01 and -3.456, so getValue() returns a Double.
Not only this, but I'd like the step size should be dynamic using something like following formula (10% of the magnitude):
stepSize = 0.1 * pow(10, round( log(currentValue) ));
Is it possible? Or should I ask, is it worth the hassle?
With adaption of Vishal's answer, I've produced the following class to make nice double spinners. So far, they've shown to work really well in my program although I will abstract the adaptive step size into another, parent class so I can make AdaptiveDoubleSpinners and AdaptiveIntegerSpinners later.
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class DoubleSpinner extends JSpinner {
private static final long serialVersionUID = 1L;
private static final double STEP_RATIO = 0.1;
private SpinnerNumberModel model;
public DoubleSpinner() {
super();
// Model setup
model = new SpinnerNumberModel(0.0, -1000.0, 1000.0, 0.1);
this.setModel(model);
// Step recalculation
this.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
Double value = getDouble();
// Steps are sensitive to the current magnitude of the value
long magnitude = Math.round(Math.log10(value));
double stepSize = STEP_RATIO * Math.pow(10, magnitude);
model.setStepSize(stepSize);
}
});
}
/**
* Returns the current value as a Double
*/
public Double getDouble() {
return (Double)getValue();
}
}
Yes it is possible . See the example Given Below:
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
public class SpinnerDemo extends JFrame
{
JSpinner s;
SpinnerNumberModel model ;
JSpinner.NumberEditor editor;
JTextField stepText;
JButton bStepSet;
public void prepareAndShowGUI()
{
model = new SpinnerNumberModel(0.0,-1000.0 ,1000.0,0.1);
s = new JSpinner(model);
editor = new JSpinner.NumberEditor(s) ;
s.setEditor(editor);
stepText = new JTextField(10);
bStepSet = new JButton("Set Step");
bStepSet.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent evt)
{
try
{
/* You can apply your stepSize deduction logic here*/
Double val = Double.parseDouble(stepText.getText().trim());
/*Setting the stepSize*/
model.setStepSize(val);
}
catch (Exception ex){}
}
});
Container c = getContentPane();
c.add(s);
JPanel southPanel = new JPanel();
southPanel.add(stepText);southPanel.add(bStepSet);
c.add(southPanel,BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater( new Runnable()
{
@Override
public void run()
{
SpinnerDemo sd = new SpinnerDemo();
sd.prepareAndShowGUI();
}
});
}
}