The Artima Developer Community
Sponsored Link

Java Answers Forum
New to Java confusion with output/CheckBox and GUI?

1 reply on 1 page. Most recent reply: Mar 14, 2002 9:13 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Tim O'B

Posts: 1
Nickname: timob12
Registered: Mar, 2002

New to Java confusion with output/CheckBox and GUI? Posted: Mar 14, 2002 8:35 PM
Reply to this message Reply
Advertisement
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 again ******
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[] = {"Filet Mignon", "Chicken Cheese Steak", "Tacos", "Ribs"};
private JCheckBox mashed, cole, baked, french;
private String output = "";
private String diners[] = new String[100]; //set maximum Diner amounts allowed to 100

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

Container container = getContentPane();


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

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


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

getParty.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent actionEvent)
{

partyNum = Integer.parseInt(partyNumField.getText());
String ans=JOptionPane.showInputDialog(null, "Total Number is party is: "
+ partyNum + " is this correct?\n\n" + "Enter 1 to continue\n"
+ "Enter 2 to cancel\n");
if (ans.equals("1")) {
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("Please enter Diner's name");
dinerLabel.setEnabled(false);
dinerName = new JTextField(30);
dinerName.setEnabled(false);
continueOrder = new JButton("continue");
continueOrder.setEnabled(false);

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


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

entreeOrder = new JLabel("Please choose an entree");
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("Mashed Potatoes");
middlePanel.add(mashed);
mashed.setEnabled(false);
cole = new JCheckBox("Cole Slaw");
middlePanel.add(cole);
cole.setEnabled(false);
baked = new JCheckBox("Baked Beans");
middlePanel.add(baked);
baked.setEnabled(false);
french = new JCheckBox("FrenchFries");
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("NEXT DINER");
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("");
}
}
); // 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, "Final Order of Party",
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[] = {"Filet Mignon", "Chicken Cheese Steak", "Tacos", "Ribs"};
private JCheckBox mashed, cole, baked, french;
private String output = "";
private String diners[] = new String[100]; //set maximum Diner amounts allowed to 100

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

Container container = getContentPane();


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

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


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

getParty.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent actionEvent)
{

partyNum = Integer.parseInt(partyNumField.getText());
String ans=JOptionPane.showInputDialog(null, "Total Number is party is: "
+ partyNum + " is this correct?\n\n" + "Enter 1 to continue\n"
+ "Enter 2 to cancel\n");
if (ans.equals("1")) {
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("Please enter Diner's name");
dinerLabel.setEnabled(false);
dinerName = new JTextField(30);
dinerName.setEnabled(false);
continueOrder = new JButton("continue");
continueOrder.setEnabled(false);

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


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

entreeOrder = new JLabel("Please choose an entree");
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("Mashed Potatoes");
middlePanel.add(mashed);
mashed.setEnabled(false);
cole = new JCheckBox("Cole Slaw");
middlePanel.add(cole);
cole.setEnabled(false);
baked = new JCheckBox("Baked Beans");
middlePanel.add(baked);
baked.setEnabled(false);
french = new JCheckBox("FrenchFries");
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("NEXT DINER");
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("");
}
}
); // 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);

}
}
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: New to Java confusion with output/CheckBox and GUI? Posted: Mar 14, 2002 9:13 PM
Reply to this message Reply
Looks like you have two different class definitions for Menu. Maybe you can repost just the one you are using? Also, if you use the java tags (see Formatting Your Post at the bottom of the page, when you are submitting your post), the code will be a lot easier for people to read.

Flat View: This topic has 1 reply on 1 page
Topic: lil more on Arraylist NoteBook Previous Topic   Next Topic Topic: Why arrays start with index 0 everywhere?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use