import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.WindowConstants;
// GUI Class
// Will contain all the code that will display my GUI
// Will use the recipe class.
public class gui {
recipe test = new recipe();
DefaultComboBoxModel recipeNames = new DefaultComboBoxModel();
ArrayList<recipe> recipes = new ArrayList<recipe>();
ArrayList<String> recipeNamesArray = new ArrayList<String>();
ImageIcon stars0 = createImageIcon("amazonstar0C.gif");
ImageIcon stars1 = createImageIcon("amazonstar1C.gif");
ImageIcon stars2 = createImageIcon("amazonstar2C.gif");
ImageIcon stars3 = createImageIcon("amazonstar3C.gif");
ImageIcon stars4 = createImageIcon("amazonstar4C.gif");
ImageIcon stars5 = createImageIcon("amazonstar5C.gif");
JLabel ratingImage = new JLabel();
JComboBox recipeList = new JComboBox(recipeNames);
// Constructor will initialize everything.
public gui() {
test.setName("test");
test.setRating(3);
test.addIngredient("1/4 cup milk");
test.addDirection("Get milk.");
test.setNote("testNote");
test.setPeople(4);
test.setTime("00:01");
recipes.add(test);
recipeNamesArray.add(test.getName());
recipeNames.addElement(test.getName());
// The main JFrame that will hold my whole GUI.
JFrame main = new JFrame("E-Chef");
// The main tabbed pane that will run my program.
// Everything is done from this.
JTabbedPane menu = new JTabbedPane();
// All 3 panels for each tab.
JPanel mainMenu = new JPanel();
mainMenu.setLayout(new BoxLayout(mainMenu, BoxLayout.X_AXIS));
JPanel add = new JPanel();
JPanel search = new JPanel();
// All 3 tabbed icons.
ImageIcon icon1 = createImageIcon("menucooking.png");
ImageIcon icon2 = createImageIcon("addcooking.png");
ImageIcon icon3 = createImageIcon("searchcooking.png");
// Each tab.
menu.addTab("Main", icon1, mainMenu, "The Main Menu");
menu.addTab("Add", icon2, add, "Add a Recipe");
menu.addTab("Search", icon3, search, "Search for Recipe");
// Panel 1 -- mainMenu
// Creating two more panels.
// One to store recipe info, the other to store 3 navigation buttons.
JPanel recipeInfo = new JPanel();
recipeInfo.setLayout(new BoxLayout(recipeInfo, BoxLayout.Y_AXIS));
JPanel navigation = new JPanel();
navigation.setLayout(new BoxLayout(navigation, BoxLayout.X_AXIS));
// Add things to each panel.
JLabel title = new JLabel("Recipe List");
recipeList.setEditable(true);
MyActionListener recipeChange = new MyActionListener();
recipeList.addActionListener(recipeChange);
JLabel rating = new JLabel("Rating");
// Add things to recipeInfo
recipeInfo.add(title);
recipeInfo.add(recipeList);
recipeInfo.add(rating);
recipeInfo.add(ratingImage);
// Add panels to mainMenu panel.
mainMenu.add(recipeInfo);
mainMenu.add(navigation);
// Add tabbed pane to the frame.
main.add(menu);
// Setting the frame visible and displaying it.
main.pack();
main.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
main.setVisible(true);
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = gui.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
class MyActionListener implements ActionListener {
Object oldItem;
public void actionPerformed(ActionEvent evt) {
JComboBox cb = (JComboBox) evt.getSource();
String newItem = (String)cb.getSelectedItem();
int position = recipeNamesArray.indexOf(newItem);
int rating = recipes.get(position).getRating();
switch (rating) {
case 0: ratingImage.setIcon(stars0); break;
case 1: ratingImage.setIcon(stars1); break;
case 2: ratingImage.setIcon(stars2); break;
case 3: ratingImage.setIcon(stars3); break;
case 4: ratingImage.setIcon(stars4); break;
case 5: ratingImage.setIcon(stars5); break;
default: ratingImage.setIcon(stars0);break;
}
}
}
}