Pregunta

I am doing a project where I have to convert a fraction into its lowest terms. I am experiencing errors with .getText from a JTextField. I printed their input and the input is being received as the properties rather than as a string. I am having problems with this code:

    double userDenominator = Double.parseDouble(userDenominatorInput.getText());

this give the variable a value of

javax.swing.JTextField[,12,93,166x36,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@c112a58,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]

I was wondering if any problems could be caused by me using a GUI form or a key released listener.

Here is the full code separated into two classes:

package reducefraction;

public class ReduceFraction {

    public static void main(String[] args) {
        NewJFrame form = new NewJFrame();
        form.setVisible(true);
        form.setLocationRelativeTo(null);
    }

}

Here is the second class

package reducefraction;


public class NewJFrame extends javax.swing.JFrame {

/**
 * Creates new form NewJFrame
 */
public NewJFrame() {

    initComponents();
    userDenominatorInput.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            userDenominatorInputKeyReleased(evt);
        }
    });

    userNumeratorInput.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            userNumeratorInputKeyReleased(evt);
        }
    });
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jSeparator1 = new javax.swing.JSeparator();
    userDenominatorInput = new javax.swing.JTextField();
    jSeparator2 = new javax.swing.JSeparator();
    numeratorOutput = new javax.swing.JTextField();
    denominatorOutput = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    userNumeratorInput = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Reduce Fractions");

    jLabel2.setText("This program will reduce a fraction to its lowest terms");

    userDenominatorInput.setFont(new java.awt.Font("Gabriola", 1, 24)); // NOI18N
    userDenominatorInput.setForeground(new java.awt.Color(255, 102, 51));

    numeratorOutput.setEditable(false);
    numeratorOutput.setColumns(10);
    numeratorOutput.setFont(new java.awt.Font("Gabriola", 1, 24)); // NOI18N
    numeratorOutput.setForeground(new java.awt.Color(255, 102, 51));

    denominatorOutput.setEditable(false);
    denominatorOutput.setColumns(10);
    denominatorOutput.setFont(new java.awt.Font("Gabriola", 1, 24)); // NOI18N
    denominatorOutput.setForeground(new java.awt.Color(255, 102, 51));

    jLabel3.setFont(new java.awt.Font("Tempus Sans ITC", 1, 36)); // NOI18N
    jLabel3.setText("=");

    userNumeratorInput.setFont(new java.awt.Font("Gabriola", 1, 24)); // NOI18N
    userNumeratorInput.setForeground(new java.awt.Color(255, 102, 51));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(155, 155, 155)
                            .addComponent(jLabel1))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(75, 75, 75)
                            .addComponent(jLabel2)))
                    .addGap(0, 82, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jSeparator1)
                        .addComponent(userDenominatorInput, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                        .addComponent(userNumeratorInput))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addGap(29, 29, 29)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jSeparator2)
                        .addComponent(denominatorOutput, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(numeratorOutput, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addGap(18, 18, 18)
            .addComponent(jLabel2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(userNumeratorInput, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(userDenominatorInput, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(denominatorOutput, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(numeratorOutput, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(50, 50, 50)))
            .addGap(22, 22, 22))
    );

    pack();
}// </editor-fold>                        
**public void userDenominatorInputKeyReleased(java.awt.event.KeyEvent evt) {
    double userDenominator = Double.parseDouble(userDenominatorInput.getText());
    double userNumerator = Double.parseDouble(userNumeratorInput.getText());
    calculate(userNumerator, userDenominator);
    }
public void userNumeratorInputKeyReleased(java.awt.event.KeyEvent evt) {
    double userDenominator = Double.parseDouble(userDenominatorInput.getText());
    double userNumerator = Double.parseDouble(userNumeratorInput.getText());
    calculate(userNumerator, userDenominator);
    }
public void calculate(double userNumerator, double userDenominator) {
        System.out.println(userNumeratorInput);
        double a = userDenominator;
        double b = userNumerator % userDenominator;
        double c;
        do {
            c = a % b;
            a = b;
            b = c;
        } while (a != 0);
        int greatestCommonDenominator = (int) c;
        double numeratorAnswer = userNumerator / greatestCommonDenominator;
        double denominatorAnswer = userDenominator / greatestCommonDenominator;
        numeratorOutput.setText(Double.toString(numeratorAnswer));
        denominatorOutput.setText(Double.toString(denominatorAnswer));
}**

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JTextField denominatorOutput;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JTextField numeratorOutput;
public javax.swing.JTextField userDenominatorInput;
private javax.swing.JTextField userNumeratorInput;
// End of variables declaration                   

}

¿Fue útil?

Solución 2

MadProgrammer gave me the answer I needed that was causing the output of properties rather than a string. This Problem is solved by doing the .getText method as described here:

Don't use System.out.println(userNumeratorInput), it's calling userNumeratorInput.toString()which prints the properties of the text field, use ystem.out.println(userNumeratorInput.getText()) to print the text content of the field – MadProgrammer

Here is the correct code from my form class

package reducefraction;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class NewJFrame extends javax.swing.JFrame {

    double userDenominator, userNumerator;

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {

        initComponents();
//        userDenominatorInput.addActionListener(new MyTextActionListener());
        userDenominatorInput.getDocument().addDocumentListener(new MyDocumentListener());
        userDenominatorInput.getDocument().putProperty("name", "Text Field");

        /*    userDenominatorInput.addKeyListener(new java.awt.event.KeyAdapter() {
         public void keyReleased(java.awt.event.KeyEvent evt) {
         userDenominatorInputKeyReleased(evt);
         }
         });
         */
        //        userNumeratorInput.addActionListener(new MyTextActionListener());
        userNumeratorInput.getDocument().addDocumentListener(new MyDocumentListener());
        userNumeratorInput.getDocument().putProperty("name", "Text Field");

        /*    
         userNumeratorInput.addKeyListener(new java.awt.event.KeyAdapter() {
         public void keyReleased(java.awt.event.KeyEvent evt) {
         userNumeratorInputKeyReleased(evt);
         }
         });
         */
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        userDenominatorInput = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        numeratorOutput = new javax.swing.JTextField();
        denominatorOutput = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        userNumeratorInput = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Reduce Fractions");

        jLabel2.setText("This program will reduce a fraction to its lowest terms");

        userDenominatorInput.setFont(new java.awt.Font("Gabriola", 1, 24)); // NOI18N
        userDenominatorInput.setForeground(new java.awt.Color(255, 102, 51));

        numeratorOutput.setEditable(false);
        numeratorOutput.setColumns(10);
        numeratorOutput.setFont(new java.awt.Font("Gabriola", 1, 24)); // NOI18N
        numeratorOutput.setForeground(new java.awt.Color(255, 102, 51));

        denominatorOutput.setEditable(false);
        denominatorOutput.setColumns(10);
        denominatorOutput.setFont(new java.awt.Font("Gabriola", 1, 24)); // NOI18N
        denominatorOutput.setForeground(new java.awt.Color(255, 102, 51));

        jLabel3.setFont(new java.awt.Font("Tempus Sans ITC", 1, 36)); // NOI18N
        jLabel3.setText("=");

        userNumeratorInput.setFont(new java.awt.Font("Gabriola", 1, 24)); // NOI18N
        userNumeratorInput.setForeground(new java.awt.Color(255, 102, 51));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(155, 155, 155)
                                .addComponent(jLabel1))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(75, 75, 75)
                                .addComponent(jLabel2)))
                        .addGap(0, 82, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jSeparator1)
                            .addComponent(userDenominatorInput, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                            .addComponent(userNumeratorInput))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jSeparator2)
                            .addComponent(denominatorOutput, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(numeratorOutput, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(userNumeratorInput, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(userDenominatorInput, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(denominatorOutput, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(numeratorOutput, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)))
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>                        
 /*   public void userDenominatorInputKeyReleased(java.awt.event.KeyEvent evt) {
     double userDenominator = Double.parseDouble(userDenominatorInput.getText());
     double userNumerator = Double.parseDouble(userNumeratorInput.getText());
     calculate(userNumerator, userDenominator);
     }
     */
    /*    public void userNumeratorInputKeyReleased(java.awt.event.KeyEvent evt) {
     double userDenominator = Double.parseDouble(userDenominatorInput.getText());
     double userNumerator = Double.parseDouble(userNumeratorInput.getText());
     calculate(userNumerator, userDenominator);
     }
     */

    public void update() {
        if (!userDenominatorInput.getText().isEmpty() && !userNumeratorInput.getText().isEmpty()) {
            double userDenominator = Double.parseDouble(userDenominatorInput.getText());
            double userNumerator = Double.parseDouble(userNumeratorInput.getText());
            calculate(userNumerator, userDenominator);
        }
    }

    public void calculate(double userNumerator, double userDenominator) {

        if (Double.toString(userNumerator).contains(".")) {
            int decimalLocationuserNumerator = Double.toString(userNumerator).lastIndexOf(".");
            String decimalsuserNumerator = Double.toString(userNumerator).substring(decimalLocationuserNumerator + 1, Double.toString(userNumerator).length());
            int magnitudeUserNumerator = decimalsuserNumerator.length();
            userNumerator = userNumerator * Math.pow(10, magnitudeUserNumerator);
            userDenominator = userDenominator * Math.pow(10, magnitudeUserNumerator);
        }

        if (Double.toString(userDenominator).contains(".")) {
            int decimalLocationUserDenominator = Double.toString(userDenominator).lastIndexOf(".");
            String decimalsUserDenominator = Double.toString(userDenominator).substring(decimalLocationUserDenominator + 1, Double.toString(userDenominator).length());
            int magnitudeUserDenominator = decimalsUserDenominator.length();
            userDenominator = userDenominator * Math.pow(10, magnitudeUserDenominator);
            userNumerator = userNumerator * Math.pow(10, magnitudeUserDenominator);
        }

        double a = userNumerator;
        double b = userDenominator;
        double c = a % b;

        while (c > 1) {
            c = a % b;
            a = b;
            b = c;
        }

        int greatestCommonDenominator = (int) a;

        int numeratorAnswer = (int) userNumerator / greatestCommonDenominator;
        int denominatorAnswer = (int) userDenominator / greatestCommonDenominator;

        numeratorOutput.setText(Integer.toString(numeratorAnswer));
        denominatorOutput.setText(Integer.toString(denominatorAnswer));

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JTextField denominatorOutput;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField numeratorOutput;
    public javax.swing.JTextField userDenominatorInput;
    private javax.swing.JTextField userNumeratorInput;
    // End of variables declaration                   

    private class MyDocumentListener implements DocumentListener {

        public MyDocumentListener() {
        }

        @Override
        public void insertUpdate(DocumentEvent de) {
            update();

        }

        @Override
        public void removeUpdate(DocumentEvent de) {
            update();

        }

        @Override
        public void changedUpdate(DocumentEvent de) {
            update();

        }
    }
}

Otros consejos

The "probable" cause is the use of KeyListener on the text fields.

The main problem is you are not taking into account what should occur if one of the fields is empty, "" is not a valid number.

Don't use KeyListeners on text fields, if you want to perform real time updates on fields, you should be using a DocumentListener instead. KeyListener won't be notified if the the user pastes content into the field...

You should also consider using a JSpinner or a JFormattedField or even a DocumentFilter to restrict what the user can actually type into the fields

Something like...

if (!userDenominatorInput.getText().isEmpty() && !userNumeratorInput.getText().isEmpty()) {

    double userDenominator = Double.parseDouble(userDenominatorInput.getText());
    double userNumerator = Double.parseDouble(userNumeratorInput.getText());
    calculate(userNumerator, userDenominator);

}

Should help...

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top