Frage

I made a small dialog with a JFormattedTextField, to enter a Float number between 0 and 10 up to 3 decimal numbers. I'm using NumberFormat for that, and a PropertyChangeListener to validate the value or get back to the old value. But it is not working:

public class IRCompensationDialog extends JDialog{

private static final long serialVersionUID = 1L;

private JDialog irDialog;
private JButton cancelButton, okButton;
private JFormattedTextField resistorValue;
private INode nodo;

public IRCompensationDialog(int idNodo) throws BusinessException, ParseException{
    super(MainFrame.getInstance());
    this.irDialog = this;
    this.setResizable(false);
    this.setModal(true);
    this.setTitle("IR Compensation");
    this.nodo = new ServicesFactoryImpl().getNodesServices().getNode(idNodo);

    initComponents();

    this.pack();
    this.setLocationRelativeTo(null);
    this.setVisible(true);
}

private void initComponents() throws ParseException{
    NumberFormat numberFormat = NumberFormat.getInstance();
    numberFormat.setMaximumFractionDigits(3);
    resistorValue = new JFormattedTextField(numberFormat);
    resistorValue.addPropertyChangeListener("value", new IRValueChangeListener());
    float currentValue = nodo.getIR() / 1000;
    resistorValue.setValue(currentValue);

    JPanel botonera = new JPanel();
    cancelButton = new JButton("Cancel");
    cancelButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            irDialog.setVisible(false);
        }
    });
    botonera.add(cancelButton);
    okButton = new JButton("OK");
    okButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Float newValue = (Float)resistorValue.getValue()*1000;
            nodo.setIR(newValue.intValue());
        }
    });
    botonera.add(okButton);

    this.setLayout (new BorderLayout());
    this.add (resistorValue, BorderLayout.CENTER);
    this.add (new JLabel("Enter resistor value (Ohms):"), BorderLayout.NORTH);
    this.add (botonera, BorderLayout.SOUTH);
}

private class IRValueChangeListener implements PropertyChangeListener{

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        JFormattedTextField field = (JFormattedTextField) evt.getSource();
        Float newValue = (Float)evt.getNewValue().toString();

        if(newValue>0 && newValue<=10000){
            JOptionPane.showMessageDialog(MainFrame.getInstance(), " Value must be between 0 and 10 Ohm", "Error", JOptionPane.ERROR_MESSAGE);
            Float oldValue = (Float) evt.getOldValue();
            field.setValue(oldValue);
        }

    }

}

}

INode is a class I made, which stores an int value I'm getting with getIR() method and updating it with setIR(int) method.

I'm getting java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Float in the line Float newValue = (Float)resistorValue.getValue()*1000; and also at line Float newValue = (Float)evt.getNewValue();

War es hilfreich?

Lösung

there are two areas

1) not Float but float,

2) I use casting for getting value from JFormattedTextField

3) float newValue = (((Number) resistorValue.getValue()).floatValue());

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top