Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Unhappy Java thread help and more...


    Hey guys, ive just joined DevShed, and im looking for some help , At the moment i have been given some work to work on, What i have been given is a "working" program of a catalogue shop eg argos. the user can check if a item is in stock, if it is they can buy it or not, and what not, now I have been asked to create a class that will show out standing collections and orders being processed. so at the moment the code i have for the displayGUI is this

    package Clients;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;

    import java.text.NumberFormat;
    import java.util.Locale;

    import Middle.*;

    /**
    * Implements the GUI for the Display client.
    * @author Michael Alexander Smith
    * @version 2.1
    */

    class DisplayGUI
    {
    private static final int H = 300; // Height of window pixels
    private static final int W = 400; // Width of window pixels

    private JLabel theAction = new JLabel();
    private JTextArea outStandingOrders = new JTextArea();
    private JScrollPane theSP = new JScrollPane();
    private OrderProcessing theOrder = null;

    //Added
    //private CheckOrders theCO = new CheckOrders();

    public DisplayGUI( RootPaneContainer rpc, MiddleFactory mf )
    {
    try //
    {
    theOrder = mf.makeOrderProcessing(); // DataBase Access
    } catch ( Exception e )
    {
    System.out.println("Exception: " + e.getMessage() );
    }
    Container cp = rpc.getContentPane(); // Content Pane
    Container rootWindow = (Container) rpc; // Root Window
    cp.setLayout(null); // No layout manager
    rootWindow.setSize( W, H ); // Size of Window

    Font f = new Font("Monospaced",Font.PLAIN,12); // Font f is

    theAction.setBounds( 110, 25 , 270, 20 ); // Message area
    theAction.setText( "" ); // Blank
    cp.add( theAction ); // Add to canvas

    theSP.setBounds( 110, 100, 270, 160 ); // Scrolling pane
    outStandingOrders.setText( "" ); // Blank
    outStandingOrders.setFont( f ); // Uses font
    cp.add( theSP ); // Add to canvas
    theSP.getViewport().add( outStandingOrders ); // In TextArea

    rootWindow.setVisible( true );

    }

    public void main(String args[])
    {
    outStandingOrders.setText("ARGH");
    }

    }




    Now for a Swing app to continue to update i need a thread, but i have never come across one before, this application uses one, but there is about 40 files to look thought and some aren't small, also with my code my "outStandingOrders.setText("ARGH");" doesnt work, my text box displays nothing, how can i get a thread to keep updating my class?

    Cheers guys/
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Please edit your post and surround your code with code tags:
    [code] <CODE HERE> [/ code] (without the space after the /) .
    Use the Go Advanced button and then use the # icon.

    updating my class?
    What values are being updated?
    Where is the code that updates them?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0
    Ok cheers about the code tags. well the information is getting updated by a class called order, this class is updated by cashier class, so if someone buys something, then it goes to order class, then the displayGUI needs to show the status of that order, now all of that code is done for me, all i need to do is get displayGUI to have a thread which will allow it to keep updating and call the order.methods to be placed into the testBox,
    but i cant seem to figure out how to get a thread to work.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    how to get a thread to work.
    Where is your code that tries to use a a thread?

    Take a look at the tutorial:
    http://docs.oracle.com/javase/tutori...y/threads.html
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0
    This is the code for the pickUp

    i think the pickUp uses thread

    Code:
    class BackGroundCheck extends Thread
      {
        public void run()
        {
          while ( true )
          {
            try
            {
              boolean claimed = theState.claim();      // 
              if ( claimed )                           // Can we use
              {                                        // T
                SoldBasket sb = 
                  theOrder.getOrderToPick();           // Waiting
                if ( sb != null )                      // Items to pick
                {                                      // T
                  theState.store( sb );                //  Remember
                  theOutput.setText( sb.getDetails() );//  Display
                  theAction.setText("Item to pick");   //   what to do
                } else {                               // F
                  theState.release();                  //  Free
                }
              }
              sleep(2000);                             // delay
            } catch ( Exception err )
            {
            }
          }
        }
      }
    Cheers
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Please edit your post and surround your code with code tags:
    [code] <CODE HERE> [/ code] (without the space after the /) .
    Use the Go Advanced button and then use the # icon.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0
    ok what about this, how can i get hash table to be displayed in a swing java text area?
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    A text area is for displaying String data. What does the hash table contain? Can you extract its contents as a String?
    If so, you can add it to the textarea for it to be displayed.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0
    Here is the code for the order class

    Code:
    package Processing;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.io.ByteArrayOutputStream;
    import java.io.PrintStream;
    import Catalogue.*;
    import Middle.*;
    
    /**
      * The order processing system.
      * @author  Michael Alexander Smith
      * @version 2.0
      */
     
    public class Order implements OrderProcessing
    {
      private static int theNextNumber = 1;          // Start at 1
      private ArrayList<SoldBasket>  theWaitingTray 
                                     = new ArrayList<SoldBasket>(10);
      private ArrayList<SoldBasket>  theBeingProcessedTray 
                                     = new ArrayList<SoldBasket>(10);
      private ArrayList<SoldBasket>  theWaitingToBeCollectedTray 
                                     = new ArrayList<SoldBasket>(10);
    
      /**
       * Used for debug information
       *
       */
    
      private String asString( SoldBasket bl )
      {
        ByteArrayOutputStream bos = new ByteArrayOutputStream( 1024 );
        PrintStream ps = new PrintStream( bos );
        ps.printf( "#%d (", bl.getOrderNo() );
        ArrayList<Product> p = bl.getProducts();
        for ( Product pr: p )
        {
           ps.printf( "%s:%d ", pr.getDescription(), pr.getQuantity() );
        }
        ps.printf( ")" );
        return bos.toString();
      }
    
      /**
       * Generates a unique order number
       * @return A unique order number
       */
    
      public synchronized int uniqueNumber()
      {
        return theNextNumber++;
      }
    
      /**
       * Add a new order to the order processing system
       */
    
      public synchronized void newOrder( SoldBasket bought )
             throws OrderException
      {
        // You need to modify and fill in the correct code
    	System.out.println( "DEBUG: New order" );
      }
    
      /**
       * Returns an order to pick from the warehouse
       *  if no order then returns null.
       * @return An order to pick or null if none
       */
    
      public synchronized SoldBasket getOrderToPick()
             throws OrderException
      {
    	// You need to modify and fill in the correct code
        System.out.println( "DEBUG: Get order to pick" );
        return null;
      }
    
      /**
       * Informs the order processing system that the order has been
       * picked and the products are now on the conveyor belt to
       * the shop floor.
       */
    
      public synchronized void informOrderPicked( int orderNo )
             throws OrderException
      {
        // You need to modify and fill in the correct code
        System.out.println( "DEBUG: Order picked " + orderNo );
        return;
      }
    
      /**
       * Informs the order processing system that the order has been
       * collected by the customer
       * @return true -> Order in system false -> no such order
       */
    
      public synchronized boolean informOrderColected( int orderNo )
             throws OrderException
      {
        // You need to modify and fill in the correct code
        System.out.println( "DEBUG: Order collected " + orderNo );
        return false;
      }
    
      /**
       * Returns information about all orders in the order processing system
       * This will consist of a map with the order numbers in the 3 queue
       * waiting, being picked, to be collected.
       * 1> Key "Waiting"       -> a list of orders waiting to be processed
       * 2> Key "BeingPicked"   -> a list of orders that are currently being picked
       * 3> Key "ToBeCollected" -> a list of orders that can now be collected
       * @return a Map with the keys: Waiting, BeingPicked, ToBeCollected
       */
    
      public synchronized Map<String, List<Integer> > getOrderState()
             throws OrderException
      {
        System.out.println( "DEBUG: get state of order system" );
        Map < String, List<Integer> > res = 
          new HashMap< String, List<Integer> >();
        res.put( "Waiting",       orderNos(theWaitingTray) );
        res.put( "BeingPicked",   orderNos(theBeingProcessedTray) );
        res.put( "ToBeCollected", orderNos(theWaitingToBeCollectedTray) );
    
        return res;
      }
      
      private List< Integer > orderNos( ArrayList<SoldBasket> queue )
      {
        List <Integer> res = new ArrayList<Integer>();
        for ( SoldBasket sb: queue )
        {
          res.add( sb.getOrderNo() );
        }
        return res;
      }
      
      public int returnNum(){
          int n = 9;
          return n;
        }
    
    
    }
    I need to show the information there in the textarea box
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    I need to show the information there in the textarea box
    Where is the "there"?
    Is it a class object? What class is it?
    Does the class have methods for getting its contents into a String?
    Once it is in a String then you can pass it to one of the textarea methods and have it displayed in the textarea.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by NormR
    Where is the "there"?
    Is it a class object? What class is it?
    Does the class have methods for getting its contents into a String?
    Once it is in a String then you can pass it to one of the textarea methods and have it displayed in the textarea.
    well that is the code for order, so could i maybe write a method in order to return a huge string with all the hash in it?
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Yes you could do that. It depends on what you want to see in the textarea.
    The Order class could have a method to format its contents as a String for displaying.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by NormR
    Yes you could do that. It depends on what you want to see in the textarea.
    The Order class could have a method to format its contents as a String for displaying.
    ok cool, any chance of a bit of code to format a hash data?
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    I have no idea what you mean by "hash data"
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by NormR
    I have no idea what you mean by "hash data"
    how can i convert the aarraylists of type soldbucket to a string?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo