By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,712 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,712 IT Pros & Developers. It's quick & easy.

GUI Delete Button Problem

P: 7
My delete button is not working in my GUI and my due date is today before midnight. Can anyone show me how to correct this error? My assignment statement is below as well as 5 classes. InventoryGUI is the main class to begin execution.
Thanks!

Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the number of units in stock, and the price of each unit.An item added to the inventory should have an item number one more than the previous last item.
Add a Save button to the GUI that saves the inventory to a C:\data\inventory.dat file.
Use exception handling to create the directory and file if necessary.
Add a search button to the GUI that allows the user to search for an item in the inventory by the product name. If the product is not found, the GUI should display an appropriate message. If the product is found, the GUI should display that products information in the
GUI.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import java.awt.FlowLayout;
  4. import java.awt.HeadlessException;
  5. import java.awt.Image;
  6.  
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9.  
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.ObjectOutputStream;
  15. import java.io.Serializable;
  16.  
  17. import javax.swing.ImageIcon;
  18. import javax.swing.JButton;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.JPanel;
  23. import javax.swing.JScrollPane;
  24. import javax.swing.JTextArea;
  25.  
  26.  
  27. public class InventoryGUI extends JFrame implements ActionListener,Serializable 
  28. {
  29.    private JTextArea textArea;
  30.  
  31.    private JButton first,
  32.                    next,
  33.                    previous,
  34.                    last,
  35.                    add,
  36.                    modify,
  37.                    delete,
  38.                    save,
  39.                    search,
  40.                    exit;
  41.    JLabel imageLabel;
  42.  
  43.    private static Inventory inv = new Inventory();
  44.  
  45.    /**
  46.      * @param arg0
  47.      * @throws HeadlessException
  48.      */
  49.    public InventoryGUI( String arg0 ) throws HeadlessException 
  50.    {
  51.       super( "Inventory GUI" );
  52.  
  53.       textArea = new JTextArea( 250,30 );
  54.  
  55.       JScrollPane scrollPane = new JScrollPane( textArea ); 
  56.  
  57.       textArea.setEditable( false );
  58.  
  59.       scrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
  60.  
  61.       scrollPane.setPreferredSize( new Dimension( 250, 250 ) );
  62.  
  63.       JPanel cp = new JPanel();
  64.  
  65.       cp.setSize( 250, 40 );
  66.  
  67.       cp.setLayout( new BorderLayout() );
  68.  
  69.       cp.add( scrollPane,BorderLayout.CENTER );
  70.  
  71.       JPanel buttonPaenl = new JPanel();
  72.  
  73.       JPanel buttonPaenl1 = new JPanel();
  74.  
  75.       first = new JButton( "First" );
  76.       first.addActionListener( this );
  77.  
  78.       search = new JButton( "Search" );
  79.       search.addActionListener( this );
  80.  
  81.       next = new JButton( "Next" );
  82.       next.addActionListener( this );
  83.  
  84.       previous = new JButton( "Previous" );
  85.       previous.addActionListener( this );
  86.  
  87.       last = new JButton( "Last" );
  88.       last.addActionListener( this );
  89.  
  90.       add = new JButton( "Add" );
  91.       add.addActionListener( this );
  92.  
  93.       modify = new JButton( "Modify" );
  94.       modify.addActionListener( this );
  95.  
  96.       delete = new JButton( "Delete" );
  97.       delete.addActionListener( this );
  98.  
  99.       save = new JButton( "Save" );
  100.       save.addActionListener( this );
  101.  
  102.       exit = new JButton( "Exit" );
  103.       exit.addActionListener( this );
  104.  
  105.       buttonPaenl.setLayout( new FlowLayout() );
  106.       buttonPaenl1.setLayout( new FlowLayout() );
  107.       buttonPaenl.add( first );
  108.       buttonPaenl.add( previous );
  109.       buttonPaenl.add( next );
  110.       buttonPaenl.add( last );
  111.       buttonPaenl1.add( add );
  112.       buttonPaenl1.add( modify );
  113.       buttonPaenl1.add( delete );
  114.       buttonPaenl1.add( save );
  115.       buttonPaenl1.add( search );
  116.       buttonPaenl1.add( exit );
  117.  
  118.       cp.add( buttonPaenl,BorderLayout.SOUTH );
  119.       cp.add( buttonPaenl1,BorderLayout.NORTH );
  120.  
  121.       ImageIcon icon = new ImageIcon( "C:\\data\\logo.gif","a pretty but meaningless splat" );
  122.       imageLabel = new JLabel( "Xavier", icon, JLabel.CENTER );
  123.       cp.add( imageLabel,BorderLayout.EAST );
  124.       this.setContentPane( cp );
  125.  
  126.       this.setTitle( " Week Nine Final Project: Inventory Program Part Six - Transformers Autobot Inventory ( More Than Meets the Eye o_0?! ) " );
  127.  
  128.       this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  129.       this.textArea.setText(inv.getFirst());
  130.       this.setSize(400, 400);
  131.       this.pack();
  132.       this.setVisible( true );
  133.    }
  134.  
  135.    @Override
  136.    public void actionPerformed( ActionEvent e ) 
  137.    {
  138.       // TODO Auto-generated method stub
  139.       if( e.getActionCommand().equals( "First" ) )
  140.       {
  141.          textArea.setText( inv.getFirst() );
  142.       }
  143.  
  144.       if( e.getActionCommand().equals( "Next" ) )
  145.       {
  146.          textArea.setText(inv.getNext());
  147.       }
  148.  
  149.       if( e.getActionCommand().equals( "Previous" ) )
  150.       {
  151.          textArea.setText( inv.getPrevious() );
  152.       }
  153.  
  154.       if( e.getActionCommand().equals( "Last" ) )
  155.       {
  156.          textArea.setText(inv.getLast());
  157.       }
  158.  
  159.       if( e.getActionCommand().equals( "Save" ) )
  160.       {
  161.          save();
  162.       }
  163.  
  164.       if( e.getActionCommand().equals( "Exit" ) )
  165.       {
  166.          System.exit(0);
  167.       }
  168.  
  169.       if( e.getActionCommand().equals( "Add" ) ) 
  170.       {
  171.          InventoryDialogue id = new InventoryDialogue( true," Add a new Inventory " );
  172.       }
  173.  
  174.       if( e.getActionCommand().equals( "Modify" ) )
  175.       {
  176.          InventoryDialogue id = new InventoryDialogue( false," Modify Inventory " );
  177.       }
  178.  
  179.       if( e.getActionCommand().equals( "Search" ) )
  180.       {
  181.          String s = JOptionPane.showInputDialog( null, " Enter Product Name ",null, 1 );
  182.  
  183.          s = inv.Search(s);
  184.  
  185.          if( s == null )
  186.          {
  187.             JOptionPane.showMessageDialog( this, " No Results Found " ) ;
  188.          } 
  189.  
  190.          else textArea.setText( s );
  191.  
  192.          if( e.getActionCommand().equals( "Delete" ) )
  193.          {
  194.             inv.getproducts().remove( inv.index );
  195.             }
  196.       }
  197.    }
  198.  
  199.    public void save()
  200.    {
  201.       File f = new File( "C:\\data\\inventory.dat" );
  202.  
  203.       try 
  204.       {
  205.          FileOutputStream out = new FileOutputStream( f );
  206.  
  207.          try 
  208.          {
  209.             ObjectOutputStream objectOut = new ObjectOutputStream( out );
  210.                objectOut.writeObject( inv );
  211.          } 
  212.  
  213.          catch (IOException e) 
  214.          {
  215.             // TODO Auto-generated catch block
  216.             JOptionPane.showMessageDialog( null, e.getMessage() );
  217.                e.printStackTrace();
  218.          }
  219.       } 
  220.  
  221.       catch ( FileNotFoundException e ) 
  222.       {
  223.          // TODO Auto-generated catch block
  224.          JOptionPane.showMessageDialog( null, e.getMessage() );
  225.             e.printStackTrace();
  226.       }
  227.  
  228.    }  
  229.  
  230.    public static Inventory getInv() 
  231.    {
  232.       return inv;
  233.    }
  234.  
  235.    public static void setInv( Inventory inv ) 
  236.    {
  237.       InventoryGUI.inv = inv;
  238.    }
  239.  
  240.    /**
  241.     * @param args
  242.     */
  243.    public static void main( String[] args ) 
  244.    {
  245.       InventoryGUI inventory = new InventoryGUI( "Inventory" );
  246.    }
  247. }
  248.  
Expand|Select|Wrap|Line Numbers
  1. import java.awt.BorderLayout;
  2. import java.awt.FlowLayout;
  3. import java.awt.LayoutManager;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.BoxLayout;
  8. import javax.swing.JButton;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JPanel;
  13. import javax.swing.JTextField;
  14.  
  15.  
  16. public class InventoryDialogue extends JFrame implements ActionListener
  17. {
  18.    private boolean status = false; //false is for modify
  19.  
  20.    JTextField nameTextField,
  21.               unitTextField,
  22.               priceTextField;
  23.  
  24.    JLabel nameLabel,
  25.           unitLabel,
  26.           priceLabel;
  27.  
  28.    JPanel TextFields,
  29.           buttons;
  30.  
  31.    JButton modify, 
  32.            add;
  33.  
  34.    public InventoryDialogue( Boolean stat, String title )
  35.    {
  36.       super( title );
  37.       status = stat;
  38.       JPanel but = new JPanel();
  39.       modify = new JButton( "Modify" );
  40.       modify.addActionListener( this );
  41.       add = new JButton( "Add" );
  42.       add.addActionListener( this );
  43.       but.add( add );
  44.       but.add( modify );
  45.  
  46.       if( status == false )
  47.       {
  48.          add.setVisible( false );
  49.       }
  50.  
  51.       else
  52.       {
  53.          modify.setVisible( false );
  54.       }
  55.  
  56.       nameTextField = new JTextField( "", 20 );
  57.       unitTextField = new JTextField( "", 5 );
  58.       priceTextField = new JTextField( "" , 5 );
  59.       nameLabel = new JLabel( "Name" );
  60.       unitLabel = new JLabel( "Units" );
  61.       priceLabel = new JLabel( "Price per Unit" );
  62.       JPanel p = new JPanel();
  63.  
  64.       p.setLayout( new FlowLayout() );
  65.       p.add( nameLabel );
  66.       p.add( nameTextField );
  67.       p.add( unitLabel );
  68.       p.add( unitTextField );
  69.       p.add( priceLabel );
  70.       p.add( priceTextField );
  71.       JPanel cp = new JPanel();
  72.       cp.setLayout( new BorderLayout() );
  73.       cp.add( p, BorderLayout.CENTER );
  74.       cp.add( but, BorderLayout.EAST );
  75.       this.setContentPane( cp );
  76.       this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
  77.       this.pack();
  78.       this.setVisible( true );
  79.    }
  80.  
  81.    @Override
  82.    public void actionPerformed( ActionEvent arg0 ) 
  83.    {
  84.       if( arg0.getActionCommand().equals( "Add" ) )
  85.       {
  86.          String name;
  87.          int units;
  88.          Double price;
  89.          name = nameTextField.getText();
  90.  
  91.          try 
  92.          {
  93.             units = Integer.parseInt( unitTextField.getText() );
  94.          } 
  95.  
  96.          catch ( NumberFormatException e ) 
  97.          {
  98.             JOptionPane.showMessageDialog( null, e.getMessage() );
  99.  
  100.             e.printStackTrace();
  101.  
  102.             return;
  103.          }
  104.  
  105.          try 
  106.          {
  107.             price = Double.parseDouble( priceTextField.getText() );
  108.          } 
  109.  
  110.          catch ( NumberFormatException e ) 
  111.          {
  112.             // TODO Auto-generated catch block
  113.             JOptionPane.showMessageDialog( null, e.getMessage() );
  114.  
  115.             e.printStackTrace();
  116.  
  117.             return;
  118.          }
  119.  
  120.          InventoryGUI.getInv().getproducts().add( new ProductModified( name, InventoryGUI.getInv().products.size(), 
  121.                                                                        units,price ) );
  122.  
  123.          this.dispose();
  124.       }
  125.  
  126.       if( arg0.getActionCommand().equals( "Modify" ) )
  127.       {
  128.          String name;
  129.          int units;
  130.          Double price;
  131.          name = nameTextField.getText();
  132.  
  133.          try 
  134.          {
  135.             units = Integer.parseInt( unitTextField.getText() );
  136.          } 
  137.  
  138.          catch ( NumberFormatException e ) 
  139.          {
  140.             JOptionPane.showMessageDialog( null,"Please Enter Valid Integers" );
  141.  
  142.             e.printStackTrace();
  143.  
  144.             return;
  145.          }
  146.  
  147.          try 
  148.          {
  149.             price = Double.parseDouble( priceTextField.getText() );
  150.          } 
  151.  
  152.          catch ( NumberFormatException e ) 
  153.          {
  154.             // TODO Auto-generated catch block
  155.             JOptionPane.showMessageDialog( null,"Please Enter Valid Price" );
  156.  
  157.             //e.printStackTrace();
  158.             return;
  159.          }
  160.  
  161.          InventoryGUI.getInv().getproducts().set( InventoryGUI.getInv().index, 
  162.             new ProductModified( name, InventoryGUI.getInv().index, units, price ) );
  163.  
  164.          this.dispose();
  165.       }
  166.  
  167.    }
  168.  
  169.    public static void main( String args[] )
  170.    {
  171.       InventoryDialogue idf = new InventoryDialogue( true,"cc" );
  172.    }
  173. }
  174.  
Expand|Select|Wrap|Line Numbers
  1. import java.text.Collator;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.Locale;
  5.  
  6.  
  7. public class Inventory 
  8. {
  9.    private String inventoryName; // name of inventory
  10.  
  11.    public String restockRate; // restock rate percentage
  12.  
  13.    public double totalRestock;
  14.  
  15.    public static int index;
  16.  
  17.    public ArrayList < ProductModified > products = new ArrayList < ProductModified > ();
  18.  
  19.    public Inventory()
  20.    {
  21.       products.add( new ProductModified( "No Sale Bot", 0, 1, 0.00 ) );
  22.       products.add( new ProductModified( "Optimus Prime", 7, 52, 160.00 ) );
  23.       products.add( new ProductModified( "BumbleeBee", 16, 61, 97.00 ) );
  24.       products.add( new ProductModified( "Ironhide", 25, 106, 88.00 ) );
  25.       products.add( new ProductModified( "Ratchet", 34, 125, 79.00 ) );
  26.       products.add( new ProductModified( "Jazz", 0, 124, 70.00 ) );
  27.  
  28.       index = 0;
  29.    }
  30.  
  31.    public void sort()
  32.    {
  33.       Locale loc = Locale.ENGLISH;
  34.  
  35.       ProductModified Temp;
  36.  
  37.       Collator col = Collator.getInstance( loc );
  38.  
  39.       for ( int i = 0; i < products.size(); i++ ) 
  40.       {
  41.          for ( int j = i + 1; j < products.size(); j++ )
  42.          {
  43.             if( col.compare( products.get( i ).getAutobotName(), products.get( j ).getAutobotName() ) > 0 )
  44.             {
  45.                Temp = products.get( i );
  46.  
  47.                products.set( i, products.get( j ) );
  48.                products.set( j, Temp );
  49.             }
  50.          }
  51.       }
  52.    }
  53.  
  54.    public String getInventoryName() 
  55.    {
  56.       return inventoryName;
  57.    }
  58.  
  59.     public void setInventoryName( String inventoryName ) 
  60.     {
  61.        this.inventoryName = inventoryName;
  62.     }
  63.  
  64.     public String getRestockRate() 
  65.     {
  66.        return restockRate;
  67.     }
  68.  
  69.     public void setRestockRate( String restockRate ) 
  70.     {
  71.        this.restockRate = restockRate;
  72.     }
  73.  
  74.     public double getTotalRestock() 
  75.     {
  76.        return totalRestock;
  77.     }
  78.  
  79.     public void setTotalRestock( double totalRestock ) 
  80.     {
  81.        this.totalRestock = totalRestock;
  82.     }
  83.  
  84.     public ArrayList < ProductModified > getproducts() 
  85.     {
  86.        return products;
  87.     }
  88.  
  89.     public void seproducts( ArrayList < ProductModified > products) 
  90.     {
  91.        this.products = products;
  92.     }
  93.  
  94.     public String toString()
  95.     {
  96.        String transformersSub = new String(" ");
  97.  
  98.        transformersSub = "Welcome to the\n" + getInventoryName() + "!\n\n";
  99.  
  100.        transformersSub = transformersSub + "Below is the available inventory:\n\n";
  101.  
  102.        transformersSub = transformersSub + "The restock interest rate is at " + getRestockRate() + ".\n\n";
  103.  
  104.        transformersSub = transformersSub + "Index\tProductModified #\t\t\tName\t\tUnits\t\tPrice\t\t";
  105.  
  106.        transformersSub = transformersSub + "Unit Restock Value\t";    
  107.  
  108.        transformersSub = transformersSub + "Stock Value\t\t";
  109.  
  110.        transformersSub = transformersSub + "Total ProductModified Restock\n";
  111.  
  112.        Iterator < ProductModified > i = this.getproducts().iterator();
  113.  
  114.        while(i.hasNext())
  115.        {
  116.           transformersSub = transformersSub + i.next();
  117.        }
  118.  
  119.       return transformersSub;
  120.    }
  121.  
  122.    public Double getTotal()
  123.    {
  124.       Double Total = 0.0;
  125.  
  126.       ProductModified p;
  127.  
  128.       Iterator < ProductModified > i = products.iterator();
  129.  
  130.       while( i.hasNext() )
  131.       {
  132.             p = i.next();
  133.  
  134.             Total = Total + p.getPrice() * p.getUnit();
  135.       }
  136.  
  137.       return Total;
  138.    }
  139.  
  140.    public String processOutPut( ProductModified p )
  141.    {
  142.       String out = "Welcome to the Transformers Autobot Toy Inventory\n\nTotal Inventory = " 
  143.       + this.getTotal() + "\nRestocking Fee = 5%" 
  144.       + "\n\nItem #\tName\tUnit\tPrice\tUnit Restock Value" 
  145.       + p;
  146.  
  147.       return out;
  148.    }
  149.  
  150.    public String getFirst()
  151.    {
  152.       index = 0;
  153.  
  154.       return processOutPut( products.get(0) );
  155.  
  156.    }
  157.  
  158.    public String getLast()
  159.    {
  160.       index = products.size() - 1;
  161.  
  162.       return processOutPut( products.get( products.size() - 1 ) );
  163.    }
  164.  
  165.    public String getNext()
  166.    {
  167.       if( index == products.size() - 1 ) 
  168.       {
  169.          getFirst();
  170.  
  171.          return getFirst();
  172.       } 
  173.  
  174.       else
  175.       {
  176.          index++;
  177.  
  178.          return processOutPut( products.get( index ) );
  179.       }
  180.    }
  181.  
  182.    public String getPrevious()
  183.    {
  184.       if( index == 0 )
  185.       {
  186.          return getLast();
  187.       } 
  188.  
  189.       else
  190.  
  191.       return processOutPut(products.get(--index));
  192.    }
  193.  
  194.    public String Search( String Search )
  195.    {
  196.       Iterator < ProductModified > i = products.iterator();
  197.  
  198.       String te = null;
  199.  
  200.       ProductModified p;
  201.  
  202.       while( i.hasNext() )
  203.       {
  204.          p = i.next();
  205.  
  206.          if( p.getAutobotName().equals( Search ) )
  207.          {
  208.             te = processOutPut( p );
  209.          }
  210.       }
  211.  
  212.       return te;
  213.    }
  214. }
  215.  
Expand|Select|Wrap|Line Numbers
  1. import static java.lang.System.out;
  2.  
  3.  
  4. //class declaration for TransformersProduct
  5. public class Product
  6. {
  7.    public String autobotName; // array of autobot toy names
  8.    public int productNumber; // product # array ID for product
  9.    public int unit; // array of autobot toy units
  10.    public double price; // array of autobot prices
  11.    public double inventoryValue;
  12.  
  13.    /**
  14.     * @param autobotName
  15.     * @param productNumber
  16.     * @param unit           
  17.     * @param price
  18.     */
  19.  
  20.    public Product( String autobotName, int productNumber, int unit,
  21.                    double price ) 
  22.    {
  23.       super();
  24.       this.autobotName = autobotName;
  25.       this.productNumber = productNumber;
  26.       this.unit = unit;
  27.       this.price = price;
  28.    }
  29.  
  30.    public String getAutobotName() 
  31.    {
  32.       return autobotName;
  33.    }
  34.  
  35.    public void setAutobotName( String autobotName ) 
  36.    {
  37.       this.autobotName = autobotName;
  38.    }
  39.  
  40.    public int getProductNumber() 
  41.    {
  42.       return productNumber;
  43.    }
  44.  
  45.    public void setProductNumber( int productNumber ) 
  46.    {
  47.       this.productNumber = productNumber;
  48.    }
  49.  
  50.    public int getUnit() 
  51.    {
  52.       return unit;
  53.    }
  54.  
  55.    public void setUnit( int unit ) 
  56.    {
  57.       this.unit = unit;
  58.    }
  59.  
  60.    public double getPrice() 
  61.    {
  62.       return price;
  63.    }
  64.  
  65.    public void setPrice( double price ) 
  66.    {
  67.       this.price = price;
  68.    }
  69.  
  70.    public double getInventoryValue() 
  71.    {
  72.       return inventoryValue;
  73.    }
  74.  
  75.    public Double inventoryValue()
  76.    {
  77.       return this.getPrice() * this.getUnit();
  78.    }
  79.  
  80.    public String toString()
  81.    {
  82.       return "\n"+this.productNumber+"\t"+this.autobotName+"\t"+this.unit +"\t"+this.price+ "\t"; 
  83.    }
  84. }
Expand|Select|Wrap|Line Numbers
  1. public class ProductModified extends Product 
  2. {
  3.    public ProductModified( String autobotName, int productNumber, int unit,
  4.                            double price ) 
  5.    {
  6.       super( autobotName, productNumber, unit, price );
  7.       this.inventoryValue = this.inventoryValue();
  8.       // TODO Auto-generated constructor stub
  9.    }
  10.  
  11.    public Double inventoryValue()
  12.    {
  13.       return this.getUnit() * this.getPrice() + this.getUnit() * this.getPrice() * 0.05;
  14.    }
  15.  
  16.    public String toString()
  17.    {
  18.       return "\n" + this.productNumber + "\t" + this.autobotName 
  19.                   + "\t" + this.unit + "\t" + this.price + "\t" 
  20.                   + this.getInventoryValue(); 
  21.    }
  22. }
  23.  
Nov 18 '07 #1
Share this Question
Share on Google+
7 Replies


P: 7
I managed to get the "Delete" button to work. I just have one last problem dealing with the "Save" button execution.

When I click on save, it does create a inventory.dat file in my Data folder located my C: drive. However, when I review my General Output screen, I can this message:

"--------------------Configuration: <Default>--------------------
java.io.NotSerializableException: Inventory
at java.io.ObjectOutputStream.writeObject0(ObjectOutp utStream.java:1156)
at java.io.ObjectOutputStream.writeObject(ObjectOutpu tStream.java:326)
at InventoryGUI.save(InventoryGUI.java:211)
at InventoryGUI.actionPerformed(InventoryGUI.java:161 )
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6038)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3260)
at java.awt.Component.processEvent(Component.java:580 3)
at java.awt.Container.processEvent(Container.java:205 8)
at java.awt.Component.dispatchEventImpl(Component.jav a:4410)
at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429 )
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)"

Is this normal? Is the program saving correctly? I have no idea if it is because I don't know what program to use to open the inventory.dat file to check. When I click on it, the file opens in notepad and I get...

" {sr java.io.NotSerializableException(Vx 5 xr java.io.ObjectStreamExceptiondk9 xr java.io.IOExceptionlsde% xr java.lang.Exception>; xr java.lang.Throwable5'9w L causet Ljava/lang/Throwable;L
detailMessaget Ljava/lang/String;[
stackTracet [Ljava/lang/StackTraceElement;xpq ~ t Inventoryur [Ljava.lang.StackTraceElement;F*<<"9 xp sr java.lang.StackTraceElementa Ś&6݅ I
lineNumberL declaringClassq ~ L fileNameq ~ L
methodNameq ~ xp t java.io.ObjectOutputStreamt ObjectOutputStream.javat writeObject0sq ~ Fq ~ q ~ t writeObjectsq ~ t InventoryGUIt InventoryGUI.javat savesq ~ q ~ q ~ t actionPerformedsq ~ t javax.swing.AbstractButtont AbstractButton.javat fireActionPerformedsq ~ t "javax.swing.AbstractButton$Handlerq ~ q ~ sq ~ t javax.swing.DefaultButtonModelt DefaultButtonModel.javaq ~ sq ~ q ~ q ~ !t
setPressedsq ~ t *javax.swing.plaf.basic.BasicButtonListenert BasicButtonListener.javat
mouseReleasedsq ~ t java.awt.Componentt Component.javat processMouseEventsq ~ t javax.swing.JComponentt JComponent.javaq ~ +sq ~ q ~ )q ~ *t processEventsq ~ 
t java.awt.Containert Container.javaq ~ 0sq ~ :q ~ )q ~ *t dispatchEventImplsq ~ Dq ~ 2q ~ 3q ~ 5sq ~ q ~ )q ~ *t
dispatchEventsq ~ t java.awt.LightweightDispatcherq ~ 3t retargetMouseEventsq ~ q ~ :q ~ 3q ~ +sq ~ Lq ~ :q ~ 3q ~ 8sq ~ 6q ~ 2q ~ 3q ~ 5sq ~ }t java.awt.Windowt Window.javaq ~ 5sq ~ q ~ )q ~ *q ~ 8sq ~ Wt java.awt.EventQueuet EventQueue.javaq ~ 8sq ~ t java.awt.EventDispatchThreadt EventDispatchThread.javat pumpOneEventForFilterssq ~ q ~ Gq ~ Ht pumpEventsForFiltersq ~ *q ~ Gq ~ Ht pumpEventsForHierarchysq ~ q ~ Gq ~ Ht
pumpEventssq ~ q ~ Gq ~ Hq ~ Osq ~ yq ~ Gq ~ Ht runx"

This does not look right; it looks very similar to what I get in the General Output screen. I was expecting to see a saved inventory list. Other than that, I believe I met the requirements of this assignment. If something is not working with the save feature, what is needed to correct the problem? Thanks.
Nov 18 '07 #2

Expert 10K+
P: 11,448
You are trying to (de)serialize your Inventory class. By convention Java can only
(de)serialize objects if they implement the (empty) Serializable interface; as in:

Expand|Select|Wrap|Line Numbers
  1. public class Inventory implements Serializable { ... }
  2.  
This is basically all you have to do for a class if you want to make it serializable.
Note that if your class contains other class objects, they have to be serializable
as well.

kind regards,

Jos
Nov 18 '07 #3

P: 7
Thanks a bunch! I've implemented the serialization to the Inventory and ProductModified class (I also implemented it in the Product class; however, I received the same output whether I implemented it or not. Therefore, took it out of the Product class).

Now, the inventory.dat file produced something different, which is:

" sr InventoryQ%wt D totalRestockL
inventoryNamet Ljava/lang/String;L productst Ljava/util/ArrayList;L restockRateq ~ xp psr java.util.ArrayListxa I sizexp w
sr ProductModified_ xr ProductX>i D inventoryValueD priceI
productNumberI unitL autobotNameq ~ xp t No Sale Botsq ~ @ @d  4t
Optimus Primesq ~ @Dٙ@X@  =t
BumbleeBeesq ~ @!33333@V  jt Ironhidesq ~ @@` @S " }t Ratchetsq ~ @ @Q |t Jazzxp"

It still looks kind of funny...is it suppose to look that way? I think it is suppose to look that way because in the past, I experimented on opening files with the wrong programs and I get something to that effect. Maybe I need to open it with another program other than notepad so it can be viewed better (I don't know of a program to open .dat files), or maybe the only way to view it correctly is if I had a Load feature in my Inventory Program; however, that's not required for this assignment (I might implement that feature later on after I submit this just because....).

Please let me know (or anyone else) if everything is ok (and what program to use to view the .dat file correctly....if available). I thank you a bunch again!
Nov 18 '07 #4

Expert 10K+
P: 11,448
Thanks a bunch! I've implemented the serialization to the Inventory and ProductModified class (I also implemented it in the Product class; however, I received the same output whether I implemented it or not. Therefore, took it out of the Product class).
For an Inventory object to be serializable all of its members need to be serializable.
An Inventory object contains other object types so they too need to implement
the Serializable interface. Also re-read what I wrote in my previous reply.

Now, the inventory.dat file produced something different, which is:

[ snip ]

It still looks kind of funny...is it suppose to look that way? I think it is suppose to look that way because in the past, I experimented on opening files with the wrong programs and I get something to that effect. Maybe I need to open it with another program other than notepad so it can be viewed better (I don't know of a program to open .dat files), or maybe the only way to view it correctly is if I had a Load feature in my Inventory Program; however, that's not required for this assignment (I might implement that feature later on after I submit this just because....).

Please let me know (or anyone else) if everything is ok (and what program to use to view the .dat file correctly....if available). I thank you a bunch again!
A serialized object isn't just text, i.e. it is a binary representation of the object.
I've seen that gibberish before and it looks fine, it just isn't plain text. You can
open that file for reading again, wrap an ObjectInputStream around it and read
the entire Inventory object back in in one read call. It is sort of symmetrical to
how you wrote the object.

kind regards,

Jos
Nov 18 '07 #5

P: 7
Thank you again for your input. One last thing, if you could point me in the right direction with a nice trail :). My assignment requires that I use exception handling to create a directory and file if necessary.

I was able to create a C:data\inventory.dat file, provided that there's an existing Data folder (directory) in my C: drive; however, I have a feeling that the assignment wants me to create an actual Data folder if one is not available. When I remove the Data folder from my C: drive, I get "C:\data\inventory.dat (The system cannot find the path specified)".

How am I to create able a folder that currently does not exist? Thank you again.
Nov 18 '07 #6

Expert 10K+
P: 11,448
Thank you again for your input. One last thing, if you could point me in the right direction with a nice trail :). My assignment requires that I use exception handling to create a directory and file if necessary.

I was able to create a C:data\inventory.dat file, provided that there's an existing Data folder (directory) in my C: drive; however, I have a feeling that the assignment wants me to create an actual Data folder if one is not available. When I remove the Data folder from my C: drive, I get "C:\data\inventory.dat (The system cannot find the path specified)".

How am I able a folder that currently does not exist? Thank you again.
Have a look at the File class and pay close attention to the mkdirs
method. Alternatively you can use the FileSystemView class.

kind regards,

Jos
Nov 18 '07 #7

P: 7
Thanks a bunch! I got it to work. I do have one last problem that I will post in new topic.
Nov 19 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.