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 1114695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:02:38+00:00 2026-05-17T03:02:38+00:00

My code so far: import java.awt.BorderLayout; import java.awt.Container; import javax.swing.Box; import javax.swing.JFrame; import javax.swing.JLabel;

  • 0

My code so far:

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class TestFile2 {

    public static void main(String args[]) {
        String size[] = {"Small", "Medium", "Large", "Extra Large"};
        String toppings[] = {"Cheese", "Pepperoni", "Sausage", "Spinach", "Pepperoncini"};
        JFrame f = new JFrame("Pizza");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JList list1 = new JList(size);
        JList list2 = new JList(toppings);
        Container c = f.getContentPane();
        JScrollPane sp1 = new JScrollPane(list1);
        sp1.setColumnHeaderView(new JLabel("Select Size"));
        JScrollPane sp2 = new JScrollPane(list2);
        sp2.setColumnHeaderView(new JLabel("Select Toppings. Hold Ctrl to select multiple toppings"));
        Box box = Box.createHorizontalBox();
        box.add(sp1);
        box.add(sp2);
        list1.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                JList jListSource = (JList) e.getSource();
                Object[] selection = jListSource.getSelectedValues();
                if (!e.getValueIsAdjusting()) {
                    System.out.println("----");
                    for (int i = 0; i < selection.length; i++) {
                        double costSize;
                        if (selection[i].equals("Small")) {
                            costSize = 7.00;
                        } else if (selection[i].equals("Medium")) {
                            costSize = 9.00;
                        } else if (selection[i].equals("Large")) {
                            costSize = 11.00;
                        } else {
                            costSize = 14.00;
                        }
                        System.out.println("selection = " + selection[i]);
                        System.out.println("selection = " + costSize);
                    }
                }
            }
        });
        c.add(box, BorderLayout.CENTER);
        f.setSize(
                300, 200);
        f.setVisible(
                true);
    }
}

I need to do something similar to what I did with list1.

I need to have the user select from a JList both the size of pizza and the toppings. At the end I need to be able to calculate the total cost. Each topping is $1. Any point in a certain direction would be greatly appreciated as I have been pulling my hair out for the past few hours trying different methods for this problem.

Thanks in advance.

  • 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-05-17T03:02:39+00:00Added an answer on May 17, 2026 at 3:02 am

    I hope this will help 🙂 … there are other ways to do it…. this just one of them …

    import java.awt.BorderLayout;
    import java.awt.Container;
    import javax.swing.Box;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    
    public class TestFile2 {
    
        private static JList list1;
        private static JList list2;
    
        public static void main(String args[]) {
            String size[] = {"Small", "Medium", "Large", "Extra Large"};
            String toppings[] = {"Cheese", "Pepperoni", "Sausage", "Spinach", "Pepperoncini"};
            JFrame f = new JFrame("Pizza");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            list1 = new JList(size);
            list2 = new JList(toppings);
            Container c = f.getContentPane();
            JScrollPane sp1 = new JScrollPane(list1);
            sp1.setColumnHeaderView(new JLabel("Select Size"));
            JScrollPane sp2 = new JScrollPane(list2);
            sp2.setColumnHeaderView(new JLabel("Select Toppings. Hold Ctrl to select multiple toppings"));
            Box box = Box.createHorizontalBox();
            box.add(sp1);
            box.add(sp2);
            list1.addListSelectionListener(new ListSelectionListener() 
            {
    
                public void valueChanged(ListSelectionEvent e)
                {
                    handleEvent(e);
                }
            });
            list2.addListSelectionListener(new ListSelectionListener() 
            {
    
                public void valueChanged(ListSelectionEvent e)
                {
                    handleEvent(e);
                }
            });
            c.add(box, BorderLayout.CENTER);
            f.setSize(
                    300, 200);
            f.setVisible(
                    true);
        }
    
        protected static void handleEvent(ListSelectionEvent e) 
        {
    
            double cost = 0.0;
             Object[] selection = list1.getSelectedValues();
             Object[] toppings = list2.getSelectedValues();
    
             if(toppings.length == 0)
                 System.out.println("Please select a topping");
    
             if(selection.length == 0)
                 System.out.println("Please select a size");
    
             if (!e.getValueIsAdjusting()) 
             {
                 System.out.println("----");
                 for (int i = 0; i < selection.length; i++) 
                 {
                     double costSize;
                     if (selection[i].equals("Small")) {
                        cost = 7.00;
                     } else if (selection[i].equals("Medium")) {
                        cost = 9.00;
                     } else if (selection[i].equals("Large")) {
                        cost = 11.00;
                     } else {
                        cost = 14.00;
                     }
    
                 }
    
    
                 for (int i = 0; i < toppings.length; i++) 
                 {
                    cost++;
                 }
    
    
                 System.out.println("Total = " + cost);
             }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code I have thus far: import java.io.*; class JAVAFilter implements FilenameFilter
This is my Code that I have so far: import java.util.*; public class VoteRecorder
I'm trying to hand-code a Java GUI using Swing and AWT. I'm using various
so im making a simple box from java and this is the code so
This is my code thus far: ArrayList<String> matches = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); String playString =
I have this code so far which perfectly but relies on there being a
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
This is my code so far: var n = 123456789; var d = n.toString().length;
Hey im having this code so far - (id)initWithDictionary:(NSDictionary *)aDictionary { self = [super
SO this is my code so far: JS: <script type=text/javascript> function Hide(srcField) { var

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.