Results 1 to 9 of 9
  1. #1
    ljk8950 is offline Member
    Join Date
    Jul 2008
    Posts
    43
    Rep Power
    0

    Default Program Compiles but Buttons do not display

    Can someone please take a quick look at this code. I finally was able to get it to compile. However, the buttons I created to not appear when the program is executed. What am I overlooking?

    Java Code:
    // CheckPoint:  InventoryProgramPart5.java
    // Week 8
    // This program calculates inventory value
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class InventoryProgramPart5
    {
         public static void main( String args[] )
         {
              double restockingFee = 0.05;
              manufacturer[] inventory = new manufacturer[5]; // array of 5
    
              inventory[0] = new manufacturer
                   ( "notepads", restockingFee, "4000", "Ampad", 60, 2.75 );
              inventory[1] = new manufacturer
                   ( "pencils", restockingFee, "5000", "Bic", 75, 1.25 );
              inventory[2] = new manufacturer
                   ( "folders", restockingFee, "2000", "3M", 30, 4.75 );
              inventory[3] = new manufacturer
                   ( "envelopes", restockingFee, "1000", "Universal", 15, 5.25 );
              inventory[4] = new manufacturer
                   ( "markers", restockingFee, "3000", "Sanford", 45, 3.50 );
    
              manufacturer temp[] = new manufacturer[1];
          
                   for( int j = 0; j < inventory.length - 1; j++ )
                   {
                        for( int k = 0; k < inventory.length - 1; k++ )
                        {
                             if( inventory[k].getItemName().compareToIgnoreCase
                                  ( inventory[k+1].getItemName() ) > 0 )
                             {
                                  temp[0] = inventory[k];
                                  inventory[k] = inventory[k+1];
                                  inventory[k+1] = temp[0];
                             } // end if
                        } // end for    
                   } // end for
    
                   javax.swing.JTextArea ta = new javax.swing.JTextArea( 10, 25 );
                   for( int j = 0; j < inventory.length; j++ )
                   {
    
                   // System.out.println( inventory[j].toString() );<-----------------
    
                        ta.append( inventory[j].toString()+"\n" );
                   } // end for
    
              // System.out.printf( "Total value of all inventory = $%.2f",
              // Inventory.etTotalValueOfAllInventory( inventory ) );
              // return; // <------------------above line and this line
              ta.append("Total value of all inventory = "+product.getTotalValueOfAllInventory(inventory));
              javax.swing.JFrame frame = new javax.swing.JFrame();
              frame.getContentPane().add( new javax.swing.JScrollPane(ta) );
              frame.pack();
              frame.setLocationRelativeTo( null );
              frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
              frame.setVisible( true );
    
         } // end main
    
    } // end class InventoryProgramPart5
    
    // The product class
    class product
    {
         String productNumber;
         String name;
         int numberOfUnits;
         double pricePerUnit;
    
         // create a new instance of Inventory
         // main constructor for the class
         product( String Item_Number, String Item_Name, int Items_in_Stock, double Item_Price )
         {
              productNumber = Item_Number;
              name = Item_Name;
              numberOfUnits = Items_in_Stock;
              pricePerUnit = Item_Price;
         } // end constructor
    
         // sets the product name
         public void setItemName( String Item_Name )
         {
              name = Item_Name;
         }
    
         // sets the product number
         public void setItemNumber( String Item_Number )
         {
              productNumber = Item_Number;
         }
    
         // sets the number of units in stock
         public void setItemsInStock( int Items_in_Stock )
         {
              numberOfUnits = Items_in_Stock;
         }
    
         // sets the price of the product
         public void setItemPrice( double Item_Price )
         {
              pricePerUnit = Item_Price;
         }
    
         // returns product name
         public String getItemName()
         {
              return name;
         }
    
         // returns product number
         public String getItemNumber()
         {
              return productNumber;
         }
    
         // returns units in stock
         public int getItemsInStock()
         {
              return numberOfUnits;
         }
    
         // returns product price
         public double getItemPrice()
         {
              return pricePerUnit;
         }
    
         // returns the total value of inventory
         public double getInventoryValue()
         {
              return pricePerUnit * numberOfUnits;
         }
    
         public static double getTotalValueOfAllInventory( product[] inv )
         {
              double tot = 0.0;
              for( int i = 0; i < inv.length; i++ )
              {
                   tot += inv[i].getInventoryValue();
              }
              return tot;
         }
    
         public String toString()
         {
              StringBuffer sb = new StringBuffer();
              sb.append( "Product Name:  \t" ).append( name ).append( "\n" );
              sb.append( "Product Number:  \t" ).append( productNumber ).append( "\n" );
              sb.append( "Units in Stock:       \t" ).append( numberOfUnits ).append( "\n" );
              sb.append( "Unit Price:            \t" ).append( String.format( "$%.2f%n", pricePerUnit ) );
              sb.append( "Inventory Value:  \t" ).append(String.format("$%.2f%n", this.getInventoryValue())); 
                   return sb.toString();
         } // end toString
    } // end class product
    
    class manufacturer extends product
    { 
         // manufacturer of product
         String manufacturer;
    
         // percentage added to base price as restocking fee
         double restockingFee;
    
         // actual inventory value will be the base inventory value plus
         // the base inventory value times this amount ( value + ( value * restockingFee ) )
         public manufacturer( String Item_Name, double restockingFee, String Item_Number,
              String manufacturer, int Items_in_Stock, double Item_Price )
    
         {
              super( Item_Number, Item_Name, Items_in_Stock, Item_Price);
                   this.manufacturer = manufacturer;               this.restockingFee = restockingFee;
         } 
    
         // returns inventory value plus the restocking fee
         public double getInventoryValue()
         {
              return super.getInventoryValue() + ( super.getInventoryValue() * restockingFee );
         } // end getInventoryValue
    
         public String toString()
         {
              StringBuffer sb = new StringBuffer( "Manufacturer:       \t" ).append( manufacturer ).append( "\n" );
              sb.append( super.toString() );
                   return sb.toString();
    
         } // end toString
    
    } // end class manufacturer
    
    abstract class RunInv extends JFrame implements ActionListener
    {
         // utility class for displaying the picture
         private class MyPanel extends JPanel
         {
              ImageIcon image = new ImageIcon( "HTML.jpg" );
              int width = image.getIconWidth();
              int height = image.getIconHeight();
              long angle = 0;
              public MyPanel()
              {
                   super();
              }
    
              public void paintComponent( Graphics g )
              {
                   super.paintComponent(g);
                   Graphics2D g2d = ( Graphics2D )g;
                   g2d.rotate( Math.toRadians(angle), 200+width/2, 90+height/2 );
                   g2d.drawImage( image.getImage(), 5, 5, this );
                   g2d.dispose();
              }
    
         } // end class MyPanel
    
         int currentIndex; // currently displayed Item
         manufacturer[] inventory = new manufacturer[5];
         JLabel name;
         JLabel number;
         JLabel manufacturer;
         JLabel units;
         JLabel price;
         JLabel fee;
         JLabel totalValue;
         JTextField nameField = new JTextField(20);
         JTextField numberField = new JTextField(20);
         JTextField manufacturerField = new JTextField(20);
         JTextField unitsField = new JTextField(20);
         JTextField priceField = new JTextField(20);
         JTextField totalValueField = new JTextField(20);
         JPanel display;
         JPanel displayHolder;
         JPanel panel;
         
         public RunInv()
         {
              // makeTheDataItems();
              setSize( 700, 300 );
              setTitle( "Inventory Program" );
    
              // make the panels
              display = new JPanel();
              JPanel other = new JPanel();
              JPanel picture = new MyPanel();
              JPanel buttons = new JPanel();
              JPanel centerPanel = new JPanel();
              displayHolder = new JPanel();
              display.setLayout( new GridLayout( 3, 3 ) );
              other.setLayout( new GridLayout( 2, 1 ) );
    
              // make the labels
              name = new JLabel( "Product Name:" );
              number = new JLabel( "Product Number:" );
              manufacturer = new JLabel( "Manufacturer:" );
              units = new JLabel( "Units in Stock:" );
              price = new JLabel( "Price     :$" );
              fee = new JLabel( "Fee     :$" );
              totalValue = new JLabel( "Total value : $" );
    
              // make the buttons
              JButton first = makeButton( "First" );
              JButton next = makeButton( "Next" );
              JButton previous = makeButton( "Previous" );
              JButton last = makeButton( "Last" );
              JButton exit = makeButton( "Exit" );
    
              // other buttons
              JButton add = makeButton( "Add" );
              JButton delete = makeButton( "Delete" );
              JButton modify = makeButton( "Modify" );
              JButton save = makeButton( "Save" );
              JButton find = makeButton( "Find" );
                   
                   // add the labels to the display panel
                   display.add( name );
                   display.add( number );
                   display.add( manufacturer );
                   display.add( units );
                   display.add( price );
                   display.add( fee );
                   display.add( totalValue );
    
                   // add the buttons to the buttonPanel
                   buttons.add( first );
                   buttons.add( previous );
                   buttons.add( next );
                   buttons.add( last );
                   buttons.add( exit );
    
                   // add the picture panel and display to the centerPanel
                   displayHolder.add( display );
                   centerPanel.setLayout( new GridLayout( 2, 1 ) );
                   centerPanel.add( picture );
                   centerPanel.add( displayHolder );
                   other.add( buttons );
                   JPanel forAdd = new JPanel(); // add the other buttons to this panel
                   forAdd.add( find );
                   forAdd.add( add );
                   forAdd.add( modify );
                   forAdd.add( delete );
                   forAdd.add( save );
                   other.add( forAdd );
              
                   // add the panels to the frame
                   getContentPane().add( centerPanel, "Center" );
                   getContentPane().add( other, "South" );
                   this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                   setVisible( true );
         
         } // end RunInv
    
         // method for creating and dressing buttons
         private JButton makeButton( String label )
         {
              JButton button = new JButton( label );
              button.setActionCommand( label );
              button.addActionListener( this );
              return button;
         }
         
    } // end class RunInv extends JFrame implements ActionListener

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You appear to have two JFrames in this program. The first is a plain JFrame to which you add a JScrollPane to the contentPane that shows your inventory information as text:
    Java Code:
        javax.swing.JFrame frame = new javax.swing.JFrame();
        frame.getContentPane().add(new javax.swing.JScrollPane(ta));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    The other JFrame is the RunInv class itself, a class that is never instantiated (never made into an object) and never shown. It is this JFrame that holds your buttons.

    It's as if you are trying to glue two separate programs together.
    Last edited by Fubarable; 08-11-2008 at 03:21 AM.

  3. #3
    ljk8950 is offline Member
    Join Date
    Jul 2008
    Posts
    43
    Rep Power
    0

    Default

    Fubarable,

    I tried to instantiate the JFrame in the main class. It won't let me do that because the RunInv class is abstract. My teacher said for whatever reason I have to have that class abstract or it won't compile. Any other ideas?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You made it abstract because it implements the ActionListener interface but does not define the only method declared in this interface, actionPerformed. I recommend that you get rid of the JFrame creating code that I posted above, that you add an actionPerformed method to your RunInv class, and that you read the Sun tutorial on Buttons and ActionListeners so you'll know what to put in that method to make your program work. The key to your program really is going to be the ActionListener's actionPerformed method.

  5. #5
    ljk8950 is offline Member
    Join Date
    Jul 2008
    Posts
    43
    Rep Power
    0

    Default

    Fubarable,

    I totally get what you are saying. How do I fix it when I have an abstract RunInv class? I tried a few different things and none of them worked.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Make your RunInv class not be abstract and give it the required actionPerformed method.
    2) Subclass RunInv and in the subclass add the required method, actionPerformed.
    3) Make RunInv non-abstract and don't have RunInv implement the ActionListener interface. Use a separate ActionListener class(es?) to handle all of your actions.

    Myself, I'd opt for number 3 and either use anonymous inner ActionListener classes or stand alone ActionListener classes, but I don't recommend that for you at this stage. Your best bet is probably option number 1.

  7. #7
    ljk8950 is offline Member
    Join Date
    Jul 2008
    Posts
    43
    Rep Power
    0

    Default

    Fubarable,

    Thank you for the advice. However, I do not know if I have enough time to change my program to that extent. My program is due is 4 hours. I wish I had a teacher that was available for me to ask my questions. It is really hard when you have to learn everything on your own. I am beginning to wonder why I am paying $11,000 per year to go to school and learn when there are no teachers to teach you. Thanks again and I will see what I can do betwen now and my deadline.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I wish you the best of luck.

  9. #9
    ljk8950 is offline Member
    Join Date
    Jul 2008
    Posts
    43
    Rep Power
    0

    Default

    Fubarable,

    Thank you. I am going to lodge a formal complaint with the University regarding teachers not being available during their posted office hours to take student calls. Thanks again for all your help!

Similar Threads

  1. Replies: 0
    Last Post: 06-28-2008, 10:26 PM
  2. Errors driving me crazy! although compiles fine
    By irishsea2828 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 04:23 PM
  3. Display program
    By nhlfan in forum New To Java
    Replies: 2
    Last Post: 11-22-2007, 01:00 AM
  4. Swing program to display JVM information
    By satya007 in forum AWT / Swing
    Replies: 3
    Last Post: 11-13-2007, 10:59 AM
  5. My program doesnt display anything
    By Bojevnik in forum AWT / Swing
    Replies: 2
    Last Post: 10-19-2007, 03:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •