dimanche 28 décembre 2014

Creating Custom Spinner Model


Vote count:

0




I'm trying to create a custom JSpinner that represents a sequence of numbers of power 2. Like, 1-2-4-8-16 and so on. I have to do that with extending AbstractSpinnerModel. The problem is, the arrow keys on my custom JSpinner don't work. Nothing changes when I click one of them. I need to show previous and next values of current value. ( Previous value = 4 Current Value = 8 Next Value = 16 ).


Here is my code :



import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Hw5SpinnerModel extends JFrame {

public static void main(String[] args) {

Hw5SpinnerModel frame = new Hw5SpinnerModel();

}

public Hw5SpinnerModel() {

setTitle("Hw5SpinnerModel");
setSize(350, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JSpinner jspn = new JSpinner(new CustomSpinnerModel());

final JLabel jlbl = new JLabel("");

add(jspn, BorderLayout.NORTH);
add(jlbl, BorderLayout.CENTER);

jspn.addChangeListener(new ChangeListener() {

@Override
public void stateChanged(ChangeEvent e) {

jlbl.setText(jspn.getPreviousValue() + jspn.getValue() + jspn.getNextValue());

}

});

//jlbl.setText(jspn.getNextValue() + "");

setVisible(true);

}

public class CustomSpinnerModel extends AbstractSpinnerModel {

Integer i = 1;

@Override
public Object getValue() {
return i;
}

@Override
public void setValue(Object value) {
i = (Integer) value;
}

@Override
public Object getNextValue() {
return 2 * i;
}

@Override
public Object getPreviousValue() {
return i / 2;
}

}


}



asked 29 secs ago







Creating Custom Spinner Model

Aucun commentaire:

Enregistrer un commentaire