Hi,
I have a problem with a code i have done but i just a problem..If u look at what i have attached and try to run it..It gave me a problem as i am meant to have a menu which is meant to show something inside it but the menu is coming up but with nothing inside it..I have tried adding ingredients and saving it but still nothing happens..please could anyone help me out..

I couldnt upload the last two so here they are:

UNITCONVERTER.JAVA

  import java.text.DecimalFormat;
  import java.util.HashMap;
  /*
   * The WeightConverter class provides a method to convert a unit to another specified unit
   */


  public class UnitConverter
  {
      private HashMap Units; //The collection which stores the unit objects
      private DecimalFormat myFormatter = new DecimalFormat("#.##");
      public UnitConverter()
      {
          Units=new HashMap();
          //CAPACITY
          Units.put("ml",new Unit("ml",0.001,101));
          Units.put("gills",new Unit("gills",0.142,102));
          Units.put("pt",new Unit("pt",0.568,103));
          Units.put("liter",new Unit("liter",1,104));
          Units.put("qt",new Unit("qt",1.136,105));
          Units.put("gal",new Unit("gal",4.546,106));


          //WEIGHT
          Units.put("mg",new Unit("mg",0.001,201));
          Units.put("cg",new Unit("cg",0.01,202));
          Units.put("gr",new Unit("gr",0.065,203));
          Units.put("gram",new Unit("gram",1,204));
          Units.put("dr",new Unit("dr",1.77,205));
          Units.put("oz",new Unit("oz",28.35,206));
          Units.put("kg",new Unit("kg",1000,207));
          Units.put("lb",new Unit("lb",454,208));
          Units.put("st",new Unit("st",6356,209));
        }

        /**
         * Return a String for new format "number+unit"
         * Parameters: value-the quantity of the unit
         *             unit-the current unit
         *             format-the specified unit
         */
        public String format(double value,String unit,String format)
        {
            String result=null;
            Unit CurrentUnit=(Unit) Units.get(unit.toLowerCase());
            Unit TargetUnit=(Unit) Units.get(format.toLowerCase());
            if(TargetUnit.getMetricRate()==CurrentUnit.getMetricRate())
            {
                result=value+""+unit;
            }
            else
            {
                double temp=value*CurrentUnit.getMetricRate()/TargetUnit.getMetricRate();
                result=myFormatter.format(temp)+""+format;
            }
            return result;
        }
          public double convert(double value,String unit,String format)
          {
              double result=0;
              Unit CurrentUnit=(Unit) Units.get(unit.toLowerCase());
              Unit TargetUnit=(Unit) Units.get(format.toLowerCase());
              if(TargetUnit.getMetricRate()==CurrentUnit.getMetricRate())
              {
                  result=value;
                }
                else
                {
                    double temp=value*CurrentUnit.getMetricRate()/TargetUnit.getMetricRate();
                    result=temp;
                }
                return result;
            }
        }

UNIT.JAVA

/**
 * Write a description of class Unit here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

/**
 * This is the class of Unit instance
 * The Unit will have a value which means the rate with 1 metric unit for
 * example, 1 litre,1 gram
 */

public class Unit
{
private String name;         //The name of the unit
private double MetricRate;   //The rate of this unit to metric unit
private int index;           //The index of this unit

public Unit(String name,double MetricRate,int index)
{
   this.name=name;
   this.MetricRate=MetricRate;
   this.index=index;
}

public String getName(){return name;}
public double getMetricRate(){return MetricRate;}
public int getIndex(){return index;}
public void setName(String name){this.name=name;}
public void setMetricRate(double MetricRate){this.MetricRate=MetricRate;}
public void setIndex(int index){this.index=index;}}

PLEASE TRY TO RUN EVERYTHING AND SEE WHAT IM TALKING ABOUT...thanx in advance

Edited 3 Years Ago by pyTony: fixed formatting

Attachments
/**
 * Write a description of class GUIRecipe here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */


  import java.awt.Color;
  import java.awt.Dimension;
  import java.io.FileInputStream;
  import java.io.ObjectInputStream;
  import java.util.HashMap;
  import java.util.*;
  import javax.swing.UIManager;
  import javax.swing.UnsupportedLookAndFeelException;
  import javax.swing.table.DefaultTableCellRenderer;
  import javax.swing.table.DefaultTableModel;

  /*
   * *This class builds a GUIRecipe for users to enter, edit, and delete recipes
   */
  
  public class GUIRecipe extends javax.swing.JFrame
  {
      //The TableCellRenderer for all cell, aims to make the content in the center
      private DefaultTableCellRenderer dtcr=new DefaultTableCellRenderer();
      //The TableModel is for the Recipe table
      private DefaultTableModel dtm = new DefaultTableModel(new Object [][]{{null,null}}, new String []{"Recipes", "Ingredients"});
      //The TableModel is for the ingredient table
      private DefaultTableModel dtm2 = new DefaultTableModel(new Object[][]{{null,null,null}}, new String[]{"ingredient", "Quantity", "Unit"});
      //The TableModel for the Menu table
      private DefaultTableModel dtm3=new DefaultTableModel(new Object [][]{{null, null}}, new String []{"Recipes", "People"});
      //This is the collection of the Recipe
      private HashMap recipeCollection=new HashMap();
      //Index of recipe, ingredient and the menu table
      private int r,c,r2,c2,r3,c3;
      //The Recipe of currentRecipe;
      private String currentRecipe;
      //The Collection of item in menu
      private HashMap Menus=new HashMap();
      
      public GUIRecipe()
      {
          r=0;c=0;r2=0;c2=0;//init
          //load();
          try //using MS WINDOWS LookAndFeel
          {
                 UIManager.setLookAndFeel("com.sun.java.swing.plat.windows.WindowsLookAndFeel");
            } catch (IllegalAccessException ex)
            {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex)
            {
                ex.printStackTrace();
            } catch (InstantiationException ex)
            {
                ex.printStackTrace();
            } catch (UnsupportedLookAndFeelException ex)
            {
                ex.printStackTrace();
            }
            
            initComponents();
            Inname.setText("");
            Outname.setText("");
            
            //set TableModel
            recipeTable.setModel(dtm);
            ingredientTable.setModel(dtm2);
            menuTable.setModel(dtm3);
            dtcr.setHorizontalAlignment(dtcr.CENTER);
            dtcr.setBackground(Color.white);
            
            //set column wide and TableCellRenderer
            recipeTable.getColumnModel().getColumn(0).setPreferredWidth(225);
            recipeTable.getColumnModel().getColumn(0).setCellRenderer(dtcr);
            recipeTable.getColumnModel().getColumn(1).setPreferredWidth(70);
            recipeTable.getColumnModel().getColumn(1).setCellRenderer(dtcr);
            ingredientTable.getColumnModel().getColumn(0).setCellRenderer(dtcr);
            ingredientTable.getColumnModel().getColumn(0).setPreferredWidth(180);
            ingredientTable.getColumnModel().getColumn(1).setCellRenderer(dtcr);
            ingredientTable.getColumnModel().getColumn(1).setPreferredWidth(60);
            ingredientTable.getColumnModel().getColumn(2).setCellRenderer(dtcr);
            ingredientTable.getColumnModel().getColumn(2).setPreferredWidth(50);
            menuTable.getColumnModel().getColumn(0).setCellRenderer(dtcr);
            menuTable.getColumnModel().getColumn(0).setPreferredWidth(218);
            menuTable.getColumnModel().getColumn(1).setCellRenderer(dtcr);
            menuTable.getColumnModel().getColumn(1).setPreferredWidth(80);
            
            //readRecipes();
        }
        
        /*
         * Read the file:Recipes.dat to input the saved data to recipeCollection
         */
        public void load()
        {
            FileInputStream fis=null;
            ObjectInputStream ois=null;
            try
            {
                fis=new FileInputStream("Recipes.dat");
                ois=new ObjectInputStream(fis);
                recipeCollection= (HashMap) ois.readObject();
                
            }
            catch(Exception e){System.out.println(e);}
            finally
            {
                try
                {
                    if(ois!=null) ois.close();
                }
                catch(Exception e){}
            }
        }
        
        /*
     * put data into the recipe table from recipeCollection using addRecipe()
     */
    public void readRecipes()
    {
        Set set=(Set) recipeCollection.keySet();
        Iterator it= set.iterator();
        while(it.hasNext())
        {
            String key=(String) it.next();
            addRecipe(key);
        }
            dtm.setRowCount(dtm.getRowCount()-1);
            Dimension TableSize=recipeTable.getPreferredSize();
            TableSize.setSize(TableSize.getWidth(),TableSize.getHeight()-16);
            recipeTable.setPreferredSize(TableSize);
        }

            
    /*
     * put data into the recipe table from recipeCollection
     */
    public void addRecipe(String recipename)
    {
        if(recipeCollection.containsKey(recipename))
        {
            Recipe temp=(Recipe) recipeCollection.get(recipename);
            recipeTable.setValueAt(temp.getName(),r,c);
            c++;
            recipeTable.setValueAt(temp.getIngredients().size(),r,c);
            r++;
            c=0;
            dtm.setRowCount(dtm.getRowCount()+1);
            Dimension TableSize=recipeTable.getPreferredSize();
            TableSize.setSize(TableSize.getWidth(),TableSize.getHeight()+16);
            recipeTable.setPreferredSize(TableSize);
        }
    }
    
    /**
     * put the data into the Ingredient Table
     */
    public void displayIngredient(Recipe recipe)
    {
        r2=0;c2=0;
        dtm2.setRowCount(0);
        dtm2.setRowCount(1);
        ingredientTable.setPreferredSize(new Dimension(225,16));
        
        for(int i=0;i<recipe.getIngredients().size();i++)
        {
            Ingredient temp=(Ingredient)recipe.getIngredients().get(i);
            ingredientTable.setValueAt(temp.getName(),r2,c2);
            c2++;
            ingredientTable.setValueAt(temp.getQuantity(),r2,c2);
            c2++;
            ingredientTable.setValueAt(temp.getUnit(),r2,c2);
            r2++;
            c2=0;
            
            dtm2.setRowCount(dtm2.getRowCount()+1);
            Dimension TableSize=ingredientTable.getPreferredSize();
            TableSize.setSize(TableSize.getWidth(),TableSize.getHeight()+16);
            ingredientTable.setPreferredSize(TableSize);
        }
    }
    
    /**
     * Display the Ingredient detail in Ingredient table of selected Recipe
     */
    public void displayRecipe()
    {
        try
        {
            String name=(String)recipeTable.getValueAt(recipeTable.getSelectedRow(),0);
            currentRecipe=name;
            //recipename.setText(name);
            displayIngredient((Recipe)recipeCollection.get(currentRecipe));
        }
        catch(Exception e){}
    }
       
      /**
       * Read Menu Collection data then set the data into menu table
       */
      public void displayMenu()
      {
          r3=0;c3=0;
          dtm3.setRowCount(0);
          menuTable.setPreferredSize(new Dimension(298,0));
          /*
            dtm2.setRowCount(dtm2.getRowCount()+1);
            Dimension TableSize=menuTable.getPreferredSize();
            TableSize.setSize(TableSize.getWidth(),TableSize.getHeight()+16);
            menuTable.setPrererredSize(TableSize);
           */
          Set set=(Set) Menus.keySet();
          Iterator it= set.iterator();
          while(it.hasNext())
          {
              dtm3.setRowCount(dtm3.getRowCount()+1);
              Dimension TableSize=menuTable.getPreferredSize();
              TableSize.setSize(TableSize.getWidth(),TableSize.getHeight()+16);
              menuTable.setPreferredSize(TableSize);
              
              String name=(String) it.next();
              menuTable.setValueAt(name,r3,c3);
              c3++;
              menuTable.setValueAt(Menus.get(name),r3,c3);
              r3++;
              c3=0;
            }
        }
        
        /**
         * Read Menu Collection data then set the data into menu table
         * for minus times of item in menu, remain selected after refresh table
         */
        public void displayMenu(int row)
      {
          r3=0;c3=0;
          dtm3.setRowCount(0);
          menuTable.setPreferredSize(new Dimension(298,0));
          /*
            dtm2.setRowCount(dtm2.getRowCount()+1);
            Dimension TableSize=menuTable.getPreferredSize();
            TableSize.setSize(TableSize.getWidth(),TableSize.getHeight()+16);
            menuTable.setPrererredSize(TableSize);
           */
          Set set=(Set) Menus.keySet();
          Iterator it= set.iterator();
          while(it.hasNext())
          {
              dtm3.setRowCount(dtm3.getRowCount()+1);
              Dimension TableSize=menuTable.getPreferredSize();
              TableSize.setSize(TableSize.getWidth(),TableSize.getHeight()+16);
              menuTable.setPreferredSize(TableSize);
              
              String name=(String) it.next();
              menuTable.setValueAt(name,r3,c3);
              c3++;
              menuTable.setValueAt(Menus.get(name),r3,c3);
              r3++;
              c3=0;
            }
       
        menuTable.changeSelection(row,0,false,false);
    }
    
    //<editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents()
    {
        jScrollPane2=new javax.swing.JScrollPane();
/**
 * Write a description of class Ingredient here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

 import java.io.Serializable;
 /**
  * This is the class of the Ingredient instance
  */
 public class Ingredient implements Serializable
 {
     private String name;           //The name of the Ingredient
     private double quantity;        //The quantity of the Ingredient
     private String unit;           //The unit of the Quantity of the Ingredient
     
     public Ingredient(String name,double quantity,String unit)
     {
         this.name=name;
         this.quantity=quantity;
         this.unit=unit;
        }
        
        public String getName(){return name;}
        public double getQuantity(){return quantity;}
        public String getUnit(){return unit;}
        public void setName(String name){this.name=name;}
        public void setQuantity(double quantity){this.quantity=quantity;}
        public void setUnit(String unit){this.unit=unit;}
    }
/**
 * Write a description of class MenuDisplay here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */


  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Set;
  
  /**
   * This class builds a JFrame with a JTextArea which displays the menu
   * The Menu contains the recipe name and amount order
   */
  
  public class MenuDisplay extends javax.swing.JFrame
  {
      //The Collection of ingredients
      private HashMap IngredientCollection=new HashMap();
      //A converser for formating the unit
      private UnitConverter Converter=new UnitConverter();
      
      public MenuDisplay(HashMap menu, HashMap recipe)
      {
          
          initComponents();
          writeMenu(menu, recipe);
        }
        
        /**
         * Display the Menu information in JTextArea
         */
        public void writeMenu(HashMap menu, HashMap recipe)
        {
            IngredientCollection.clear();
            Set set=(Set) menu.keySet();
            Iterator it= set.iterator();
            while(it.hasNext())//It sets recipes and amount
            {
                String recipename=(String)it.next();
                ta.append(recipename+" : "+menu.get(recipename)+" people\n");
            }
            ta.append("\n");
            ta.append("\n");
            
            calIngredients(menu,recipe);
            
            set=(Set) IngredientCollection.keySet();
            it= set.iterator();
            while(it.hasNext())//set Ingredient information
            {
                String key=(String) it.next();
                Ingredient temp=(Ingredient) IngredientCollection.get(key);
                ta.append(temp.getName()+"   "+temp.getQuantity()+""+temp.getUnit()+"\n");
            }
            ta.append("\n");
            ta.append("\n");
        }
        
        /**
         * Calculate the quantity of ingredient based on the amount of recipe
         * Formatting the different unit to same unit for the same ingredient
         * Save the calculated result into the collection of the ingredient
         */
        public void calIngredients(HashMap menu, HashMap recipe)
        {
            Set set=(Set) recipe.keySet();
            Iterator it= set.iterator();
            while(it.hasNext())//read the recipe collection
            {
                String recipename=(String) it.next();
                if(menu.containsKey(recipename))//find the recipe
                {
                    int people=new Integer(((Integer) menu.get(recipename)));//how many people
                    ArrayList ingredients=((Recipe) recipe.get(recipename)).getIngredients();//the ingredients collection of found recipe
                    for(int i=0;i<ingredients.size();i++)
                    {
                        Ingredient currentIngredient=(Ingredient)ingredients.get(i);
                        if(IngredientCollection.containsKey(currentIngredient.getName()))// Ingredient already in collection
                        {
                            Ingredient storedIngredient=(Ingredient)IngredientCollection.get(currentIngredient.getName());
                            if(storedIngredient.getUnit().equals(currentIngredient.getUnit()))// same unit
                            {
                                double storedQ=storedIngredient.getQuantity();
                                storedQ=storedQ+currentIngredient.getQuantity()*people;
                                storedIngredient.setQuantity(storedQ);
                                IngredientCollection.put(storedIngredient.getName(),storedIngredient);
                            }
                            else //different unit
                            {
                                if(currentIngredient.getUnit()==null)//Doesn't have a unit
                                {
                                    double currentQ=currentIngredient.getQuantity();
                                    double storedQ=storedIngredient.getQuantity();
                                    storedQ=storedQ+currentQ*people;
                                    storedIngredient.setQuantity(storedQ);
                                    IngredientCollection.put(storedIngredient.getName(),storedIngredient);
                                }
                                else
                                {
                                    String targetUnit=storedIngredient.getUnit();
                                    String currentUnit=currentIngredient.getUnit();
                                    double currentQ=currentIngredient.getQuantity();
                                    double storedQ=storedIngredient.getQuantity();
                                    UnitConverter UnitC = new UnitConverter();
                                    double doublenewQ=UnitC.convert(currentQ,currentUnit,targetUnit);
                                    storedQ=storedQ+doublenewQ*people;
                                    storedIngredient.setQuantity(storedQ);
                                    IngredientCollection.put(storedIngredient.getName(),storedIngredient);
                                }
                            }
                        }//Ingredient already in the collection
                        else//Ingredient not in the collection
                        {
                            String key=new String(currentIngredient.getName());
                            String name=new String(currentIngredient.getName());
                            double quantity=new Double(currentIngredient.getQuantity());
                            if(currentIngredient.getUnit()==null)//no unit
                            {
                                String unit="";
                                Ingredient newIngredient=new Ingredient(name,quantity*people,unit);
                                IngredientCollection.put(key,newIngredient);
                            }//no unit
                            else
                            {
                                String unit=new String(currentIngredient.getUnit());
                                Ingredient newingredient=new Ingredient(name, quantity*people,unit);
                                IngredientCollection.put(key,newingredient);
                            }
                        }//Ingredient is not in the collection
                    }
                }
            }
        }
        
        //<editor-fold defaultstate+"collapsed" desc=" Generated Code ">
        private void initComponents(){
            jpanel1 = new javax.swing.JPanel();
            jScrollPanel = new javax.swing.JScrollPane();
            ta = new javax.swing.JTextArea();
            
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            setTitle("Menu");
            jpanel1.setLayout(new java.awt.GridLayout(1,0));
            getContentPane().add(jpanel1, java.awt.BorderLayout.NORTH);
            
            ta.setColumns(20);
            ta.setEditable(false);
            ta.setFont(new java.awt.Font("Hight Tower Text", 0, 18));
            ta.setRows(5);
            jScrollPanel.setViewportView(ta);
            
            getContentPane().add(jScrollPanel,java.awt.BorderLayout.CENTER);
            
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((screenSize.width-500)/2, (screenSize.height-600)/2, 500, 600);
        }//</editor-fold>
        
        
        public static void main(String args[])
        {
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    new MenuDisplay(new HashMap(),new HashMap()).setVisible(true);
                }
            });
        }
        
        //Variabled declaration - don not modify
        private javax.swing.JPanel jpanel1;
        private javax.swing.JScrollPane jScrollPanel;
        private javax.swing.JTextArea ta;
        //End of variables declaration
    }
/**
 * Write a description of class Recipe here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

import java.io.Serializable;
import java.util.ArrayList;

  /**
   * This is the class of Recipe instance
   */
  
  public class Recipe implements Serializable
  {
      private ArrayList IngredientCollection;
      //A collection where you store the ingredient recipe
      private String name;
      // The name of the Recipe
      
      public Recipe(String name)
      {
          this.name=name;
          IngredientCollection=new ArrayList();
      }
      
      public void addIngredient(Ingredient ingredient)
      {
          IngredientCollection.add(ingredient);
      }
      
      public ArrayList getIngredients()
      {
          return IngredientCollection;
      }
      
      public String getName()
      {
          return name;
      }
      
      public void setName(String name)
      {
          this.name=name;
      }
      
      /**
       * Returns true if recipe contains one or more ingredient match the specified name.
       * Parameters: Name- The name of specified ingredient
       */
      public boolean contains(String Name)
      {
          boolean result=false;
          String target;
          for(int i=0;i<IngredientCollection.size();i++)
          {
              Ingredient temp=(Ingredient) IngredientCollection.get(i);
              if(temp.getName().endsWith(Name))
              {
                  result=true;
                  break;
               }
            }
            return result;
        }
    }
/**
 * Write a description of class RecipeGUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */


  import java.awt.Color;
  import java.awt.event.*;
  import java.awt.Dimension;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.PrintWriter;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.Set;
  import javax.swing.*;
  import javax.swing.JOptionPane;
  import javax.swing.JTable;
  import javax.swing.UIManager;
  import javax.swing.UnsupportedLookAndFeelException;
  import javax.swing.table.DefaultTableCellRenderer;
  import javax.swing.table.DefaultTableModel;
  import javax.swing.table.TableCellRenderer;
  import javax.swing.table.TableModel;
  
  /*
   * *This class builds a RecipeGUI for users to enter, edit, and delete recipes
   */
  
  public class RecipeGUI extends javax.swing.JFrame
  {
      //The TableCellRenderer for all cell, aims to make the content in the center
      private DefaultTableCellRenderer dtcr=new DefaultTableCellRenderer();
      //The TableModel is for the Recipe table
      private DefaultTableModel dtm = new DefaultTableModel(new Object [][]{{null,null}}, new String []{"Recipes", "Ingredients"});
      //The TableModel is for the ingredient table
      private DefaultTableModel dtm2 = new DefaultTableModel(new Object[][]{{null,null,null}}, new String[]{"ingredient", "Quantity", "Unit"});
      //This is the collection of the Recipe
      private HashMap recipeCollection=new HashMap();
      private int r,c,r2,c2;
      //Index of recipe and ingredient table
      private boolean isWeight;
      //status of toggle button
      private String currentRecipe;
      //The Recipe is of current display or for editing
      private boolean isEditing;
      //if system is in editing status
      
      public RecipeGUI()
      {
          r=0;c=0;r2=0;c2=0;//init
          isWeight=true;
          isEditing=false;
          //load();
          try //using MS WINDOWS LookAndFeel
          {
              UIManager.setLookAndFeel("com.sun.java.swing.plat.windows.WindowsLookAndFeel");
            } catch (IllegalAccessException ex)
            {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex)
            {
                ex.printStackTrace();
            } catch (InstantiationException ex)
            {
                ex.printStackTrace();
            } catch (UnsupportedLookAndFeelException ex)
            {
                ex.printStackTrace();
            }
            initComponents();
            //Display w.jpe on toggle button
            isWeight=true;
            jToggleButton1.setIcon(new javax.swing.ImageIcon("src\\pic\\w.JPG"));
            jToggleButton1.setDisabledIcon(new javax.swing.ImageIcon("src\\pic\\w.JPG"));
            jList1.removeAll();
            jList1.setModel(new javax.swing.AbstractListModel(){
                String[] strings= {"mg", "gram", "dr", "oz", "kg", "lb", "st", "No unit"};
            public int getSize() {return strings.length;}
            public Object getElementAt(int i) { return strings[i]; }});
            jList1.setSelectedIndex(7);
            
            //set TableModel
            recipeTable.setModel(dtm);
            ingredientTable.setModel(dtm2);
            dtcr.setHorizontalAlignment(dtcr.CENTER);
            dtcr.setBackground(Color.white);
            
            //set colcoum wide and TableCellRenderer
            recipeTable.getColumnModel().getColumn(0).setPreferredWidth(225);
            recipeTable.getColumnModel().getColumn(0).setCellRenderer(dtcr);
            recipeTable.getColumnModel().getColumn(1).setPreferredWidth(70);
            recipeTable.getColumnModel().getColumn(1).setCellRenderer(dtcr);
            ingredientTable.getColumnModel().getColumn(0).setCellRenderer(dtcr);
            ingredientTable.getColumnModel().getColumn(1).setCellRenderer(dtcr);
            ingredientTable.getColumnModel().getColumn(2).setCellRenderer(dtcr);
            
            readRecipes();
            setStatus(false);
        }
        
        /*
         * write all the data in the recipeCollection to file.Recipe.dat
         */
        public void save()
        {
            FileOutputStream fos=null;
            ObjectOutputStream oos=null;
            try
            {
                fos=new FileOutputStream("Recipes.dat");
                oos=new ObjectOutputStream(fos);
                oos.writeObject(recipeCollection);
                oos.flush();
                oos.close();
            }
            catch(Exception e){System.out.println(e);}
            finally
            {
                try
                {
                    if(oos!=null)oos.close();
                }
                catch(Exception e){}
            }
        }
        
        /*
         * Read the file:Recipes.dat to input the saved data to recipeCollection
         */
        public void load()
        {
            FileInputStream fis=null;
            ObjectInputStream ois=null;
            try
            {
                fis=new FileInputStream("Recipes.dat");
                ois=new ObjectInputStream(fis);
                recipeCollection=(HashMap) ois.readObject();
            }
            catch(Exception e){System.out.println(e);}
            finally
            {
                try
                {
                    if(ois!=null) ois.close();
                }
                catch(Exception e){}
            }
        }
        
        /*
         * write all the data in recipeCollection to file:Recipe.txt
         * test method, can using notepad to check the file
         */
        public void save2()
      {
          FileWriter fileWriter=null;
          PrintWriter printWriter=null;
          try
          {
            fileWriter = new FileWriter("Recipes.txt");
            printWriter=new PrintWriter(fileWriter);
            Set set=(Set) recipeCollection.keySet();
            Iterator it= set.iterator();
            while(it.hasNext())
            {
                String key=(String) it.next();
                Recipe temp=(Recipe) recipeCollection.get(key);
                printWriter.println(temp.getName());
                ArrayList tempi=temp.getIngredients();
                for(int i=0;i<tempi.size();i++)
                {
                    printWriter.println(((Ingredient)tempi.get(i)).getName());
                    printWriter.println(((Ingredient)tempi.get(i)).getQuantity());
                    printWriter.println(((Ingredient)tempi.get(i)).getUnit());
                }
            }
            printWriter.close();
        }
        catch(IOException e){}
        finally {if(fileWriter !=null) printWriter.close();}
    }
    
    /*
     * put data into the recipe table from recipeCollection using addRecipe()
     */
    public void readRecipes()
    {
        Set set=(Set) recipeCollection.keySet();
        Iterator it= set.iterator();
        while(it.hasNext())
        {
            String key=(String) it.next();
            addRecipe(key);
        }
    }
    
    /*
     * put data into the recipe table from recipeCollection
     */
    public void addRecipe(String recipename)
    {
        if(recipeCollection.containsKey(recipename))
        {
            Recipe temp=(Recipe) recipeCollection.get(recipename);
            recipeTable.setValueAt(temp.getName(),r,c);
            c++;
            recipeTable.setValueAt(temp.getIngredients().size(),r, c);
            r++;
            c=0;
            dtm.setRowCount(dtm.getRowCount()+1);
            Dimension TableSize=recipeTable.getPreferredSize();
            TableSize.setSize(TableSize.getWidth(),TableSize.getHeight()+16);
            recipeTable.setPreferredSize(TableSize);
        }
    }
    
    /**
     * put the data into the Ingredient Table
     */
    public void displayIngredient(Recipe recipe)
    {
        r2=0;c2=0;
        dtm2.setRowCount(0);
        dtm2.setRowCount(1);
        ingredientTable.setPreferredSize(new Dimension(225,16));
        
        for(int i=0;i<recipe.getIngredients().size();i++)
        {
            Ingredient temp=(Ingredient)recipe.getIngredients().get(i);
            ingredientTable.setValueAt(temp.getName(),r2,c2);
            c2++;
            ingredientTable.setValueAt(temp.getQuantity(),r2,c2);
            c2++;
            ingredientTable.setValueAt(temp.getUnit(),r2,c2);
            r2++;
            c2=0;
            
            dtm2.setRowCount(dtm2.getRowCount()+1);
            Dimension TableSize=ingredientTable.getPreferredSize();
            TableSize.setSize(TableSize.getWidth(),TableSize.getHeight()+16);
            ingredientTable.setPreferredSize(TableSize);
        }
    }
    /**
     * Display the Ingredient detail in Ingredient table of selected Recipe
     */
    public void displayRecipe()
    {
        try
        {
            String name=(String)recipeTable.getValueAt(recipeTable.getSelectedRow(),0);
            currentRecipe=name;
            recipename.setText(name);
            displayIngredient((Recipe)recipeCollection.get(currentRecipe));
        }
        catch(Exception e){}
    }
    /**
     *Delete the selected Recipe
     */
    public void deleteRecipe()
    {
        try
        {
            String key=(String)recipeTable.getValueAt(recipeTable.getSelectedRow(),0);
            dtm.removeRow(recipeTable.getSelectedRow());
            Dimension TableSize=recipeTable.getPreferredSize();
            TableSize.setSize(TableSize.getWidth(),TableSize.getHeight()-16);
            recipeTable.setPreferredSize(TableSize);
            r--;
            recipeCollection.remove(key);
        }
        catch(Exception e){}
    }
    
    /**
     * Delet

f u look at what i have attached and try to run it..It gave me a problem as i am meant to have a menu which is meant to show something inside it but the menu is coming up but with nothing inside it.

So try simplifying your code so it displays just one thing on your menu. If needs be start a new project.

When you simplify it, you eventually get to a stage where you know that only ONE thing may be causing your error. This is an essential part of programming.

Good luck.

hi sir . I want help . how to write code in frames to accept weight in kilograms in a text field and convert it into grams and milligrams on the click of two separate buttons . display the result in a second text field .

Hi Sravani Kunam, welcome to daniweb.
a few do's and don'ts, though:

DON't hijack dead threads
DO create your own thread for your questions
DON't copy paste your assignment here, expecting us to deliver custom made code
DO show some effort. what have you got so far and what are you stuck with?
DO be specific when asking questions. "Don't work" is way too vague for us to figure out, for instance.

This article has been dead for over six months. Start a new discussion instead.