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

  • SEARCH
  • Home
  • 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 8692005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:14:00+00:00 2026-06-13T00:14:00+00:00

I am making a little program that calculates the calories burned based on 5

  • 0

I am making a little program that calculates the calories burned based on 5 different physical activities selected from a combobox in java. the user is given 5 options: bowling, archery, lacross, wrestling, and painting. here is the website with the given numbers calculated, and the user enters his weight and how long he worked out, in either minutes or hours, and when he/she hits calculate it should display the total calories burned.

http://www.nutristrategy.com/activitylist4.htm

my problem is I dont know how to make the fields update based on the selection, in other words, I need to create a method that waits for the selection, then updates the formula to calculate their calories burned based on their weight. how do I do this? Do i make an if else statement that says, “if archery selected and there is a number in the weight textbox?” then use this formula? I dont know how to check what the selection was from the combobox.

here is my code.

import java.awt.Component;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class calorie extends JFrame {

    public calorie() {
        JLabel titleLabel = new JLabel("Calories Burned during excercise");
        titleLabel.setFont(new Font("Serif", Font.BOLD, 20));
        titleLabel.setForeground(Color.blue);

        final JLabel distLabel = new JLabel("Choose an Activity");
        final JTextField distText = new JTextField(8);
        String[] actStrings = {"bowling", "lacrosse", "wrestling", "painting", "archery"};
        JComboBox jComboBox1 = new JComboBox(actStrings);
        jComboBox1.setEditable(true);

        final JLabel fuelLabel = new JLabel("Current weight:");
        final JTextField fuelText = new JTextField(8);

        final JLabel actTime = new JLabel("How long did you work out:");
        final JTextField time = new JTextField(8);
        String[] timeStrings = {"Minutes", "Hours"};
        JComboBox jComboBox2 = new JComboBox(timeStrings);
        jComboBox2.setEditable(true);

        final JLabel mpgLabel = new JLabel("Calories burned = ");
        final JTextField mpgText = new JTextField(8);

        JButton clearButton = new JButton("Clear");
        JButton calcButton = new JButton("Calculate");
        final JLabel labelPic1;
        final ImageIcon[] imgIcons;
        String[] fileNames = {"pics/bowling.jpg", "pics/lacrosse.jpg",
            "pics/wrestling.jpg", "pics/painting.jpg", "pics/archery.jpg"};
        imgIcons = new ImageIcon[fileNames.length];

        BufferedImage image = null;
        for (int i = 0; i < fileNames.length; i++) {
            try {
                image = ImageIO.read(new File(fileNames[i]));
            } catch (IOException ex) {
                System.out.println(ex.toString());
                System.out.println(fileNames[i]);
                JOptionPane.showMessageDialog(null, ex.toString() + " " + fileNames[i]);
                System.exit(0); // exit program
            }
            Image newimg = image.getScaledInstance(200, 200, java.awt.Image.SCALE_SMOOTH);
            imgIcons[i] = new ImageIcon(newimg);
        }
        labelPic1 = new JLabel(imgIcons[0]);
        setResizable(false);
        JPanel p = new JPanel(new MigLayout("", "[][][][][]",
                "[][][][][][][][][][][][][][][]"));
        p.setBackground(Color.WHITE);
        setContentPane(p);
        p.add(labelPic1, "cell 0 3 1 3");
        p.add(calcButton, "cell 0 9");
        p.add(titleLabel, "cell 1 0 2 1");
        p.add(distLabel, "cell 0 2");
        p.add(fuelLabel, "cell 1 5");
        p.add(mpgLabel, "cell 1 9");
        p.add(jComboBox1, "cell 1 2");
        p.add(fuelText, "cell 1 7");
        p.add(jComboBox2, "cell 1 7");
        p.add(actTime, "cell 0 7");
        p.add(distText, "cell 1 5");
        p.add(mpgText, "cell 1 9");

        jComboBox1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                //JComboBox jComboBox1 = (JComboBox)event.getSource();
                JComboBox jComboBox1 = (JComboBox) event.getSource();
                if (jComboBox1.getSelectedItem() == "bowling") {
                    labelPic1.setIcon(imgIcons[0]);
                }
                if (jComboBox1.getSelectedItem() == "lacrosse") {
                    labelPic1.setIcon(imgIcons[1]);
                }
                if (jComboBox1.getSelectedItem() == "wrestling") {
                    labelPic1.setIcon(imgIcons[2]);
                }
                if (jComboBox1.getSelectedItem() == "painting") {
                    labelPic1.setIcon(imgIcons[3]);
                }
                if (jComboBox1.getSelectedItem() == "archery") {
                    labelPic1.setIcon(imgIcons[4]);
                }
            }
        });

        calcButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {

                if (isNumeric(distText.getText()) && isNumeric(fuelText.getText())) {
                    double fuel;
                    double dist;
                    double result;
                    fuel = Double.parseDouble(fuelText.getText());
                    dist = Double.parseDouble(distText.getText());
                    result = dist / fuel;
                    mpgText.setText(String.format("%f", result));
                } else {
                    JOptionPane.showMessageDialog(null, "Enter distance traveled and fuel used");
                }
            }
        });
        setTitle("Calorie Calculator");
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private static boolean isNumeric(String text) {
        try {
            Double.parseDouble(text);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            // "javax.swing.plaf.metal.MetalLookAndFeel");
                            // "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                            UIManager.getCrossPlatformLookAndFeelClassName());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                new calorie().setVisible(true);
            }
        });
    }
}
  • 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-06-13T00:14:01+00:00Added an answer on June 13, 2026 at 12:14 am

    Do i make an if else statement that says, “if archery selected and there is a number in the weight textbox?” then use this formula?

    Yes.

    I have not read all your code, but you want to do something like this, by adding a call to an update textbox method updateTextBox(); and remove JComboBox jComboBox1 = (JComboBox) event.getSource();.

    Edit, code below updated:

    public void actionPerformed(ActionEvent event)
    {
        updateTextBox();
    }
    

    Below is the method where you can change and update the textbox:

    public void updateTextBox()
    {
        if (jComboBox1.getSelectedIndex() == 0) //bowling
        {
            labelPic1.setIcon(imgIcons[0]);
            //Update textbox here to show results for item 0
        }
        else if (jComboBox1.getSelectedIndex() == 1) //lacrosse
        {
            labelPic1.setIcon(imgIcons[1]);
            //Update textbox here to show results for item 1
        }
    }
    

    To make this work you need to make your “jComboBox1” static.
    So add this line above your fist class static JComboBox jComboBox1 = null

    Then replace JComboBox jComboBox1 = new ComboBox(actStrings);

    with this jComboBox1 = new JComboBox(actStrings); since we have already created the combobox earlier with static JComboBox jComboBox1 = null

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

Sidebar

Related Questions

I want to start making a little window-based program that runs on both Linux
Now I'm making a little program in Java which must read a really big
I'm making a program that has little programs inside of it, and I've come
I'm making a program that takes names from the user seperated by commas. The
So what I need is basically to create a java program that runs from
I'm making a little program that will crawl my hard drive and present a
I have been making a little program that needs to read a list of
I'm making this little program because I was bored and I ran across this
I`m making an little php server that has 2 clients, on connects to the
I'm making a little message sending module. It'll handle queuing messages from a request

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.