I am very new to this site and a novice programmer so I'm hoping someone here can help me with the problem I'm facing. I am making a simple program that uses a card layout to select/enter several user-specific options that will be stored in string variables and then after the last question, used to give a unique answer. Right now the program advances slide's successfully and displays the proper question, however it does not clear the JRadioButton from the first card and keeps using this radio button instead of the desired component for the duration of the program. For example, The second and third card should have JTextField's while the 4th card should have a JComboBox. I have two main class files, one that is the actual frame and the other that is displayed below which does the work behind the scenes. For future reference, I plan to modify this into an applet once it is working properly so any advice on that would be great. Any help will be greatly appreciated, thanks.
I believe the problem may stem from the creation of multiple card objects here, the ScreenPanel objects each take the same parameters but depending on the different card, a different Component needs to be produced.
// set up questions
String question1 = "Sex: ";
String[] responses1 = {"Female", "Male"};
ask[0] = new ScreenPanel(question1, responses1);
String question2 = "Height in inches: ";
String[] responses2 = new String[1];
ask[1] = new ScreenPanel(question2, responses2);
String question3 = "Weight in pounds: ";
String[] responses3 = new String[1];
ask[2] = new ScreenPanel(question3, responses3);
String question4 = "Event: ";
String[] responses4 = {"Shot Put", "Discus Throw", "Long Jump", "Triple Jump", "High Jump", "Pole Vault", "4 x 800 Relay", "100 Meter Hurdles", "100 Meter Dash", "4 x 200 Relay",
"1600 Meter Run", "4 x 100 Relay", "400 Meter Dash", "300 Meter Hurdles", "800 Meter Run", "200 Meter Dash", "3200 Meter Run", "4 x 400 Relay"};
ask[3] = new ScreenPanel(question4, responses4);
String question5 = "Distance(inches) or Time(seconds)";
ask[4] = new ScreenPanel(question5, new String[1]);
ask[4].setFinalQuestion(true);
addListeners();
}
The Screen Panel class here creates each card, could be a problem with the conditionals but not sure.
class ScreenPanel extends JPanel{
JLabel question;
JRadioButton[] response1;
JTextField response2;
JTextField response3;
JComboBox response4;
JTextField response5;
JButton nextButton = new JButton("Next");
JButton finalButton = new JButton("Finish");
String textResponse1, textResponse2, textResponse3, textResponse4, textResponse5;
ScreenPanel(String ques, String[] resp){
super();
setSize(320, 260);
question = new JLabel(ques);
JPanel sub1 = new JPanel();
JPanel sub2 = new JPanel();
sub1.add(question);
if (TrackAndField.currentScreen == 0){
response1 = new JRadioButton[resp.length];
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < resp.length; i++){
response1[i] = new JRadioButton(resp[i], false);
group.add(response1[i]);
sub2.add(response1[i]);
}
// textResponse1 = group.getSelection().toString();
}
if (TrackAndField.currentScreen == 1){
sub2.remove(response1[0]);
sub2.remove(response1[1]);
response2 = new JTextField(4);
KeyAdapter monitor = new KeyAdapter() {
public void keyTyped(KeyEvent event){
textResponse2 = response2.getText();
}
};
sub2.add(response2);
}
if (TrackAndField.currentScreen == 2){
sub2.remove(response2);
response3 = new JTextField(10);
KeyAdapter monitor = new KeyAdapter() {
public void keyTyped(KeyEvent event){
textResponse3 = response3.getText();
}
};
sub2.add(response3);
}
if (TrackAndField.currentScreen == 3){
sub2.remove(response3);
response4 = new JComboBox(resp);
sub2.add(response4);
textResponse4 = response4.getSelectedItem().toString();
}
Solved by adding an extra string parameter as an ID for the component and used that for the conditionals instead of currentScreen
I've been searching around for days trying to find the answer to this, and I can't find out what's wrong. What I want to do is make it so the top JLabel (called display) align to the right and the bottom JLabel (called notice) to align to the left. Neither seems to want to do either. From what I've read, what I have should work, but it doesn't. Help?
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
public class Calculator {
private static JButton clear, add, subtract, multiply, divide, equals, point, zero, one, two, three, four, five, six, seven, eight, nine;
private static JLabel display, notice, blank1, blank2, blank3;
private static JPanel mainPanel, buttonPanel, topLabel, bottomLabel;
public static void goGUI() {
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(600,300));
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
frame.setContentPane(mainPanel);
Border empty = BorderFactory.createEmptyBorder(10,10,10,10);
mainPanel.setBorder(empty);
buttonPanel = new JPanel(new GridLayout(5,4, 5,5));
Border buttonBorder = BorderFactory.createEmptyBorder(10,0,10,0);
buttonPanel.setBorder(buttonBorder);
topLabel = new JPanel();
bottomLabel = new JPanel();
clear = new JButton("C");
add = new JButton("+");
subtract = new JButton("-");
multiply = new JButton("*");
divide = new JButton("/");
equals = new JButton("=");
point = new JButton(".");
zero = new JButton("0");
one = new JButton("1");
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
// Here I added ActionListeners to all the buttons...
display = new JLabel("0");
display.setAlignmentX(Component.RIGHT_ALIGNMENT);
notice = new JLabel("*Maximum 19 digits - Order of operations not taken into account*");
notice.setAlignmentX(Component.LEFT_ALIGNMENT);
blank1 = new JLabel();
blank2 = new JLabel();
blank3 = new JLabel();
buttonPanel.add(clear);
buttonPanel.add(blank1);
buttonPanel.add(blank2);
buttonPanel.add(blank3);
buttonPanel.add(seven);
buttonPanel.add(eight);
buttonPanel.add(nine);
buttonPanel.add(divide);
buttonPanel.add(four);
buttonPanel.add(five);
buttonPanel.add(six);
buttonPanel.add(multiply);
buttonPanel.add(one);
buttonPanel.add(two);
buttonPanel.add(three);
buttonPanel.add(subtract);
buttonPanel.add(zero);
buttonPanel.add(point);
buttonPanel.add(equals);
buttonPanel.add(add);
topLabel.add(display);
bottomLabel.add(notice);
mainPanel.add(topLabel);
mainPanel.add(buttonPanel);
mainPanel.add(bottomLabel);
frame.pack();
frame.setVisible(true);
} //end goGUI
//ActionListener classes went here...
public static void main(String[] args) {
try {
// Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
}
catch (Exception e) {}
goGUI();
} //end main
} //end Calculator
I removed all ActionListener stuff for clarity. But this is the layout that I can't fix.
Consider having topLabel not use the default FlowLayout but rather something that makes its contents fill it up such as BorderLayout.
topLabel.setLayout(new BorderLayout());
Next make sure that you set the display's horizontal alignment, not its alignmentX to the right:
display.setHorizontalAlignment(SwingConstants.RIGHT);
Similar changes should be made for your notice JLabel and its container.
I add few line of code because I like BoxLayout. I have almost the same problems in align components few hours ago.
If we want to acheive something like this
// +----------------------------------+
// | Label1 | | Label2 |
// +----------------------------------+
You have only to put a "Box.createHorizontalGlue()" between the two.
If instead we want something like this:
// +----------------------------------+
// | Label1 | |
// |----------+ +----------|
// | | Label2 |
// +----------------------------------+
We have to put the labels inside two differen JPanel, setting a different AlignmentX to them.
This is because in the layout all the components should have the same alignmentX. To avoid this restriction we can do something like this:
// +-----------------------------------------+
// | Label1 | label1 is inside a JPanel |
// +----------+ - - - - - - - - - - - - - - -+
// + - - - - - - - - - - - - - - -+----------+
// | label2 also | Label2 |
// +-----------------------------------------+
(don't mind about the space between the two panel, is only for graphic)
This can be done with this code:
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.PAGE_AXIS));
JLabel lbl1 = new JLabel("Label1");
JPanel p1 = new JPanel();
p1.setOpaque(false);
p1.setLayout(new BorderLayout(0, 0));
p1.add(lbl1, BorderLayout.CENTER);
JLabel lbl2 = new JLabel("Label1");
JPanel p2 = new JPanel();
p2.setOpaque(false);
p2.setLayout(new BorderLayout(0, 0));
p2.add(lbl2, BorderLayout.CENTER);
mainPanel.add(p1);
mainPanel.add(p2);
I hope this is useful.
You can simply use BorderLayout() :
public static void goGUI() {
....
topLabel = new JPanel(new BorderLayout());
bottomLabel = new JPanel(new BorderLayout());
....
topLabel.add(display, BorderLayout.EAST);
bottomLabel.add(notice, BorderLayout.WEST);
}
Also remove these calls :
display.setAlignmentX(Component.RIGHT_ALIGNMENT);
notice.setAlignmentX(Component.LEFT_ALIGNMENT);
I'm a Java newbie. I am in a college intro Java course, and I'm at a point now where none of my programs are working.
This point is at the GUI creation chapter. I'm not sure what I'm doing wrong. I've been working on this non-stop for over a week and no progress. I've searched far and wide across the Java forums, YouTube videos, previous StackOverflow questions, Oracle demos and tutorials; I updated my Java JDK to 1.7 just in case, and nothing has worked.
First I should mention that I was previously having a problem where it would say "javaw.exe has encountered a problem and needs to close," but this proglem seems to have disappeared after updating to 1.7 I'm okay on that. I am running the program in the IDE Eclipse helios.
I decided to try and change the program to a JApplet to see if this would help, but it hasn't. I tried debugging but it won't even let me finish debugging. What am I doing wrong? When I run the JApplet, I get ouput on the console, but StackOverflow won't let me post it.
Here is a copy of my code. The javadocs aren't finished, and I apologize for this. I have just made so many changes trying to fix things that I haven't been able to keep up at the pace of creating all the javadocs. I should also warn you that I did create an input format validation (do-while try-catch), but I have omitted this here because first I'm trying to figure out what I'm doing wrong before moving on to adding this. I also apologize for the sloppy indentations in the code. somehow it didn't transfer very well on to the StackOverflow website.
package assignment12;
/*
* File: CalculateBill.java
* ------------------------
* This program calculates a table's bill at a restaurant.
* The program uses a frame user interface with the following components:
* input textfields for the waiter name and table number
* four interactive combo boxes for each category of the menu containing all the menu items
* a listbox that keeps track of menu item that is ordered
* buttons that allow the user to delete an item or clear all the items on the listbox
* a textarea that displays the table and waiter name entered
* a label that refreshes the total, subtotal, and tax when an item is entered or deleted
* a restaurant logo for "Charlotte's Apple tree restaurant"
*/
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* CalculateBill.java uses these additional files:
* images/appletree.gif
*/
/**
*
* #version 1.7
* #since 2011-11-21
*/
#SuppressWarnings("serial")
public class CalculateBill extends JPanel implements ActionListener {
JComboBox beverageList;
JComboBox appetizerList;
JComboBox dessertList;
JComboBox maincourseList;
JLabel restaurantLogo;
JTextField textTableNum; //where the table number is entered
JTextField waiterName; //where the waiter name is entered
JTextArea textArea;//where the waiter name and table number appears at the bottem
JLabel waiter;
JLabel table;
DefaultListModel model;//model
JList list; // list
static int tableNum = 0; // setting table number to an integer outside the range (1-10) keeps loop going until
// valid user entry in textTableNum textfield
String tn; //string value of table number
String wn; //string value of waiter name
JScrollPane scrollpane;
public double subtotal = 0.00;
public double tax = 0.00;
public double total = 0.00;
JLabel totallabel;
CalculateBill() {
super(new BorderLayout());
//create and set up the window.
JFrame frame = new JFrame("Charlotte's Appletree Restaurant");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(300, 600));
String[] beverages = {"Beverages", "Soda", "Tea", "Coffee", "Mineral Water", "Juice", "Milk"};
String[] appetizers = {"Appetizers", "Buffalo Wings", "Buffalo Fingers", "Potato Skins", "Nachos", "Mushroom Caps", "Shrimp Cocktail", "Chips and Salsa"};
String[] maincourses = {"Main Courses", "Seafood Alfredo", "Chicken Alfredo", "Chicken Picatta", "Turkey Club", "Lobster Pie", "Prime Rib", "Shrimp Scampi", "Turkey Dinner", "Stuffed Chicken"};
String[] desserts = {"Desserts", "Apple Pie", "Sundae", "Carrot Cake", "Mud Pie", "Apple Crisp"};
/*create the combo boxes, selecting the first item at index 0.
indices start at 0, so so 0 is the name of the combo box*/
// beverages combobox
beverageList = new JComboBox(beverages);
beverageList.setEditable(false);
beverageList.setSelectedIndex(0);
add(new JLabel(" Beverages:"), BorderLayout.CENTER);
add(beverageList, BorderLayout.CENTER);
// appetizers combobox
appetizerList = new JComboBox(appetizers);
appetizerList.setEditable(false);
appetizerList.setSelectedIndex(0);
add(new JLabel(" Appetizers:"), BorderLayout.CENTER);
add(appetizerList, BorderLayout.CENTER);
// maincourses combobox
maincourseList = new JComboBox(maincourses);
maincourseList.setEditable(false);
maincourseList.setSelectedIndex(0);
add(new JLabel(" Main courses:"), BorderLayout.CENTER);
add(maincourseList, BorderLayout.CENTER);
// desserts combox
dessertList = new JComboBox(desserts);
dessertList.setEditable(false);
dessertList.setSelectedIndex(0);
add(new JLabel(" Desserts:"), BorderLayout.CENTER);
add(dessertList, BorderLayout.CENTER);
// listbox
model = new DefaultListModel();
JPanel listPanel = new JPanel();
list = new JList(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
// list box continued
JScrollPane listPane = new JScrollPane();
listPane.getViewport().add(list);
listPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
listPanel.add(listPane);
// total label
totallabel = new JLabel(setTotalLabelAmount());
add((totallabel), BorderLayout.SOUTH);
totallabel.setVisible(false);
// sets up listbox buttons
add(new JButton("Delete"), BorderLayout.SOUTH);
add(new JButton("Clear All"), BorderLayout.SOUTH);
// sets up restaurant logo
restaurantLogo = new JLabel();
restaurantLogo.setFont(restaurantLogo.getFont().deriveFont(Font.ITALIC));
restaurantLogo.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
restaurantLogo.setPreferredSize(new Dimension(123, 200 + 10));
ImageIcon icon = createImageIcon("images/appletree.gif");
restaurantLogo.setIcon(icon);
restaurantLogo.setText("Charlotte's Apple Tree Restaurant");
add((restaurantLogo), BorderLayout.NORTH);
// sets up the label next to textfield for table number
table = new JLabel(" Enter Table Number (1-10):");
/**
* #throws InputMismatchException if the textfield entry is not an integer
*
*/
// sets up textfield next to table lable
table.setLabelFor(textTableNum);
add((table), BorderLayout.NORTH);
// sets up label for waiter name
waiter = new JLabel(" Enter Waiter Name: ");
// sets up textfield next to waiter lable
waiter.setLabelFor(waiterName);
add((waiter), BorderLayout.NORTH);
// listens to the textfields
textTableNum.addActionListener(this);
waiterName.addActionListener(this);
// sets up textarea
textArea = new JTextArea(5, 10);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
// lays out listpanel
listPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(listPane, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
scrollPane.setVisible(true);
}
private double getPrices(String item) {
// create hashmap to store menu items with their corresponding prices
HashMap<String, Double> hm = new HashMap<String, Double>();
// put elements to the map
hm.put("Soda", new Double(1.95));
hm.put("Tea", new Double(1.50));
hm.put("Coffee", new Double(1.25));
hm.put("Mineral Water", new Double(2.95));
hm.put("Juice", new Double(2.50));
hm.put("Milk", new Double(1.50));
hm.put("Buffalo Wings", new Double(5.95));
hm.put("Buffalo Fingers", new Double(6.95));
hm.put("Potato Skins", new Double(8.95));
hm.put("Nachos", new Double(8.95));
hm.put("Mushroom Caps", new Double(10.95));
hm.put("Shrimp Cocktail", new Double(12.95));
hm.put("Chips and Salsa", new Double(6.95));
hm.put("Seafood Alfredo", new Double(15.95));
hm.put("Chicken Alfredo", new Double(13.95));
hm.put("Chicken Picatta", new Double(13.95));
hm.put("Turkey Club", new Double(11.95));
hm.put("Lobster Pie", new Double(19.95));
hm.put("Prime Rib", new Double(20.95));
hm.put("Shrimp Scampi", new Double(18.95));
hm.put("Turkey Dinner", new Double(13.95));
hm.put("Stuffed Chicken", new Double(14.95));
hm.put("Apple Pie", new Double(5.95));
hm.put("Sundae", new Double(3.95));
hm.put("Carrot Cake", new Double(5.95));
hm.put("Mud Pie", new Double(4.95));
hm.put("Apple Crisp", new Double(5.95));
double price = hm.get(item);
return price;
}
/**
* validates that the correct path for the image was found to prevent crash
*
* #param path is the icon path of the restaurant logo
*
* #return nothing if you can't find the image file
*
* #return imageIcon(imgURL) the path to image if you can find it
*/
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = CalculateBill.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
JOptionPane.showMessageDialog(null, "Couldn't find file: "
+ path, "image path error", JOptionPane.ERROR_MESSAGE);
return null;
}
}
//Listens to the combo boxes
private void getSelectedMenuItem(JComboBox cb) {
String mnItem = (String) cb.getSelectedItem();
updateListBox(mnItem);
}
/**
* updates the list box
*
* #param name the element to be added to the list box
*/
private void updateListBox(String name) {
totallabel.setVisible(false);
model.addElement(name);
Addition(getPrices(name));
totallabel.setVisible(true);
}
void showWaiterAndTableNum() {
textArea.append("Table Number: " + tn + " Waiter: " + wn);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
/**
* adds to the subtotal/total calculator.
*
* #param s The name of the menu item which will be used to access its hashmap key value.
*
*/
private void Addition(double addedP) {
subtotal = subtotal + addedP;
tax = .0625 * subtotal;
total = subtotal + tax;
}
/**
* subtracts from to the subtotal/total calculator.
*
* #param subtractedp The price of the menu item which will be used to access its hashmap key value.
*
*/
// sets up the 'total' label which shows subtotal, tax, total
private void Subtraction(double subtractedp) {
subtotal = subtotal - subtractedp;
tax = subtotal * .0625;
total = subtotal + tax;
}
private void resetCalculator() {
subtotal = 0.00;
tax = 0.00;
total = 0.00;
}
// listens to list buttons
#Override
public void actionPerformed(ActionEvent e) {
JTextField tf = (JTextField) e.getSource();
if (tf.equals("textTableNum")) {
tn = tf.getText();
} else if (tf.equals("waiterName")) {
wn = tf.getText();
}
String cmd = e.getActionCommand();
if (cmd.equals("Delete")) {
totallabel.setVisible(false);
ListSelectionModel selmodel = list.getSelectionModel();
int index = selmodel.getMinSelectionIndex();
String foodName = (String) list.getSelectedValue();
Subtraction(getPrices(foodName));
totallabel.setVisible(true);
//subtracts from the subtotal
if (index >= 0) {
model.remove(index);
}
} else if (cmd.equals("Clear all")) {
model.clear();
resetCalculator();
}
}
//combobox mouse listener
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
JComboBox cb = (JComboBox) e.getSource();
getSelectedMenuItem(cb);
}
}
private String setTotalLabelAmount() {
String totlab = "Subtotal: $ " + subtotal + " Tax: $" + tax + "\n" + "Total: $ " + total;
return totlab;
}
}
**my applet:**
package assignment12;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
#SuppressWarnings("serial")
public class Main extends JApplet {
/**
* #param args
*/
public void init() {
// schedule job for event-dispatching
//while showing Aplication GUI
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
} catch (Exception e) {
System.err.println("createAndShowGUI didn't complete successfully");
}
}
// create and show GuI
private void createAndShowGUI() {
//Create and set up the content pane.
CalculateBill newContentPane = new CalculateBill();
newContentPane.setOpaque(true); //content panes must be opaque
setContentPane(newContentPane);
}
}
Well, on the one hand "what isn't wrong" comes to mind, but in truth you've come quite away.
There are several issues going on here. I've only taken a stab at some of them.
It would be easier to subclass JPanel than JFrame. Create a panel, and add it to a frame. See Eric's answer on how to structure that, and see the main method below.
You have two missing JTextFields: textTableNum, and waiterName. First thing I got build this up is an NPE at these two locations. Next, your constraints are wrong for the GridBag. I am not a GridBag person. GridBag gives me hives, so I can't say what's wrong with those, so I eliminated the constraints and replaced them with 'null'. Once I did this, I at least got a frame and a GUI.
Next, all of your BorderLayout code is wrong. When you specify a location on a Border Layout, that's exactly what you are doing -- specifying a location. If you put 3 things in BorderLayout.NORTH, they will all go up there, and layer on top of each other (that it you will see only one of them). So, clearly, all of your layout code needs a lot of work.
After some butchery, we have this:
package soapp;
/*
* File: CalculateBill.java
* ------------------------
* This program calculates a table's bill at a restaurant.
* The program uses a frame user interface with the following components:
* input textfields for the waiter name and table number
* four interactive combo boxes for each category of the menu containing all the menu items
* a listbox that keeps track of menu item that is ordered
* buttons that allow the user to delete an item or clear all the items on the listbox
* a textarea that displays the table and waiter name entered
* a label that refreshes the total, subtotal, and tax when an item is entered or deleted
* a restaurant logo for "Charlotte's Apple tree restaurant"
*/
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* CalculateBill.java uses these additional files:
* images/appletree.gif
*/
/**
*
* #version 1.7
* #since 2011-11-21
*/
#SuppressWarnings("serial")
public class CalculateBill extends JPanel implements ActionListener {
JComboBox beverageList;
JComboBox appetizerList;
JComboBox dessertList;
JComboBox maincourseList;
JLabel restaurantLogo;
JTextField textTableNum; //where the table number is entered
JTextField waiterName; //where the waiter name is entered
JTextArea textArea;//where the waiter name and table number appears at the bottem
JLabel waiter;
JLabel table;
DefaultListModel model;//model
JList list; // list
static int tableNum = 0; // setting table number to an integer outside the range (1-10) keeps loop going until
// valid user entry in textTableNum textfield
String tn; //string value of table number
String wn; //string value of waiter name
JScrollPane scrollpane;
public double subtotal = 0.00;
public double tax = 0.00;
public double total = 0.00;
JLabel totallabel;
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame("SO App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CalculateBill cb = new CalculateBill();
frame.getContentPane().add(cb);
frame.pack();
frame.setVisible(true);
}
CalculateBill() {
super(new BorderLayout());
JPanel panel;
//create and set up the window.
// JFrame frame = new JFrame("Charlotte's Appletree Restaurant");
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setPreferredSize(new Dimension(300, 600));
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
String[] beverages = {"Beverages", "Soda", "Tea", "Coffee", "Mineral Water", "Juice", "Milk"};
String[] appetizers = {"Appetizers", "Buffalo Wings", "Buffalo Fingers", "Potato Skins", "Nachos", "Mushroom Caps", "Shrimp Cocktail", "Chips and Salsa"};
String[] maincourses = {"Main Courses", "Seafood Alfredo", "Chicken Alfredo", "Chicken Picatta", "Turkey Club", "Lobster Pie", "Prime Rib", "Shrimp Scampi", "Turkey Dinner", "Stuffed Chicken"};
String[] desserts = {"Desserts", "Apple Pie", "Sundae", "Carrot Cake", "Mud Pie", "Apple Crisp"};
/*create the combo boxes, selecting the first item at index 0.
indices start at 0, so so 0 is the name of the combo box*/
// beverages combobox
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
beverageList = new JComboBox(beverages);
beverageList.setEditable(false);
beverageList.setSelectedIndex(0);
panel.add(new JLabel(" Beverages:"), BorderLayout.CENTER);
panel.add(beverageList, BorderLayout.CENTER);
add(panel);
// appetizers combobox
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
appetizerList = new JComboBox(appetizers);
appetizerList.setEditable(false);
appetizerList.setSelectedIndex(0);
panel.add(new JLabel(" Appetizers:"), BorderLayout.CENTER);
panel.add(appetizerList, BorderLayout.CENTER);
// maincourses combobox
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
maincourseList = new JComboBox(maincourses);
maincourseList.setEditable(false);
maincourseList.setSelectedIndex(0);
panel.add(new JLabel(" Main courses:"), BorderLayout.CENTER);
panel.add(maincourseList, BorderLayout.CENTER);
add(panel);
// desserts combox
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
dessertList = new JComboBox(desserts);
dessertList.setEditable(false);
dessertList.setSelectedIndex(0);
panel.add(new JLabel(" Desserts:"), BorderLayout.CENTER);
panel.add(dessertList, BorderLayout.CENTER);
add(panel);
// listbox
model = new DefaultListModel();
JPanel listPanel = new JPanel();
list = new JList(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
// list box continued
JScrollPane listPane = new JScrollPane();
listPane.getViewport().add(list);
listPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
listPanel.add(listPane);
add(listPanel);
// total label
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
totallabel = new JLabel(setTotalLabelAmount());
panel.add((totallabel), BorderLayout.SOUTH);
totallabel.setVisible(false);
add(panel);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
// sets up listbox buttons
panel.add(new JButton("Delete"), BorderLayout.SOUTH);
panel.add(new JButton("Clear All"), BorderLayout.SOUTH);
add(panel);
// sets up restaurant logo
// restaurantLogo = new JLabel();
// restaurantLogo.setFont(restaurantLogo.getFont().deriveFont(Font.ITALIC));
// restaurantLogo.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
// restaurantLogo.setPreferredSize(new Dimension(123, 200 + 10));
// ImageIcon icon = createImageIcon("images/appletree.gif");
// restaurantLogo.setIcon(icon);
// restaurantLogo.setText("Charlotte's Apple Tree Restaurant");
// add((restaurantLogo), BorderLayout.NORTH);
// sets up the label next to textfield for table number
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
table = new JLabel(" Enter Table Number (1-10):");
/**
* #throws InputMismatchException if the textfield entry is not an integer
*
*/
// sets up textfield next to table lable
textTableNum = new JTextField();
table.setLabelFor(textTableNum);
panel.add((table), BorderLayout.NORTH);
panel.add(textTableNum, BorderLayout.NORTH);
add(panel);
// sets up label for waiter name
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
waiter = new JLabel(" Enter Waiter Name: ");
waiterName = new JTextField();
// sets up textfield next to waiter lable
waiter.setLabelFor(waiterName);
panel.add((waiter), BorderLayout.NORTH);
panel.add(waiterName, BorderLayout.NORTH);
add(panel);
// listens to the textfields
textTableNum.addActionListener(this);
waiterName.addActionListener(this);
// sets up textarea
textArea = new JTextArea(5, 10);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
// lays out listpanel
// listPanel.setLayout(new GridBagLayout());
// GridBagConstraints c = new GridBagConstraints();
// c.gridwidth = GridBagConstraints.REMAINDER;
// c.fill = GridBagConstraints.HORIZONTAL;
// add(listPane, c);
// add(listPane, null);
// c.fill = GridBagConstraints.BOTH;
// c.weightx = 1.0;
// c.weighty = 1.0;
// add(scrollPane, c);
add(scrollPane, null);
scrollPane.setVisible(true);
}
private double getPrices(String item) {
// create hashmap to store menu items with their corresponding prices
HashMap<String, Double> hm = new HashMap<String, Double>();
// put elements to the map
hm.put("Soda", new Double(1.95));
hm.put("Tea", new Double(1.50));
hm.put("Coffee", new Double(1.25));
hm.put("Mineral Water", new Double(2.95));
hm.put("Juice", new Double(2.50));
hm.put("Milk", new Double(1.50));
hm.put("Buffalo Wings", new Double(5.95));
hm.put("Buffalo Fingers", new Double(6.95));
hm.put("Potato Skins", new Double(8.95));
hm.put("Nachos", new Double(8.95));
hm.put("Mushroom Caps", new Double(10.95));
hm.put("Shrimp Cocktail", new Double(12.95));
hm.put("Chips and Salsa", new Double(6.95));
hm.put("Seafood Alfredo", new Double(15.95));
hm.put("Chicken Alfredo", new Double(13.95));
hm.put("Chicken Picatta", new Double(13.95));
hm.put("Turkey Club", new Double(11.95));
hm.put("Lobster Pie", new Double(19.95));
hm.put("Prime Rib", new Double(20.95));
hm.put("Shrimp Scampi", new Double(18.95));
hm.put("Turkey Dinner", new Double(13.95));
hm.put("Stuffed Chicken", new Double(14.95));
hm.put("Apple Pie", new Double(5.95));
hm.put("Sundae", new Double(3.95));
hm.put("Carrot Cake", new Double(5.95));
hm.put("Mud Pie", new Double(4.95));
hm.put("Apple Crisp", new Double(5.95));
double price = hm.get(item);
return price;
}
/**
* validates that the correct path for the image was found to prevent crash
*
* #param path is the icon path of the restaurant logo
*
* #return nothing if you can't find the image file
*
* #return imageIcon(imgURL) the path to image if you can find it
*/
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = CalculateBill.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
JOptionPane.showMessageDialog(null, "Couldn't find file: " + path, "image path error", JOptionPane.ERROR_MESSAGE);
return null;
}
}
//Listens to the combo boxes
private void getSelectedMenuItem(JComboBox cb) {
String mnItem = (String) cb.getSelectedItem();
updateListBox(mnItem);
}
/**
* updates the list box
*
* #param name the element to be added to the list box
*/
private void updateListBox(String name) {
totallabel.setVisible(false);
model.addElement(name);
Addition(getPrices(name));
totallabel.setVisible(true);
}
void showWaiterAndTableNum() {
textArea.append("Table Number: " + tn + " Waiter: " + wn);
textArea.setCaretPosition(textArea.getDocument().getLength());
}
/**
* adds to the subtotal/total calculator.
*
* #param s The name of the menu item which will be used to access its hashmap key value.
*
*/
private void Addition(double addedP) {
subtotal = subtotal + addedP;
tax = .0625 * subtotal;
total = subtotal + tax;
}
/**
* subtracts from to the subtotal/total calculator.
*
* #param subtractedp The price of the menu item which will be used to access its hashmap key value.
*
*/
// sets up the 'total' label which shows subtotal, tax, total
private void Subtraction(double subtractedp) {
subtotal = subtotal - subtractedp;
tax = subtotal * .0625;
total = subtotal + tax;
}
private void resetCalculator() {
subtotal = 0.00;
tax = 0.00;
total = 0.00;
}
// listens to list buttons
#Override
public void actionPerformed(ActionEvent e) {
JTextField tf = (JTextField) e.getSource();
if (tf.equals("textTableNum")) {
tn = tf.getText();
} else if (tf.equals("waiterName")) {
wn = tf.getText();
}
String cmd = e.getActionCommand();
if (cmd.equals("Delete")) {
totallabel.setVisible(false);
ListSelectionModel selmodel = list.getSelectionModel();
int index = selmodel.getMinSelectionIndex();
String foodName = (String) list.getSelectedValue();
Subtraction(getPrices(foodName));
totallabel.setVisible(true);
//subtracts from the subtotal
if (index >= 0) {
model.remove(index);
}
} else if (cmd.equals("Clear all")) {
model.clear();
resetCalculator();
}
}
//combobox mouse listener
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
JComboBox cb = (JComboBox) e.getSource();
getSelectedMenuItem(cb);
}
}
private String setTotalLabelAmount() {
String totlab = "Subtotal: $ " + subtotal + " Tax: $" + tax + "\n" + "Total: $ " + total;
return totlab;
}
}
This is not representative code of, well, anything. It's sole capability is that it functions to at least show something like what you were looking for.
Also note I commented out all of the logo code, since I didn't have the image file - I just punted on that part for the sake of expediency.
What it does is it create a JPanel subclass. That panel has a BoxLayout, that lines up vertically. You misunderstand how the BorderLayout works by putting multiple components in to the same slot. For example, you but both the 'Delete' and 'Clear All' JButton in to the BorderLayout.SOUTH. That means they both consume the same space, in the end one ends up on top of the other so it looks like only one component.
A BoxLayout has a Flow, that is as you add components to them, the components do not overlap and get added and expand the space. The basic JPanels layout is a vertical BoxLayout, so that as components are added, they will stack and appear in rows.
Next, a common idiom in Swing, especially with hand made GUIs, is you use JPanels as containers. If you ever used a drawing program and use the Group function to turn, say, 4 lines in to a single box, the JPanel in Swing and layouts works the same way. Each JPanel has its own layout, and then once completed, the JPanel is treated as a whole.
So, here I used the BoxLayout again, only this time using the LINE_AXIS style, which puts components end to end, laying them out in a line.
You can see I create several JPanels, set the layout to the BoxLayout, then add components to each individual JPanel, and then add these JPanels to the master JPanel. The components in the individual JPanels lay out end to end, but as they are added to the master JPanel, its BoxLayout stacks them top to bottom.
Note that the remnants of your BorderLayout details remains, because I didn't clean that up. I would remove all of that.
This is where I stopped, as the code compiles and shows the GUI, which should at least give you something to work with other than 400 lines of black box "nothings happening" code. It likely does not look quite like you imagined, that wasn't my goal either. I would play with the JPanel and sub-JPanel idiom and the layout managers until you get closer to what you want. The Java Swing tutorial linked from the Javadoc for Swing is pretty good, if a bit scattered. So, more reading is in store.
As others have mentioned, when starting out something like thing, you would be best to start off small (like getting 2 of the drop down boxes to work). Then you would only have a couple of things to focus on to get those to work, and then you can build on that toward a final project.
I hope this is helpful to get you on your way.
It seems there are a bunch of attributes, starting with waiterName that are declared but not initialized.
First off you should switch back from an Applet to a regular app. And create the JFrame in the main method instead of doing it in your panel:
Change your main class to:
public class Main{
public static void main(String[] args){
JFrame frame = new JFrame("Charlotte's Appletree Restaurant");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CalculateBill newContentPane = new CalculateBill();
frame.getContentPane().add(newContentPane);
frame.setPreferredSize(new Dimension(300, 600));
frame.setVisible(true);
}
The main part of your problem however appears to be that you aren't initializaing textTableNum before you try to add an ActionListener to it.
A NullPointerException happens whenever you try to access a variable that isn't referencing an actual object. To diagnosis them go go the line number and see what variables are being dereferenced on that line.
In this case the stack trace tells you that the exception is happening on line 186 of CalculateBill.java,, which is:
textTableNum.addActionListener(this);
So based on that line of code the only variable that can be a problem is textTableNum. You need to make sure that variable is initialized before you use it.
Your second problem that you mentioned in the comments is because you started adding components to the CalculateBill panel using GridBagConstraints when you declared CalculateBill to use a BorderLayout.
Also, I noticed you add several components to the same BorderLayout region. You can't do this. You can add only one. If you want multiple components in the NORTH region then you need to create a sub-panel, layout the components you want there, then add the sub-panel to the NORTH of the BorderLayout.