如何从自定义jtextfield中检索double值

时间:2015-10-28 10:44:46

标签: java swing

我创建了一个只接受数字值的自定义JTextField。

所以我有这个:

package com.mcsolution.common.Componenti_Swing;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import com.mcsolution.common.supporto.VisualMessage;


public class DoubleTextFieldFormat extends TextFieldFormat{

    /**
     * 
     */
    private static final long serialVersionUID = 8856130645860365803L;
    public DoubleTextFieldFormat(){
        addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                char ch = e.getKeyChar();

                if (!isNumber(ch) && !isValidSignal(ch) && !validatePoint(ch)  && ch != '\b') {
                    e.consume();
                }
            }
        });

    }

    public DoubleTextFieldFormat(Font font,Boolean isSelectedAll,Integer horizzontalAlignment,
            Integer numberCharacter,String text,Dimension preferredSize,
            Boolean isEditable){

        if(font!=null)
            this.setFont(font);

        if(isSelectedAll){
            this.addFocusListener(new FocusListener() {
                public void focusGained(FocusEvent e){
                    selectAll();
                }
                public void focusLost(FocusEvent e){
                }
            });
        }
        if(horizzontalAlignment!=null)
            this.setHorizontalAlignment(horizzontalAlignment);

        if(numberCharacter!=null)
            this.setDocument(new PersonalizzaJtextField(numberCharacter));

        if(text!=null)
            this.setText(text);
        if(preferredSize!=null)
            this.setPreferredSize(preferredSize);

        if(isEditable!=null)
            this.setEditable(isEditable);

        addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                char ch = e.getKeyChar();

                if (!isNumber(ch) && !isValidSignal(ch) && !validatePoint(ch)  && ch != '\b') {
                    e.consume();
                }
            }
        });
    }

    private boolean isNumber(char ch){
        return ch >= '0' && ch <= '9';
    }

    private boolean isValidSignal(char ch){
        if( (getText() == null || "".equals(getText().trim()) ) && ch == '-'){
            return true;
        }

        return false;
    }

    private boolean validatePoint(char ch){
        if(ch != '.'){
            return false;
        }

        if(getText() == null || "".equals(getText().trim())){
            setText("0.");
            return false;
        }else if("-".equals(getText())){
            setText("-0.");
        }

        return true;
    }

    public Double getValue(){
        try{
            String valore = this.getText();
            valore= valore.replaceAll(",", ".");
            return Double.parseDouble(valore);
        }catch(Exception e){
            VisualMessage.getErrore();
            return null;
        }
    }
}

现在,如果我想要检索值,我使用此代码:

Double val = myTextField.getValue();

此方法并非每次都有效。

例如,如果我有一个错误的话,我将这个值输入TextField 1.900,50。

如何将此格式的值转换为x.xxx,xx?

1 个答案:

答案 0 :(得分:0)

我修复了错误,我用这个改变了我的getValue()方法:

public Double getValue(){
    try{
        NumberFormat nf = NumberFormat.getInstance(Locale.getDefault());
        //String valore = ;
        return nf.parse(this.getText()).doubleValue();

    }catch(Exception e){
        VisualMessage.getErrore();
        return null;
    }
}