Search code examples
javaswingactionlistenerjtextfield

Trying to only allow for numbers to be entered into JTextField and non-numbers be set as 0


Having trouble guys this is for my Fahrenheit-Celsius converter class, I can't seem to get my code to recognize the following which makes sure that only numbers are entered into the text field and if not, set as 0:

try {
            Double.parseDouble(fahr.getText());
        } catch (NumberFormatException e) {
            fahr.setText("0");
        }

I think it is because of the placement of it within my class. Please check out my full class (I placed it towards the bottom of the code):

public class Temperature implements ActionListener  {

    private static JTextField fahr;
    private static JTextField cels;
    private JFrame frame;
    private JButton first;
    private JButton second;
    private double fahrVal;
    private Double fahrConv;
    private double celsVal;
    private double celsConv;
    private NumberFormat amountFormat;


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Temperature window = new Temperature();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Temperature() {
        initialize();
        first.addActionListener((ActionListener) this);
        second.addActionListener((ActionListener) this);
    }

    private void initialize() { 
        amountFormat = NumberFormat.getNumberInstance();
        frame = new JFrame("raB eltiT");
        frame.setBounds(100, 50, 350, 350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        first = new JButton("...To Celsius");
        first.setBounds(130, 60, 100, 50);
        first.setBackground(Color.cyan);
        first.setActionCommand("click");
        first.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                JButton src = (JButton) ae.getSource();
                //System.out.println("Button labeled " + src.getText() + " was clicked");
            }
        });
        first.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent me) {
                 cels.setText(String.valueOf(fahrConv));
            }
        });


        frame.getContentPane().add(first);

        second = new JButton("...To Fahrenheit");
        second.setBounds(130, 230, 100, 50);
        second.setBackground(Color.pink);
        second.setActionCommand("instead");
        frame.getContentPane().add(second);
        second.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent me) {
                 fahr.setText(String.valueOf(celsConv));
            }
        });

        // Build the Fahrenheit side

        JLabel fLabel = new JLabel("Fahrenheit");
        fahr = new JTextField("32");
        fahr.setPreferredSize(new Dimension(50,50));
        fahr.setHorizontalAlignment(JTextField.CENTER);
        JPanel fPanel = new JPanel();
        fPanel.setBounds(40, 120,100,100);
        fPanel.add(fLabel);
        fPanel.add(fahr);
        frame.getContentPane().add(fPanel);


        JLabel cLabel = new JLabel("Celsius");
        cels = new JTextField("0");
        cels.setPreferredSize(new Dimension(50,50));
        cels.setHorizontalAlignment(JTextField.CENTER);
        JPanel cPanel = new JPanel();
        cPanel.setBounds(220, 120, 100,100);
        cPanel.add(cLabel);
        cPanel.add(cels);
        frame.getContentPane().add(cPanel); 


        // Put it all up on the screen

        frame.setVisible(true);  

        try {// if is number
            Double.parseDouble(fahr.getText());
        } catch (NumberFormatException e) {
            fahr.setText("0");
        }
    }
    public void actionPerformed(ActionEvent event) {
         String fahrText = fahr.getText();
         fahrVal = Double.parseDouble(fahrText);
         fahrConv = ((fahrVal - 32)*5)/9;
         fahrConv = Double.parseDouble(new DecimalFormat("##.#").format(fahrConv));

        String celsText = cels.getText();
        celsVal = Double.parseDouble(celsText);
        celsConv = 32 + (celsVal * 9 / 5);
        celsConv = Double.parseDouble(new DecimalFormat("##.#").format(celsConv));


    }
}

Solution

  • The problem is not so much what you're doing but rather where and when you're doing it. You're calling

    try {// if is number
        Double.parseDouble(fahr.getText());
    } catch (NumberFormatException e) {
        fahr.setText("0");
    }
    

    in the code where you create the GUI, and this then happens before the user has any chance of inputting data into the JTextField, making this code useless. A better solution is to put this code into a listener that responds when the user is ready to submit the data, such as at the start of your ActionListener -- then call return:

    Something like:

    public void actionPerformed(ActionEvent event) {
        try {
            Double.parseDouble(fahr.getText());
        } catch (NumberFormatException e) {
            fahr.setText("0");
            // display a JOptionPane error message so the user knows there's been a problem
            return; // so the code below isn't called if input in error
        }
        String fahrText = fahr.getText();
        fahrVal = Double.parseDouble(fahrText);
        fahrConv = ((fahrVal - 32)*5)/9;
        fahrConv = Double.parseDouble(new DecimalFormat("##.#").format(fahrConv));
    
        String celsText = cels.getText();
        celsVal = Double.parseDouble(celsText);
        celsConv = 32 + (celsVal * 9 / 5);
        celsConv = Double.parseDouble(new DecimalFormat("##.#").format(celsConv));
    }
    

    Other options:

    • Use a JFormattedTextField
    • Or use a JSpinner
    • Or use a DocumentListener

    Other unrelated issues:

    • Don't add MouseListeners to JButtons but rather use ActionListeners. Else the listener will not behave correctly. For instance, a MouseListener still will respond if the button has been disabled, whereas an ActionListener will correctly not respond in this situation.
    • Avoid null layouts and absolute positioning as this leads to rigid GUI's that are difficult to enhance, debug and that look sort-of OK on one platform and awful on all others.