Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6844187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:20:43+00:00 2026-05-27T00:20:43+00:00

I made a small dialog with a JFormattedTextField, to enter a Float number between

  • 0

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();

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:20:43+00:00Added an answer on May 27, 2026 at 12:20 am

    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());

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a small log service that i want to publish to a
I made a small page ( http://www.ovlu.li ) using CMS Made Simple ( http://www.cmsmadesimple.org/
I made this small location class so that I can better handle address information
Possible Duplicate: What is object serialization? I've made a small RSS Reader app using
I made a small MVC Framework, but i need that my view Class can
I made a small application using XCode. How can i create a application package
I made this small Java program using eclipse IDE. I have set the workspace
I've made a small app where Form is threaded (using BackgroundWorker ), and in
I am trying to create a small app that posts the entries made by
I made a small DLL in MSIL with two methods: float AddNumbers(int, int) int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.