need help in controlling my program flow.i have sample code below:1. want a method to handle add food items and subtract food items. 2. a method to get the complete food item in my program. am finding it difficult to organize my program flow and actions. somebody should help me out.thanks in advance.
code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class FoodCheck extends JFrame{
private DefaultListModel carbohydrateFood;
private JList list;
private JMenu foodClass;
private JMenuItem carbohydrateItem,proteinItem,vitaminItem;
private JMenuBar bar;
public FoodCheck(){
super("Nutritional food");
setJMenuBar(bar);
bar=new JMenuBar();
foodClass=new JMenu("FOOD CLASS");
foodClass.addSeparator();
carbohydrateItem=new JMenuItem("CARBOHYDRATE");
carbohydrateItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Carbohydrate();
}
}
);
proteinItem=new JMenuItem("PROTEIN");
vitaminItem=new JMenuItem("VITAMINS");
foodClass.add(carbohydrateItem);
foodClass.add(proteinItem);
foodClass.add(vitaminItem);
bar.add(foodClass);
add(bar,"North");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,400);
setResizable(false);
}
public void Carbohydrate(){
//super("Carbohydrate Foods");
carbohydrateFood=new DefaultListModel();
carbohydrateFood.addElement("Rice");
carbohydrateFood.addElement("Maize");
list=new JList(carbohydrateFood);
//allow user to select only one food at a time
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//create JButton for adding food
JButton addButton=new JButton("Add Food");
addButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
//prompt user for new food name
String name=JOptionPane.showInputDialog(FoodCheck.this,"Enter");
//add new food to model
carbohydrateFood.addElement(name);
}
}
);
//create JButton for removing selected food
JButton removeButton=new JButton("Remove Food");
removeButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
//remove selected food from model
carbohydrateFood.removeElement(list.getSelectedValue());
}
}
);
//lay out GUI components
JPanel inputPanel=new JPanel();
inputPanel.add(addButton);
inputPanel.add(removeButton);
//Container container=getContentPane();
add(list,BorderLayout.CENTER);
add(inputPanel,BorderLayout.SOUTH);
}
public static void main(String arg[]){
new FoodCheck();
}
}