开发者

How do I format the initial value with JSpinner?

I want to change the default decimal format for a JSpinner, specifying a number of decima开发者_如何学Pythonl places but leaving other formatting locale specific.

But if I do this

@Test public void fails() {
    JSpinner spinner = new JSpinner();
    NumberEditor editor = new JSpinner.NumberEditor(spinner);
    editor.getFormat().setMinimumFractionDigits(3);
    spinner.setEditor(editor);
    assertEquals(0, spinner.getValue());
    assertEquals("0.000", ((NumberEditor) spinner.getEditor()).getTextField().getText());
        // FAIL HERE with "0"

    spinner.setValue(0.01);
    assertEquals("0.010", ((NumberEditor) spinner.getEditor()).getTextField().getText());
}

as I think I found in Sun example code, the initial formatted value is "0", as the changes to the format have not been detected.

I can set the format picture explicitly

@Test public void delMe() {
    JSpinner spinner = new JSpinner();
    spinner.setEditor(new JSpinner.NumberEditor(spinner, "0.000"));
    assertEquals(0, spinner.getValue());
    assertEquals("0.000", ((NumberEditor) spinner.getEditor()).getTextField().getText());
}

but this does not respect locale settings with regards to commas etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜