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

    Default Output to TextArea

    Below is my program. How do I get the contents to display in TextArea?

    Java Code:
    // CheckPoint:  Inventory4.java
    // Week 7
    // This program calculates inventory value
    
    import java.awt.*;
    import javax.swing.*;
    
    public class Inventory4
    {
    
         // main method begins program execution
         public static void main( String args[] )
         {
              
              // display a welcome message to the InventoryProgramPart4 user
              System.out.println( "Welcome to Inventory Program Part 4!" );
    
              // office supplies
              
              supplies[] supplies = new supplies[100]; // an array of 100 supplies
              manufacturer[] manufacturer = new manufacturer[100];
                    
              manufacturer notepads = new manufacturer
                   ( 4000, "notepads", 60, 2.75, "Ampad" );
              manufacturer pencils = new manufacturer
                   ( 5000, "pencils", 75, 1.25, "Bic" );
              manufacturer folders = new manufacturer
                   ( 2000, "folders", 30, 4.75, "3M" );
              manufacturer envelopes = new manufacturer
                   ( 1000, "envelopes", 15, 5.25, "Universal" );
              manufacturer markers = new manufacturer
                   ( 3000, "markers", 45, 3.50, "Sanford" );
              manufacturer temp[] = new manufacturer[1];
              for( int i = 0; i < args.length - 1; i++ )
              {
                   for( int j = 0; j < args.length - 1; j++ )
                   {
                        if( manufacturer[j].getSuppliesName().compareToIgnoreCase( 
                             manufacturer[j+1].getSuppliesName() ) > 0 )
                        {
                             temp[0] = manufacturer[j];
                             manufacturer[j] = manufacturer[j+1];
                             manufacturer[j+1] = temp[0];
                        } // end if
                   } // end for
              } // end for
                                 
              // display the inventories one at a time
              envelopes.showInventory();
              folders.showInventory();
              markers.showInventory();
              notepads.showInventory();
              pencils.showInventory();
    
              // sort supplies by name
              for ( int i = 0; i < args.length; i++ )
              System.out.println( args[i] + ", " );
    
              double array[] = { 82.69, 149.63, 165.38, 173.25, 98.44 };
              double total = 0;
    
              // add each element's value to total
              for ( int counter = 0; counter < array.length; counter++)
                   total += array[ counter ];
              System.out.printf( "\nTotal inventory value is: $%.2f\n", total );
    
              System.out.println( "\nThank you for using Inventory Program Part 4!\n" );
     
              javax.swing.JTextArea ta = new javax.swing.JTextArea( 10, 20 );
                   for( int i = 0; i < args.length; i++ )
                   {
                        ta.append( args[i].toString()+"\n" );  
                   } // end for
    
              {
              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 frame
             
         } // end main
    
    } // end class Inventory4
    
    // Office Supplies
    class supplies
    {
         public int suppliesNumber;
         public String suppliesName = new String();
         public int suppliesUnits;
         public double suppliesPrice;
    
         // set supplies number
         public void setSuppliesNumber( int number )
         {
              this.suppliesNumber = number;
         } // end method set supplies number
    
         // return supplies number
         public int getSuppliesNumber()
         {
              return suppliesNumber;
         } // end method get supplies number
    
         // set supplies name
         public void setSuppliesName( String name )
         {
              this.suppliesName = name;
         } // end method set supplies name
    
         // return supplies name
         public String getSuppliesName()
         {
              return suppliesName;
         } // end method get supplies name
    
         // set supplies in stock
         public void setSuppliesUnits( int units )
         {
              this.suppliesUnits = units;
         } // end method set supplies units
    
         // return supplies units
         public int getSuppliesUnits()
         {
              return suppliesUnits;
         } // end method get supplies units
    
         // set supplies price
         public void setSuppliesPrice( double price )
         {
              this.suppliesPrice = price;
         } // end method set supplies price
    
         // return supplies price
         public double getSuppliesPrice()
         { 
              return suppliesPrice;
         } // end method get supplies price
    
         // calculate supplies inventory value
         public double getValue()
         {
              return suppliesUnits * suppliesPrice;
         } // end method supplies inventory value 
    
        // four-argument constructor
         supplies( int number, String name, int units, double price )
         { 
              suppliesNumber = number;
              suppliesName = name;
              suppliesUnits = units;
              suppliesPrice = price;
         } // end four-argument constructor
    
        // display inventory
         public void showInventory()
         {
              System.out.println(); // outputs blank line
    
              System.out.println( "Product Number:  "+suppliesNumber );
              System.out.println( "Product Name:  "+suppliesName );
              System.out.println( "Units in Stock:  "+suppliesUnits );
              System.out.printf( "Unit Price:  $%.2f", suppliesPrice );
    
              // value() method and display the value
              System.out.printf( "\nInventory value of "+suppliesName+ " is = $%.2f\n",
                   getValue() );                           
      
        } // end display inventory
    
    } // end class supplies
    
    class manufacturer extends supplies
    {
         // holds the supplies manufacturer
         private String suppliesManufacturer;
    
         // five-argument constructor
         public manufacturer( int number, String name, int units,
              double price, String manufacturer )
         {
              super( number, name, units, price );
              // validate and store supplies manufacturer
              setSuppliesManufacturer( manufacturer ); 
         } // end five-argument constructor
    
         // set supplies manufacturer
         public void setSuppliesManufacturer( String manufacturer )
         {
              this.suppliesManufacturer = manufacturer;
         } // end method set supplies manufacturer
    
         // return supplies manufacturer
         public String getSuppliesManufacturer()
         {
             return suppliesManufacturer;
         } // end method get supplies manufacturer
    
         // calculate restocking fee
         public double getRestockingFee()
         {
              return super.getValue() * 0.05;
         } //end method calculate restocking fee
              
         //return String representation of suppliesManufacturer
         public String toString()
         {
              String formatString = "Manufacturer:  %s";
              formatString += "\nRestocking Fee:  $%.2f\n";
              formatString = String.format( formatString, suppliesManufacturer,
                   super.getValue() * 0.05 );          
              return( formatString );
         } // end toString()
    
         // display inventory
         public void showInventory()
         {
              super.showInventory();
              System.out.println( toString() );
              
              // Display value plus restocking fee
              System.out.printf( "\nTotal Inventory value of "+suppliesName+ " is = $%.2f\n",
                   super.getValue() * 0.05 + super.getValue() );     
    
         } // end method display inventory
    
    } // end class manufacturer

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    the contents to display in TextArea
    Look at the API doc for the TextArea and read the set and append methods.
    What are "the contents"?

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

    Default

    What is the API doc? I am new to Java and not sure of all the terminology.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    Application Program Interface documentation. It documents all the java classes and their fields and methods.
    You will need to be very familiar with this to program.
    Its a free down load from the Sun site

  5. #5
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    If you need to display some text in the JTextArea ,use the same as you did with args in your program,ta.append(yourstring+"\n" );

Similar Threads

  1. TextArea with Unicode
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-16-2008, 10:53 PM
  2. How to get the position of character in TextArea
    By loganathan.lakshmanan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-19-2008, 12:06 AM
  3. TextArea Bug?
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 12:37 PM
  4. problems when I print in textarea
    By gabriel in forum New To Java
    Replies: 1
    Last Post: 07-26-2007, 06:57 PM
  5. textarea
    By ubuntu in forum AWT / Swing
    Replies: 4
    Last Post: 05-12-2007, 09:54 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
  •