I am new to programming and just started last week, so all the java mumbojumbo is quite confusing to me. I was able to create a option pane for my BMI program that asks which unit system(metric/imperial) with radiobuttons and this determines which calculation to perform when finding the BMI. this all works fine except the first optionpane doesn't close when an option is selected, how do i make it close when an option is seleceted. I want the jpane with radiobuttons to close in the do statement.
package javaapplication21;
import java.text.DecimalFormat;
import javax.swing.*;
import java.*;
public class JavaApplication21 {
public static void main(String[] args) {
JPanel jPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
JRadioButton metricButton = new JRadioButton("Metric");
metricButton.setActionCommand("Metric");
JRadioButton imperialButton = new JRadioButton("Imperial");
imperialButton.setActionCommand("Imperial");
group.add(metricButton);
group.add(imperialButton);
jPanel.add(metricButton);
jPanel.add(imperialButton);
JOptionPane.showOptionDialog(null, "Please select prefered units", "BMI Calculator", JOptionPane.DEFAULT_OPTION,JOptionPane.QUESTION_MESSAGE, null, new Object[] { metricButton, imperialButton}, null);
DecimalFormat oneDigit = new DecimalFormat("#,##0.0");
double bodyMassIndex, weight, height;
String unitsWeight = "-1", unitsHeight = "-1";
do{
if (metricButton.isSelected()){
unitsWeight = " in kg.";
unitsHeight = " in meters";
}
else if (imperialButton.isSelected()){
unitsWeight = " in lbs";
unitsHeight = " in inches";
}
}
while ("-1".equals(unitsWeight));
String weightInput = JOptionPane.showInputDialog("Please enter your weight" + unitsWeight);
String heightInput = JOptionPane.showInputDialog("Please enter your height" + unitsHeight);
if (metricButton.isSelected()){
height = Double.parseDouble(heightInput);
weight = Double.parseDouble(weightInput);
bodyMassIndex = weight / (height * height);
System.out.println("Your Body Mass Index(BMI) is " + oneDigit.format(bodyMassIndex) + "kg/m^2");
if (bodyMassIndex < 15)
System.out.println("You are starving");
else if (bodyMassIndex < 18.5)
System.out.println("You are underweight");
else if (bodyMassIndex < 25)
System.out.println("You are healthy");
else if (bodyMassIndex < 30)
System.out.println("You are obese");
else if (bodyMassIndex < 40)
System.out.println("You are morbidly obese");
else
System.out.println("You are at high risk of many health concerns");
}
else if (imperialButton.isSelected()){
height = Double.parseDouble(heightInput);
weight = Double.parseDouble(weightInput);
bodyMassIndex = (weight * 703) / (height * height);
System.out.println("Your Body Mass Index(BMI) is " + oneDigit.format(bodyMassIndex) + "kg/m^2");
if (bodyMassIndex < 15)
System.out.println("You are starving");
else if (bodyMassIndex < 18.5)
System.out.println("You are underweight");
else if (bodyMassIndex < 25)
System.out.println("You are healthy");
else if (bodyMassIndex < 30)
System.out.println("You are obese");
else if (bodyMassIndex < 40)
System.out.println("You are morbidly obese");
else
System.out.println("No more big macs!");
}
}
}
Hacked around a bit with an answer from this question here
Closing A JOptionPane Programatically
And it seemed to work.
I created a JButton to add to the list of objects in your JOptionPane. And implemented a listener that would respond to the event of someone clicking okay.
This will give the user something to do so that they know that they are using the right measurement and that they have clicked the option correctly.
Copy this over your original JOptionPane.
JButton yesButton = new JButton("Ok");
yesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Window[] windows = Window.getWindows();
for (Window window : windows) {
if (window instanceof JDialog) {
JDialog dialog = (JDialog) window;
if (dialog.getContentPane().getComponentCount() == 1
&& dialog.getContentPane().getComponent(0) instanceof JOptionPane) {
dialog.dispose();
}
}
}
}
});
JOptionPane.showOptionDialog(null, "Please select prefered units",
"BMI Calculator", JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new Object[] {
metricButton, imperialButton, yesButton }, null);
Also when you don't click OK but have the radiobutton selected the program continues to run after you click the close button. So that will be something for you to work on.
Try to find an answer to your question through the website before you ask one. But I enjoyed helping so thankyou anyway.