Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My first program - New Java - CheckBoxs and Output?

Status
Not open for further replies.

timob12

Programmer
Mar 13, 2002
3
0
0
US
My program "should"
1. Ask for totally number in party
2. Ask if this partyNumber is correct
3. Get Name
3. Get Entree Order
4. Get Side Order
5. If diner number < number in party
6. Go to Step 3.

I've been working in this for forever and I'm at the point where I'm completely confused with what I'm doing. I need a walk away from it from an hour.

The main thing I can't figure out is I continue get Diners names and there orders and present the output.
If I choose only one diner if will produce output but if I choose 2 or more people in Party I don't even get any output and I can't understand why? Also if I do say that there is only one person in the party it will produce input but for some reason it outputs the dinner entree of the Diner 3 times?? No side order gets output in my JOptionPane.

Hoping someone could help me a bit with my problems. I've reached a point where I getting completey lost. Any guidance or suggestions would be greatly appreciated.

The output which is resulting from the code is confusing me the most.

P.S. My GUI is pretty wacked but thats another issue in itself and I'll worry about that later. Thanks again.


My code ****** I think I'm kinda close to getting this but
an hitting a wall.
package finalproject;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Menu extends JFrame {
private JTextField partyNumField, dinerName;
private JComboBox orderComboBox;
private int partyNum;
private JButton getParty, continueOrder, nextDiner;
private JLabel party, companyLogo, dinerLabel, entreeOrder;
private String dinnerEntree[] = {&quot;Filet Mignon&quot;, &quot;Chicken Cheese Steak&quot;, &quot;Tacos&quot;, &quot;Ribs&quot;};
private JCheckBox mashed, cole, baked, french;
private String output = &quot;&quot;;
private String diners[] = new String[100]; //set maximum Diner amounts allowed to 100

public Menu() {
// setting up GUI
super(&quot;O'Brien Caterer - Where we make good Eats!&quot;);

Container container = getContentPane();


JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 120, 15));
companyLogo = new JLabel(&quot;Welcome to O'Brien's Caterer's&quot;);
northPanel.add(companyLogo);

party = new JLabel(&quot;Enter the Total Number in Party Please&quot;);
partyNumField = new JTextField(5);
northPanel.add(party);
northPanel.add(partyNumField);


getParty = new JButton(&quot;GO - Continue with Order&quot;);

getParty.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent actionEvent)
{

partyNum = Integer.parseInt(partyNumField.getText());
String ans=JOptionPane.showInputDialog(null, &quot;Total Number is party is: &quot;
+ partyNum + &quot; is this correct?\n\n&quot; + &quot;Enter 1 to continue\n&quot;
+ &quot;Enter 2 to cancel\n&quot;);
if (ans.equals(&quot;1&quot;)) {
continueOrder.setEnabled(true);
dinerLabel.setEnabled(true);
dinerName.setEnabled(true);

// handle continue
} else { // assume to be 2 for cancel
System.exit(0); // handle cancel
}
}
}
); // end Listener

northPanel.add(getParty);
northPanel.setPreferredSize(new Dimension(700,150));

JPanel middlePanel = new JPanel();
middlePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
dinerLabel = new JLabel(&quot;Please enter Diner's name&quot;);
dinerLabel.setEnabled(false);
dinerName = new JTextField(30);
dinerName.setEnabled(false);
continueOrder = new JButton(&quot;continue&quot;);
continueOrder.setEnabled(false);

continueOrder.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent actionEvent){
output += &quot;Diner Name\t&quot; + &quot;Entree\t&quot; + &quot;Side Dishes\n&quot;;
output += dinerName.getText();
entreeOrder.setEnabled(true);
orderComboBox.setEnabled(true);
}
}
);


middlePanel.add(dinerLabel);
middlePanel.add(continueOrder);
middlePanel.add(dinerName);

entreeOrder = new JLabel(&quot;Please choose an entree&quot;);
entreeOrder.setEnabled(false);
orderComboBox = new JComboBox(dinnerEntree);
orderComboBox.setMaximumRowCount(5);
orderComboBox.setEnabled(false);

orderComboBox.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event)
{
if (event.getStateChange() == ItemEvent.SELECTED)
output += (String)orderComboBox.getSelectedItem();
mashed.setEnabled(true);
cole.setEnabled(true);
french.setEnabled(true);
baked.setEnabled(true);
}
}
);

mashed = new JCheckBox(&quot;Mashed Potatoes&quot;);
middlePanel.add(mashed);
mashed.setEnabled(false);
cole = new JCheckBox(&quot;Cole Slaw&quot;);
middlePanel.add(cole);
cole.setEnabled(false);
baked = new JCheckBox(&quot;Baked Beans&quot;);
middlePanel.add(baked);
baked.setEnabled(false);
french = new JCheckBox(&quot;FrenchFries&quot;);
middlePanel.add(french);
french.setEnabled(false);

CheckBoxHandler handler = new CheckBoxHandler();
mashed.addItemListener(handler);
cole.addItemListener(handler);
baked.addItemListener(handler);
french.addItemListener(handler);


middlePanel.add(entreeOrder);
middlePanel.add(orderComboBox);

JPanel southPanel = new JPanel();
southPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
nextDiner = new JButton(&quot;NEXT DINER&quot;);
southPanel.add(nextDiner);
nextDiner.setEnabled(false);

nextDiner.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent actionEvent)
{
int dinerCounter = 1;

if (dinerCounter == partyNum)
displayResults();
else
++dinerCounter;
dinerName.setText(&quot;&quot;);
}
}
); // end of nextDiner actionListener button


container.add(northPanel, java.awt.BorderLayout.NORTH);
container.add(middlePanel, java.awt.BorderLayout.CENTER);
container.add(southPanel, java.awt.BorderLayout.SOUTH);
setSize(600, 500);
show();


}

// private class to handle event of choosing CheckBoxItem
private class CheckBoxHandler implements ItemListener{
private int counter = 0; // counter to enable nextDiner Button when
// two choices are selected to enable button

public void itemStateChanged(ItemEvent event){

if (event.getSource() == mashed)
if (event.getStateChange() == ItemEvent.SELECTED)
output+= (String)orderComboBox.getSelectedItem();
counter++;

if (event.getSource() == cole)
if (event.getStateChange() == ItemEvent.SELECTED)
output+= (String)orderComboBox.getSelectedItem();
counter++;
if (event.getSource() == baked)
if (event.getStateChange() == ItemEvent.SELECTED)
output+= (String)orderComboBox.getSelectedItem();
counter++;

if (event.getSource() == french)
if (event.getStateChange() == ItemEvent.SELECTED)
output+= (String)orderComboBox.getSelectedItem();
counter++;

if (counter >= 2)
{
nextDiner.setEnabled(true);

}
}
}

public void displayResults()
{
JTextArea outputArea = new JTextArea();
outputArea.setText(output);
JOptionPane.showMessageDialog(null, output, &quot;Final Order of Party&quot;,
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}


public static void main(String args[])
{
Menu application = new Menu();
application.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
}
}
);
}
}
package finalproject;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Menu extends JFrame {
private JTextField partyNumField, dinerName;
private JComboBox orderComboBox;
private int partyNum;
private JButton getParty, continueOrder, nextDiner;
private JLabel party, companyLogo, dinerLabel, entreeOrder;
private String dinnerEntree[] = {&quot;Filet Mignon&quot;, &quot;Chicken Cheese Steak&quot;, &quot;Tacos&quot;, &quot;Ribs&quot;};
private JCheckBox mashed, cole, baked, french;
private String output = &quot;&quot;;
private String diners[] = new String[100]; //set maximum Diner amounts allowed to 100

public Menu() {
// setting up GUI
super(&quot;O'Brien Caterer - Where we make good Eats!&quot;);

Container container = getContentPane();


JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 120, 15));
companyLogo = new JLabel(&quot;Welcome to O'Brien's Caterer's&quot;);
northPanel.add(companyLogo);

party = new JLabel(&quot;Enter the Total Number in Party Please&quot;);
partyNumField = new JTextField(5);
northPanel.add(party);
northPanel.add(partyNumField);


getParty = new JButton(&quot;GO - Continue with Order&quot;);

getParty.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent actionEvent)
{

partyNum = Integer.parseInt(partyNumField.getText());
String ans=JOptionPane.showInputDialog(null, &quot;Total Number is party is: &quot;
+ partyNum + &quot; is this correct?\n\n&quot; + &quot;Enter 1 to continue\n&quot;
+ &quot;Enter 2 to cancel\n&quot;);
if (ans.equals(&quot;1&quot;)) {
continueOrder.setEnabled(true);
dinerLabel.setEnabled(true);
dinerName.setEnabled(true);

// handle continue
} else { // assume to be 2 for cancel
System.exit(0); // handle cancel
}
}
}
); // end Listener

northPanel.add(getParty);
northPanel.setPreferredSize(new Dimension(700,150));

JPanel middlePanel = new JPanel();
middlePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
dinerLabel = new JLabel(&quot;Please enter Diner's name&quot;);
dinerLabel.setEnabled(false);
dinerName = new JTextField(30);
dinerName.setEnabled(false);
continueOrder = new JButton(&quot;continue&quot;);
continueOrder.setEnabled(false);

continueOrder.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent actionEvent){
output += &quot;Diner Name\t&quot; + &quot;Entree\t&quot; + &quot;Side Dishes\n&quot;;
output += dinerName.getText();
entreeOrder.setEnabled(true);
orderComboBox.setEnabled(true);
}
}
);


middlePanel.add(dinerLabel);
middlePanel.add(continueOrder);
middlePanel.add(dinerName);

entreeOrder = new JLabel(&quot;Please choose an entree&quot;);
entreeOrder.setEnabled(false);
orderComboBox = new JComboBox(dinnerEntree);
orderComboBox.setMaximumRowCount(5);
orderComboBox.setEnabled(false);

orderComboBox.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event)
{
if (event.getStateChange() == ItemEvent.SELECTED)
output += (String)orderComboBox.getSelectedItem();
mashed.setEnabled(true);
cole.setEnabled(true);
french.setEnabled(true);
baked.setEnabled(true);
}
}
);

mashed = new JCheckBox(&quot;Mashed Potatoes&quot;);
middlePanel.add(mashed);
mashed.setEnabled(false);
cole = new JCheckBox(&quot;Cole Slaw&quot;);
middlePanel.add(cole);
cole.setEnabled(false);
baked = new JCheckBox(&quot;Baked Beans&quot;);
middlePanel.add(baked);
baked.setEnabled(false);
french = new JCheckBox(&quot;FrenchFries&quot;);
middlePanel.add(french);
french.setEnabled(false);

CheckBoxHandler handler = new CheckBoxHandler();
mashed.addItemListener(handler);
cole.addItemListener(handler);
baked.addItemListener(handler);
french.addItemListener(handler);


middlePanel.add(entreeOrder);
middlePanel.add(orderComboBox);

JPanel southPanel = new JPanel();
southPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
nextDiner = new JButton(&quot;NEXT DINER&quot;);
southPanel.add(nextDiner);
nextDiner.setEnabled(false);

nextDiner.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent actionEvent)
{
int dinerCounter = 1;

if (dinerCounter == partyNum)
displayResults();
else
++dinerCounter;
dinerName.setText(&quot;&quot;);
}
}
); // end of nextDiner actionListener button


container.add(northPanel, java.awt.BorderLayout.NORTH);
container.add(middlePanel, java.awt.BorderLayout.CENTER);
container.add(southPanel, java.awt.BorderLayout.SOUTH);
setSize(600, 500);
show();


}

// private class to handle event of choosing CheckBoxItem
private class CheckBoxHandler implements ItemListener{
private int counter = 0; // counter to enable nextDiner Button when
// two choices are selected to enable button

public void itemStateChanged(ItemEvent event){

if (event.getSource() == mashed)
if (event.getStateChange() == ItemEvent.SELECTED)
output+= (String)orderComboBox.getSelectedItem();
counter++;

if (event.getSource() == cole)
if (event.getStateChange() == ItemEvent.SELECTED)
output+= (String)orderComboBox.getSelectedItem();
counter++;
if (event.getSource() == baked)
if (event.getStateChange() == ItemEvent.SELECTED)
output+= (String)orderComboBox.getSelectedItem();
counter++;

if (event.getSource() == french)
if (event.getStateChange() == ItemEvent.SELECTED)
output+= (String)orderComboBox.getSelectedItem();
counter++;

if (counter >= 2)
{
nextDiner.setEnabled(true);

}
}
}
 
Could you try to show the output? That would be helpfull.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top