Results 1 to 11 of 11

Thread: Using JTextArea

  1. #1
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default Using JTextArea

    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.

    Java 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"));
    	}		
    }

  2. #2
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    Your problem is that it does not show? I cant find where you add it to your GUI?

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

    Default

    Quote Originally Posted by santa View Post
    Your problem is that it does not show? I cant find where you add it to your GUI?
    Yep, and the GUI it is shown in can be as simple as showing it in a JOptionPane:

    Java Code:
    JOptionPane.showMessageDialog(null, textArea);

    Also, there's a lot of redundancy in that code that removing will make it easier for us and others to understand and easier for you to fix.

  4. #4
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    Funkey reply =)

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

  6. #6
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    I added the JOptionPane.showMessageDisplay(null, textArea); , but it only displays a blank pane.

  7. #7
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    yes .. I saw a scenaro in my head where he wrote the reply first .. then he saw the JOptionPane.showMessageDialog(null, textArea); ... no offence intended =P I also tend to jump the gun ;P

  8. #8
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    That's what happened, sorry. I replied too early then say the second reply.

  9. #9
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    can u show some code so that we can see where/how you added it? =)

  10. #10
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    Will post in a few minutes, trying to clean up some of the redundancies for readablility.

  11. #11
    tnixon22 is offline Member
    Join Date
    Jan 2011
    Posts
    22
    Rep Power
    0

    Default

    Java Code:
    import java.util.*;
    import java.awt.*; 
    import javax.swing.*;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    
    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 void 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\t" + SUnits + "\t\t" + SUnitCost + "\t\t" + STotal + "\n");
    		
    
    		JOptionPane.showMessageDialog(null, textArea);
    			textArea.append(display);
    	}
    	
    	public static void main(String[] args)
    	{
    		//Declare variables
    		String ProdNum, NumUnits, again;
    		double UnitCost, Total;
    		int item, units;
    		
    		//Start Main Loop						
    		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);
    					convertToString(item, units, UnitCost, Total);
    					break;
    					
    				case 2:
    					UnitCost = 4.50;
    					Total = getCost(units, UnitCost, Total);
    					convertToString(item, units, UnitCost, Total);
    					break;
    
    										
    				case 3:
    					UnitCost = 9.98;
    					Total = getCost(units, UnitCost, Total);
    					convertToString(item, units, UnitCost, Total);
    					break;
    									
    				case 4:
    					UnitCost = 4.49;
    					Total = getCost(units, UnitCost, Total);
    					convertToString(item, units, UnitCost, Total);
    					break;
    										
    				case 5:
    					UnitCost = 6.87;
    					Total = getCost(units, UnitCost, Total);
    					convertToString(item, units, UnitCost, Total);
    					break;
    			}
    			
    				again = JOptionPane.showInputDialog(null, "Do you wish to make another purchase?  Y or N", "Another Transaction", +
    																JOptionPane.QUESTION_MESSAGE);
    																															
    		}while (again == ("Y") || again == "n");
    	}		
    }

Similar Threads

  1. JTextArea
    By larry_d1990 in forum Advanced Java
    Replies: 4
    Last Post: 01-11-2011, 05:44 PM
  2. JTextArea
    By weikang in forum AWT / Swing
    Replies: 1
    Last Post: 12-01-2010, 04:51 AM
  3. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 07:51 PM
  4. Tab key in JTextArea
    By KristoZ in forum New To Java
    Replies: 1
    Last Post: 09-25-2009, 07:27 PM
  5. JTextArea
    By saytri in forum New To Java
    Replies: 0
    Last Post: 01-13-2008, 01:07 AM

Posting Permissions

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