2

0 から 100 パーセント (0.0f-1.0f に変換) の入力を許可し、常にパーセント記号を表示し、無効な文字を許可しない JFormattedTextField を使用して、浮動小数点数をパーセント値としてフォーマットしたいと考えています。

NumberFormat.getPercentInstance() と NumberFormatter 属性を少し試してみましたが、成功しませんでした。

標準クラスでこれらのルールに従う JFormattedTextField を作成する方法はありますか? または、独自の NumberFormatter を実装する必要がありますか?

それが私がこれまでに持っているものです(100%を入力する方法はなく、0を入力すると完全に壊れます):

public class MaskFormatterTest {
    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame("Test");
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        NumberFormat format = NumberFormat.getPercentInstance();
        NumberFormatter formatter = new NumberFormatter(format);
        formatter.setMaximum(1.0f);
        formatter.setMinimum(0.0f);
        formatter.setAllowsInvalid(false);
        formatter.setOverwriteMode(true);
        JFormattedTextField tf = new JFormattedTextField(formatter);
        tf.setColumns(20);
        tf.setValue(0.56f);

        frame.add(tf);
        frame.pack();
        frame.setVisible(true);
    }
}
4

3 に答える 3

6

わかりました、私はそれを作りました。解決策は単純ではありませんが、少なくとも私が望んでいることは正確に実行されます。float の代わりに double を返す場合を除きます。主な制限の 1 つは、小数桁が許可されていないことですが、今のところは問題ありません。

import java.awt.BorderLayout;
import java.text.NumberFormat;
import java.text.ParseException;

import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.DocumentFilter;
import javax.swing.text.NavigationFilter;
import javax.swing.text.NumberFormatter;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Position.Bias;

public class JPercentField extends JComponent {

    private static final double MIN_VALUE = 0.0d;
    private static final double MAX_VALUE = 1.0d;
    private static final double STEP_SIZE = 0.01d;

    private static final long serialVersionUID = -779235114254706347L;

    private JSpinner spinner;

    public JPercentField() {
        initComponents();
        initLayout();
        spinner.setValue(MIN_VALUE);
    }

    private void initComponents() {
        SpinnerNumberModel model = new SpinnerNumberModel(MIN_VALUE, MIN_VALUE, MAX_VALUE, STEP_SIZE);
        spinner = new JSpinner(model);
        initSpinnerTextField();
    }

    private void initSpinnerTextField() {
        DocumentFilter digitOnlyFilter = new PercentDocumentFilter(getMaximumDigits());
        NavigationFilter navigationFilter = new BlockLastCharacterNavigationFilter(getTextField());
        getTextField().setFormatterFactory(
                new DefaultFormatterFactory(new PercentNumberFormatter(createPercentFormat(), navigationFilter,
                        digitOnlyFilter)));
        getTextField().setColumns(6);
    }

    private int getMaximumDigits() {
        return Integer.toString((int) MAX_VALUE * 100).length();
    }

    private JFormattedTextField getTextField() {
        JSpinner.NumberEditor jsEditor = (JSpinner.NumberEditor) spinner.getEditor();
        JFormattedTextField textField = jsEditor.getTextField();
        return textField;
    }

    private NumberFormat createPercentFormat() {
        NumberFormat format = NumberFormat.getPercentInstance();
        format.setGroupingUsed(false);
        format.setMaximumIntegerDigits(getMaximumDigits());
        format.setMaximumFractionDigits(0);
        return format;
    }

    private void initLayout() {
        setLayout(new BorderLayout());
        add(spinner, BorderLayout.CENTER);
    }

    public double getPercent() {
        return (Double) spinner.getValue();
    }

    public void setPercent(double percent) {
        spinner.setValue(percent);
    }

    private static class PercentNumberFormatter extends NumberFormatter {

        private static final long serialVersionUID = -1172071312046039349L;

        private final NavigationFilter navigationFilter;
        private final DocumentFilter digitOnlyFilter;

        private PercentNumberFormatter(NumberFormat format, NavigationFilter navigationFilter,
                DocumentFilter digitOnlyFilter) {
            super(format);
            this.navigationFilter = navigationFilter;
            this.digitOnlyFilter = digitOnlyFilter;
        }

        @Override
        protected NavigationFilter getNavigationFilter() {
            return navigationFilter;
        }

        @Override
        protected DocumentFilter getDocumentFilter() {
            return digitOnlyFilter;
        }

        @Override
        public Class<?> getValueClass() {
            return Double.class;
        }

        @Override
        public Object stringToValue(String text) throws ParseException {
            Double value = (Double) super.stringToValue(text);
            return Math.max(MIN_VALUE, Math.min(MAX_VALUE, value));
        }
    }

    /**
     * NavigationFilter that avoids navigating beyond the percent sign.
     */
    private static class BlockLastCharacterNavigationFilter extends NavigationFilter {

        private JFormattedTextField textField;

        private BlockLastCharacterNavigationFilter(JFormattedTextField textField) {
            this.textField = textField;
        }

        @Override
        public void setDot(FilterBypass fb, int dot, Bias bias) {
            super.setDot(fb, correctDot(fb, dot), bias);
        }

        @Override
        public void moveDot(FilterBypass fb, int dot, Bias bias) {
            super.moveDot(fb, correctDot(fb, dot), bias);
        }

        private int correctDot(FilterBypass fb, int dot) {
            // Avoid selecting the percent sign
            int lastDot = Math.max(0, textField.getText().length() - 1);
            return dot > lastDot ? lastDot : dot;
        }
    }

    private static class PercentDocumentFilter extends DocumentFilter {

        private int maxiumDigits;

        public PercentDocumentFilter(int maxiumDigits) {
            super();
            this.maxiumDigits = maxiumDigits;
        }

        @Override
        public void insertString(FilterBypass fb, int offset, String text, AttributeSet attrs)
                throws BadLocationException {
            // Mapping an insert as a replace without removing
            replace(fb, offset, 0, text, attrs);
        }

        @Override
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
            // Mapping a remove as a replace without inserting
            replace(fb, offset, length, "", SimpleAttributeSet.EMPTY);
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
                throws BadLocationException {
            int replaceLength = correctReplaceLength(fb, offset, length);
            String cleanInput = truncateInputString(fb, filterDigits(text), replaceLength);
            super.replace(fb, offset, replaceLength, cleanInput, attrs);
        }

        /**
         * Removes all non-digit characters
         */
        private String filterDigits(String text) throws BadLocationException {
            StringBuilder sb = new StringBuilder(text);
            for (int i = 0, n = sb.length(); i < n; i++) {
                if (!Character.isDigit(text.charAt(i))) {
                    sb.deleteCharAt(i);
                }
            }
            return sb.toString();
        }

        /**
         * Removes all characters with which the resulting text would exceed the maximum number of digits
         */
        private String truncateInputString(FilterBypass fb, String filterDigits, int replaceLength) {
            StringBuilder sb = new StringBuilder(filterDigits);
            int currentTextLength = fb.getDocument().getLength() - replaceLength - 1;
            for (int i = 0; i < sb.length() && currentTextLength + sb.length() > maxiumDigits; i++) {
                sb.deleteCharAt(i);
            }
            return sb.toString();
        }

        private int correctReplaceLength(FilterBypass fb, int offset, int length) {
            if (offset + length >= fb.getDocument().getLength()) {
                // Don't delete the percent sign
                return offset + length - fb.getDocument().getLength();
            }
            return length;
        }
    }

}
于 2011-09-29T15:27:54.477 に答える
2

1)初期値にSpinnerNumberModelを設定できるため、代わりにJSpinnerの使用を検討してくださいJFormattedTextField

APIから

Integer value = new Integer(50); 
Integer min = new Integer(0);
Integer max = new Integer(100); 
Integer step = new Integer(1);

JSpinner(with )の単純なハックでSpinnerNumberModelは、別の入力を数字として許可しません。それ以外の場合は、次のいずれかの入力が可能ですChars

2)JFormattedTextField実装する必要があるため

  • ドキュメントリスナー
  • 書類

どちらの場合も、JFormattedTextField値が必要な範囲よりも小さいか大きい場合は、catch の回避策を作成する必要があります ...

編集:

.

ここに画像の説明を入力

.

まったく真実ではありません:-)あなたは...単純な間違いからほど遠いです:-)、結果に小さな間違いがあります。このコードを見てください

import java.awt.BorderLayout;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.text.*;

public class TestDigitsOnlySpinner {

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame("enter digit");
                JSpinner jspinner = makeDigitsOnlySpinnerUsingDocumentFilter();
                frame.getContentPane().add(jspinner, BorderLayout.CENTER);
                frame.getContentPane().add(new JButton("just another widget"), BorderLayout.SOUTH);
                frame.pack();
                frame.setVisible(true);
            }

            private JSpinner makeDigitsOnlySpinnerUsingDocumentFilter() {
                JSpinner spinner = new JSpinner(new SpinnerNumberModel());
                JSpinner.NumberEditor jsEditor = (JSpinner.NumberEditor) spinner.getEditor();
                JFormattedTextField textField = jsEditor.getTextField();
                final DocumentFilter digitOnlyFilter = new DocumentFilter() {

                    @Override
                    public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                        if (stringContainsOnlyDigits(string)) {
                            super.insertString(fb, offset, string, attr);
                        }
                    }

                    @Override
                    public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
                        super.remove(fb, offset, length);
                    }

                    @Override
                    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                        if (stringContainsOnlyDigits(text)) {
                            super.replace(fb, offset, length, text, attrs);
                        }
                    }

                    private boolean stringContainsOnlyDigits(String text) {
                        for (int i = 0; i < text.length(); i++) {
                            if (!Character.isDigit(text.charAt(i))) {
                                return false;
                            }
                        }
                        return true;
                    }
                };
                /*NumberFormat format = NumberFormat.getIntegerInstance();
                format.setGroupingUsed(false);// or add the group chars to the filter
                NumberFormat format = NumberFormat.getInstance();*/

                NumberFormat format = NumberFormat.getPercentInstance();
                format.setGroupingUsed(false);
                format.setGroupingUsed(true);// or add the group chars to the filter
                format.setMaximumIntegerDigits(10);
                format.setMaximumFractionDigits(2);
                format.setMinimumFractionDigits(5);
                textField.setFormatterFactory(new DefaultFormatterFactory(new InternationalFormatter(format) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    protected DocumentFilter getDocumentFilter() {
                        return digitOnlyFilter;
                    }
                }));
                return spinner;
            }
        });
    }
}
于 2011-09-27T13:58:41.143 に答える