2

I'm taking a Java programming class at school and we've been working on a project in class - dealing with console applications. For the coming week, we're moving on to working on creating GUI applications with JAVA and I had an idea of what to do with one of my projects.

I wanted to redirect the console output to a text area inside the GUI. But I don't know if this is possible, or how to do it. Is it possible, if so, can somebody help me. I'm trying to design my form so that it looks like a cash register (with the receipt on one side of the register). In this case, the receipt will be the redirected console output.

Any help would be greatly appreciated.

Here's my source code:

package autoshop_invoice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.NumberFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AutoShop_Invoice
  {

        public static void total_sales() throws IOException
      {
        String text = "";
        String part_number = "";
        int num_items = 0;
        double price = 0.0;
        double tax = 0.0;
        double total_sale = 0.0;

        //Prompt user for part number and store value in variable part_number.
        System.out.println("Enter Part Number then hit enter.");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try
          {
            part_number = in.readLine();
          } catch (IOException ex)
          {
            Logger.getLogger(AutoShop_Invoice.class.getName()).log(Level.SEVERE, null,     ex);
          }

        //Prompt user to enter number of items sold and store value in variable num_items.
        System.out.println("Enter Number of Items sold then hit enter.");
        in = new BufferedReader(new InputStreamReader(System.in));
        text = in.readLine();
        num_items = Integer.parseInt(text);

        //Prompt user to enter Price per Item and store value in variable num_items.
        System.out.println("Enter Price per Item then hit enter.");
        in = new BufferedReader(new InputStreamReader(System.in));
        text = in.readLine();
        price = Double.parseDouble(text);

        //Display the total sale WITH tax calculated.
        total_sale = num_items * price;
        tax = total_sale * .06;
        total_sale = total_sale + tax;
        //DecimalFormat df = new DecimalFormat("#.##"); 
        NumberFormat nf = NumberFormat.getCurrencyInstance();  //Get the current system locale currency
        System.out.println();
        System.out.println("***********************************");
        System.out.print("Part Number: " + part_number + " ");  //Display the Part Number being sold
        System.out.println("QTY: " + num_items);  //Display the quantity of items sold
        System.out.println("Sub-Total is: " + nf.format(total_sale));   //Display sub-total rounded to 2 decimal points
        System.out.println("MD Sales Tax due (6%): " + nf.format(tax));  //Display the calculated sales tax
        //Display grand-total rounded to 2 decimal points and formatted with the locale currency
        //System.out.println("Grand-Total sales is: " + df.format(nf.format(total_sale + tax)));
        System.out.println("Grand-Total sales is: " + nf.format(total_sale + tax)); 
        System.out.println("***********************************");
      }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException
      {
        // TODO code application logic here
        total_sales();
      }
  }
harpun
  • 4,022
  • 1
  • 36
  • 40
Kismet Agbasi
  • 557
  • 2
  • 8
  • 28
  • You don't want to redirect the console output for this. But instead print directly to the receipt object and redraw it's container. I think you might need to learn a bit more about creating GUIs before you do down this path... Look into the Swing classes as it makes GUI creation in Java pretty simple – JNYRanger Oct 08 '13 at 18:18
  • possible duplicate of [Logging in Java GUI](http://stackoverflow.com/questions/17828152/logging-in-java-gui) – Jonathan Drapeau Oct 08 '13 at 18:19
  • @segfault - I didn't show the GUI because I didn't which parts of the code to show as I'm fairly new to JAVA programming. – Kismet Agbasi Oct 08 '13 at 18:23
  • possible duplicate of [How to redirect all console output to a Swing JTextArea/JTextPane with the right encoding?](http://stackoverflow.com/questions/1522444/how-to-redirect-all-console-output-to-a-swing-jtextarea-jtextpane-with-the-right) – Sage Oct 08 '13 at 18:23
  • @JNYRanger - you suggestion sounds interesting, how would I do that? – Kismet Agbasi Oct 08 '13 at 18:24
  • @Jonathan Drapeau - I will take a look at that link and read up on it. Thanks. – Kismet Agbasi Oct 08 '13 at 18:24
  • @KismetAgbasi depending on where you're getting your data from to fill the receipt you can simply use a textbox or textarea and use the `.append(string)` method. Otherwise you may need to created a `PrintStream` or pipe to stream the data into the various components of your GUI. Take a look at this link to get you started: http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html Keep in mind this is a very basic way of handling this, but should at least point you in the right direction! – JNYRanger Oct 08 '13 at 21:10

2 Answers2

2

Write your own OutputStream implementation that adds any bytes written to the text area. Then wrap it in a PrintStream and pass it to System.setOut():

System.setOut(new PrintStream(myTextAreaOutputStream));
Thomas
  • 174,939
  • 50
  • 355
  • 478
0

You can use Java Swing to easily create a form and implement its functionality.

Take a look at this tutorial: http://docs.oracle.com/javase/tutorial/uiswing/start/

A walk through on a basic project starts here using Netbeans http://docs.oracle.com/javase/tutorial/uiswing/learn/settingup.html

It's pretty straight forward. Lay out the controls, and then implement the functionality. I think their Celsius converter example will get you very close to finishing your project with just a few modifications.

Lea
  • 602
  • 9
  • 20
  • Thanks, I will definitely take a look at the link you suggested. I really appreciate taking the time Lea. – Kismet Agbasi Oct 08 '13 at 18:25
  • No problem, did you get the project completed? If you could, please accept this answer! thanks :-) – Lea Oct 17 '13 at 22:26