• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

GUI control

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();

}
}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd suggest you create a panel/list for each of the 3 fooditems.
add these panels to a cardlayout panel.
this cardlayout panel will be in the frame's 'center'

clicking the menuItems will show the respective panel/list
and update a number of the current showing panel (for button actionListener)

create another panel for the buttons add and delete
add this panel to the frame's 'south'

in the button's actionListener, update (add/delete) the list indicated
by the number generated from the menuItem click.

might be easier to have a JList[] array, and the number from the menuItem click
is the JList[] array index.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic