I new to java and am writing a program that to display a purchase amounts for 5 items individually, with a running Total. I want to use a JTextArea box, but I am having trouble finding good information on how to properly code the box for display and how to put information into the columns and rows in the box. Any help with this would be appreciated, below is the current code in my program.
Code:import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JTextArea;
public class TreyNixon_P3Modified
{
protected static JTextArea textArea;
String header;
public TreyNixon_P3Modified()
{
header = ("Product #\t\t# of Units\t\tUnit Cost\t\tTotal purchase\n");
textArea = new JTextArea(header, 6, 4);
textArea.setEditable(false);
}
public static double getCost(int quantity, double price, double Total)
{
double SubTotal = (quantity * price);
Total = (Total + SubTotal);
SubTotal = 0.00;
return Total;
}
public static String convertToString(int PN , int U , double UC, double TP )
{
String SItem, SUnits, SUnitCost, STotal, display;
SItem = Integer.toString(PN);
SUnits = Integer.toString(U);
SUnitCost = Double.toString(UC);
STotal = Double.toString(TP);
display = ("SItem\t\tSUnit\t\tSUnitCost\t\tSTotal");
return display;
}
public static void main(String[] args)
{
//Declare variables
String ProdNum, NumUnits, again, display;
double UnitCost, Total;
int item, units;
do
{
Total = 0.00;
ProdNum = JOptionPane.showInputDialog(null, "Enter Product No. (1-5) or -1 to Quit:", "Select Item", +
JOptionPane.QUESTION_MESSAGE);
item = Integer.parseInt(ProdNum);
if (item == -1)
System.exit(1);
NumUnits = JOptionPane.showInputDialog(null, "Enter Quantity Purchased or -1 to Quit:", "Number of Units", +
JOptionPane.QUESTION_MESSAGE);
units = Integer.parseInt(NumUnits);
if (units == -1)
System.exit(1);
//use switches to figure cost of each item
switch(item)
{
case 1:
UnitCost = 2.98;
Total = getCost(units, UnitCost, Total);
display = convertToString(item, units, UnitCost, Total);
textArea.append(display + "\n");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
break;
case 2:
UnitCost = 4.50;
Total = getCost(units, UnitCost, Total);
display = convertToString(item, units, UnitCost, Total);
textArea.append(display + "\n");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
break;
case 3:
UnitCost = 9.98;
Total = getCost(units, UnitCost, Total);
display = convertToString(item, units, UnitCost, Total);
textArea.append(display + "\n");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
break;
case 4:
UnitCost = 4.49;
Total = getCost(units, UnitCost, Total);
display = convertToString(item, units, UnitCost, Total);
textArea.append(display + "\n");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
break;
case 5:
UnitCost = 6.87;
Total = getCost(units, UnitCost, Total);
display = convertToString(item, units, UnitCost, Total);
textArea.append(display + "\n");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
break;
}
again = JOptionPane.showInputDialog(null, "Do you wish to make another purchase? Y or N", "Another Transaction", +
JOptionPane.QUESTION_MESSAGE);
if (again == toUpper ("N"))
System.exit(1);
}while (again == toUpper("Y"));
}
}

