나는 전체 범위의 실제 목록을 작성하지 않고도이를 수행 할 수있는 방법이 있다고 생각합니다.
다음과 같이 AbstractSpinnerModel
을 확장하여 LongNumberModel
으로 만드는 것이 한 방법입니다. 관련 내용 example도 참조하십시오. 진수의 JSpinner에 대한 [몇 가지 힌트
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.text.ParseException;
import javax.swing.AbstractSpinnerModel;
import javax.swing.JFormattedTextField;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.text.DefaultFormatter;
import javax.swing.text.DefaultFormatterFactory;
/**
* @see https://stackoverflow.com/a/13121724/230513
* @see https://stackoverflow.com/a/9758714/230513
*/
public class HexSpinnerTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
display();
}
});
}
private static void display() throws HeadlessException {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSpinner sp = new JSpinner(new LongNumberModel(0x8000L, 0x8000L, 0xFFFFL, 1L));
JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) sp.getEditor();
JFormattedTextField tf = editor.getTextField();
tf.setFormatterFactory(new MyFormatterFactory());
f.getContentPane().add(sp, BorderLayout.NORTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static class LongNumberModel extends AbstractSpinnerModel {
private Long value, stepSize;
private Comparable<Long> minimum, maximum;
public LongNumberModel(Long value, Long minimum, Long maximum, Long stepSize) {
this.value = value;
this.minimum = minimum;
this.maximum = maximum;
this.stepSize = stepSize;
}
@Override
public Object getValue() {
return value;
}
@Override
public void setValue(Object value) {
this.value = (Long) value;
fireStateChanged();
}
@Override
public Object getNextValue() {
long v = value.longValue() + stepSize.longValue();
return bounded(v);
}
@Override
public Object getPreviousValue() {
long v = value.longValue() - stepSize.longValue();
return bounded(v);
}
private Object bounded(long v) {
if ((maximum != null) && (maximum.compareTo(v) < 0)) {
return null;
}
if ((minimum != null) && (minimum.compareTo(v) > 0)) {
return null;
}
return Long.valueOf(v);
}
}
private static class MyFormatterFactory extends DefaultFormatterFactory {
@Override
public AbstractFormatter getDefaultFormatter() {
return new HexFormatter();
}
}
private static class HexFormatter extends DefaultFormatter {
@Override
public Object stringToValue(String text) throws ParseException {
try {
return Long.valueOf(text, 16);
} catch (NumberFormatException nfe) {
throw new ParseException(text, 0);
}
}
@Override
public String valueToString(Object value) throws ParseException {
return Long.toHexString(
((Long) value).intValue()).toUpperCase();
}
}
}
가능한 중복? 나는 올바른 접근법이야] (http://stackoverflow.com/questions/9758469/some-hints-for-hex-jspinner-i-s-the-approach-correct) – trashgod
나는 위의 해법 no. 1. 위의 링크는 16 진수 문제에 대한 서식 지정 만 해결하고 음수 값은 표시 할 수 없습니다. 다른 부정적인 hexBinary 표현으로 JSpinner를 시작하고 상위 양의 한계까지 위로 롤링하는 것과 관련된 다른 문제에 직면하고 있습니다. – schinoy
나는 결국 JSpinnerListModel을 사용하고 유틸리티 클래스에 의해 생성 된 범위의 값을 가진 hexbinary 문자열 목록을 전달하여이 작업을 완료했습니다. 물론, 나는이 해결책이 마음에 들지 않으며, 전체 범위의 실제 목록을 작성하지 않고 그것을 할 수있는 방법이있을 것이라고 생각합니다. – schinoy