Home > front end >  else if statement java gui wont execute
else if statement java gui wont execute

Time:05-16

I have been trying to make a java GUI distance converter. My else if statement won't execute, only the first if statement execute. Does anyone know why i wont executed? Below is my coding and the output.

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;



public class DistanceCalculator implements ActionListener  {
    private JFrame frame;
    private JLabel lbl1;
    private JTextField dtextfield;
    private JRadioButton cm,cf,ci;
    private JButton bcalculate,bclose;
    private JPanel centerp,southp,northp;
    
    
    DistanceCalculator(){
        frame = new JFrame("Distance Converter" );
        frame.setSize(400, 150);
        init();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
    }
    
    void init() {
        northp = new JPanel();
        northp.setLayout(new FlowLayout());
        
        centerp = new JPanel();
        centerp.setLayout(new FlowLayout());
        
        southp = new JPanel();
        southp.setLayout(new FlowLayout());
        
        lbl1 = new JLabel("Enter a distance in kilometers");
        northp.add(lbl1);
            
        dtextfield = new JTextField(15);
        northp.add(dtextfield);
        
        cm = new JRadioButton("Convert to miles");
        centerp.add(cm);
        
        cf = new JRadioButton("Convert to feet");
        centerp.add(cf);
        
        ci = new JRadioButton("Convert to inches");
        centerp.add(ci);
        
        ButtonGroup group = new ButtonGroup();
        group.add(cm);
        group.add(cf);
        group.add(ci);
        
        bcalculate = new JButton("Calculate");
        bcalculate.addActionListener(this);
        southp.add(bcalculate);
        
        bclose = new JButton("Close");
        bclose.addActionListener(this);
        southp.add(bclose);
        
        frame.add(centerp, BorderLayout.CENTER);
        frame.add(southp, BorderLayout.SOUTH);
        frame.add(northp,BorderLayout.NORTH);
        
    }
    public static void main(String[] args) {
        DistanceCalculator gui = new DistanceCalculator();
    }
    
    public void actionPerformed(ActionEvent e) {
        if (cm.isSelected() ) {
            if (e.getSource() == bcalculate ){
                double d = Double.parseDouble(dtextfield.getText());
                double d2 =  d* 0.6214;
                String str2 = String.valueOf(d2); 

                JOptionPane.showMessageDialog(null,d "\tKilometer is\t"   (str2)   "\tMiles");
                
            } else if(cf.isSelected()) { 
                 if (e.getSource() == bcalculate ){
                     double dd = Double.parseDouble(dtextfield.getText());
                     double d22 =  dd* 3281.0;
                     String str22 = String.valueOf(d22); 

                     JOptionPane.showMessageDialog(null,dd "\tKilometer is\t"   (str22)   "\tFeet");
                
                
                
            }
        }
            else if(ci.isSelected()) { 
             if (e.getSource() == bcalculate ){
                    double ddd = Double.parseDouble(dtextfield.getText());
                    double d222 =  ddd* 3281.0;
                    String str222 = String.valueOf(d222); 

                    JOptionPane.showMessageDialog(null,ddd "\tKilometer is\t"   (str222)   "\tInches");
            }
            }
            else {
                System.exit(0);
                
            }
        
        }
    }
}

Below is the output that im getting when i check the second and third radio button,input some number and clicked submit, the jOptionPane wont appear Output

CodePudding user response:

There was a mistake in the if...else statement bracket matching. use VScode's (any other IDE's) Bracket Pair Colorizer extension if you face this type of issue. by the way, I have corrected the mistake, kindly take a look at the following code.

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;

public class Main11 implements ActionListener  {
    private JFrame frame;
    private JLabel lbl1;
    private JTextField dtextfield;
    private JRadioButton cm,cf,ci;
    private JButton bcalculate,bclose;
    private JPanel centerp,southp,northp;
    
    
    Main11(){
        frame = new JFrame("Distance Converter" );
        frame.setSize(400, 150);
        init();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        
    }
    
    void init() {
        northp = new JPanel();
        northp.setLayout(new FlowLayout());
        
        centerp = new JPanel();
        centerp.setLayout(new FlowLayout());
        
        southp = new JPanel();
        southp.setLayout(new FlowLayout());
        
        lbl1 = new JLabel("Enter a distance in kilometers");
        northp.add(lbl1);
            
        dtextfield = new JTextField(15);
        northp.add(dtextfield);
        
        cm = new JRadioButton("Convert to miles");
        centerp.add(cm);
        
        cf = new JRadioButton("Convert to feet");
        centerp.add(cf);
        
        ci = new JRadioButton("Convert to inches");
        centerp.add(ci);
        
        ButtonGroup group = new ButtonGroup();
        group.add(cm);
        group.add(cf);
        group.add(ci);
        
        bcalculate = new JButton("Calculate");
        bcalculate.addActionListener(this);
        southp.add(bcalculate);
        
        bclose = new JButton("Close");
        bclose.addActionListener(this);
        southp.add(bclose);
        
        frame.add(centerp, BorderLayout.CENTER);
        frame.add(southp, BorderLayout.SOUTH);
        frame.add(northp,BorderLayout.NORTH);
        
    }
    public static void main(String[] args) {
        Main11 gui = new Main11();
    }
    
    public void actionPerformed(ActionEvent e) {
        if (cm.isSelected() ) 
        {
            if (e.getSource() == bcalculate )
            {
                double d = Double.parseDouble(dtextfield.getText());
                double d2 =  d* 0.6214;
                String str2 = String.valueOf(d2); 

                JOptionPane.showMessageDialog(null,d "\tKilometer is\t"   (str2)   "\tMiles");
                
            }
        }
        else if(cf.isSelected()) 
        { 
                 if (e.getSource() == bcalculate ){
                     double dd = Double.parseDouble(dtextfield.getText());
                     double d22 =  dd* 3281.0;
                     String str22 = String.valueOf(d22); 

                     JOptionPane.showMessageDialog(null,dd "\tKilometer is\t"   (str22)   "\tFeet");        
            }
        }
        else if(ci.isSelected()) 
        { 
            if (e.getSource() == bcalculate )
            {
                    double ddd = Double.parseDouble(dtextfield.getText());
                    double d222 =  ddd* 3281.0;
                    String str222 = String.valueOf(d222); 

                    JOptionPane.showMessageDialog(null,ddd "\tKilometer is\t"   (str222)   "\tInches");
            }
        }
        else 
        {
            System.exit(0);
                
        }       
    }
}

CodePudding user response:

From what I see, you have a wrong if hierarchy. Your is:

if (cm.isSelected() ) {
    if (e.getSource() == bcalculate ) {
    } else if (cf.isSelected()) { 
        if (e.getSource() == bcalculate ) {   
        }
    } else if (ci.isSelected()) { 
        if (e.getSource() == bcalculate) {
        }
    } else {
    }
}

It should be:

if (cm.isSelected() ) {
    if (e.getSource() == bcalculate ) {
    }
} else if (cf.isSelected()) {
    if (e.getSource() == bcalculate ) {   
    }
} else if (ci.isSelected()) { 
    if (e.getSource() == bcalculate) {
    }
} else {
}

You would have spotted the mistake withg proper code formatting.

  • Related