aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes GUI control Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GUI control" Watch "GUI control" New topic
Author

GUI control

oghenekaro EFEKODO
Ranch Hand

Joined: Oct 10, 2005
Posts: 56
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();

}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GUI control