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.