Results 1 to 15 of 15
  1. #1
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default Trying to get correct text format in .txt file

    I am having a problem with getting text to appear in the format I want. I have it formatted the way I want it to look inJTextArea but when I click button to send to a .txt file it is not the same as JtextArea. All the text is printing on the same line and some of the spaces are off by a tab.

    I am using Lucida Console font on JTextArea and on windows notepad text editor the same. The weird thing is when I click file to use file preview in windows explorer, the text looks good besides 2 of the 20 lines are off by a tab. When I actually double click file to open...all the text is on one line, Help me please.

  2. #2
    snaquetime is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    I think the JTextArea wraps the text within it, so although it appears on different lines, it is not. What is appearing in your text file is the actual text that is within the JTextArea, and if you wish to change that, you will have to parse through the text and add your own formatting, using \t for tab and \n for new lines.

  3. #3
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default

    I am using \n and \t. The weird thing to me is that i am seeing 4 different things in JTextArea, Window file preview, Netbeans file editor and when I actually open the .txt file. When I create the file and then open .txt file in netbeans the format is just how I want it, and then the JTextArea is perfect also. But...when I use file preview in windows explorer, the text is off by a cuple tabs(2 of 20 lines) and then When I actually open the file in Windows Notepad, all the text is on one line. I am think maybe Windows notepad does not support the \n from java but not sure. If you could offer any other suggestions I would appreciate.

    If you would like to look here is link to .txt file created http://dl.dropbox.com/u/27956178/JohnSmithWoody.txt
    Here is link to my java file that does calculations for JTextArea http://dl.dropbox.com/u/27956178/Invoice.java
    Last edited by patriotsfan; 06-05-2011 at 10:48 PM.

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

    Default

    I'll bet part of your problem is with fonts since the text in JTextArea loses all font information when it is extracted since the font is a property of the JTextArea not the text it contains. Differing fonts can effect the size of the text and how tabs work especially when considering mono-space vs. non mono-spaced fonts. Consider rather than using String formatting rather than tabs which can be used via String's String.format(...) method. Then when you display the text from the file, be sure to use a mono-spaced font such as one of the courier variants.
    Last edited by Fubarable; 06-05-2011 at 10:58 PM.

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

    Default

    For the benefit of all, I've downloaded the file that you've linked to and will post it here:

    Java Code:
    import java.text.DecimalFormat;
    
    public class Invoice 
    {
        private final double TC = 85.00; //Teeth Cleaing
        protected static boolean isTcChecked = false;
        private final double RB = 55.00; //Rabies Vaccination
        protected static boolean isRbChecked = false;
        private final double OS = 42.00; //Other Shots
        protected static boolean isOsChecked = false;
        private final double HW = 28.00; //Heartworm Test
        protected static boolean isHwChecked = false;
        private final double FC = 37.00; //Fecal Check
        protected static boolean isFcChecked = false;
        private final double FT = 65.00; //Fleas Treatment
        protected static boolean isFtChecked = false;
        private final double OV = 25.00; //Office Visit
        protected static boolean isOvChecked = false;
        private final double TAX = .088;
        protected static double OT; //Other
        protected static boolean isOtChecked = false;
        private double totalTax = 0.00;
        private double subTotal = 0.00;
        private double total = 0.00;
        private String tCInfo;
        private String rBInfo;
        private String oSInfo;
        private String hWInfo;
        private String fCInfo;
        private String fTInfo;
        private String oVInfo;
        private String oTInfo;
        DecimalFormat decForm = new DecimalFormat("#.##");
    
            public void calculate()
            {
                VetGUI.invoiceJTextArea.setText(null);
    
                if (isTcChecked)
                {
                    subTotal += (double)TC;
                    tCInfo = "TC\tTeeth Cleaning\t\t\t$85.00\n";
                }
                else
                    tCInfo = "";
    
                if (isRbChecked)
                {
                    subTotal += (double)RB;
                    rBInfo = "RB\tRabies Vaccination\t\t$55.00\n";
                }
                else
                    rBInfo = "";
    
    
                if (isOsChecked)
                {
                    subTotal += (double)OS;
                    oSInfo = "OS\tOther Shots\t\t\t$42.00\n";
                }
                else
                    oSInfo = "";
    
                if (isHwChecked)
                {
                    subTotal += (double)HW;
                    hWInfo = "HW\tHeartWorm Test\t\t\t$28.00\n";
                }
                else
                    hWInfo = "";
    
                if (isFcChecked)
                {
                    subTotal += (double)FC;
                    fCInfo = "FC\tFecal Check\t\t\t$37.00\n";
                }
                else
                    fCInfo = "";
    
    
                if (isFtChecked)
                {
                    subTotal += (double)FT;
                    fTInfo = "FT\tFleas Treatment\t\t\t$65.00\n";
                }
                else
                    fTInfo = "";
    
                if (isOvChecked)
                {
                    subTotal += (double)OV;
                    oVInfo = "OV\tOffice Visit\t\t\t$25.00\n";
                }
                else
                    oVInfo = "";
                
                if (isOtChecked)
                {
                    subTotal += (double)OT;
                    oTInfo = "OT\tOther\t\t\t\t$" + Double.toString(OT) + "\n";
                }
                else
                    oTInfo = "";
    
                totalTax = (double)((subTotal * TAX));
                total = totalTax + subTotal;
    
                VetGUI.invoiceJTextArea.append("Code\tProcedure\t\t\tCost\n");
                VetGUI.invoiceJTextArea.append("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
                VetGUI.invoiceJTextArea.append(tCInfo);
                VetGUI.invoiceJTextArea.append(rBInfo);
                VetGUI.invoiceJTextArea.append(oSInfo);
                VetGUI.invoiceJTextArea.append(hWInfo);
                VetGUI.invoiceJTextArea.append(fCInfo);
                VetGUI.invoiceJTextArea.append(fTInfo);
                VetGUI.invoiceJTextArea.append(oVInfo);
                VetGUI.invoiceJTextArea.append(oTInfo);
                VetGUI.invoiceJTextArea.append("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
                VetGUI.invoiceJTextArea.append("SubTotal\t\t\t\t" + "$" + decForm.format(subTotal) + "\n");
                VetGUI.invoiceJTextArea.append("Tax\t\t\t\t\t" + "$" + decForm.format(totalTax) + "\n");
                VetGUI.invoiceJTextArea.append("Total\t\t\t\t\t" + "$" + decForm.format(total) );
    
            }
    
        @Override
        public String toString()
        {
            return  VetGUI.invoiceJTextArea.getText() + "\n";
        }
    
    }
    One thing that strikes me is your use of static variables. It also looks as if the JTextArea is static as well. If so, I strongly advise you to not do this but to use instance fields instead in order to get the benefits of OOP.

  6. #6
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default

    Well I have checkboxes that change the JtextArea dynamically and recalculate the invoice when Selected/deselected. I think it would be alot more difficult to use instance fields but maybe I will try to fix after I have everything workind right. Any ideas on formatting the text though?

    So if I use the String.format() Would I only need to apply that to my toString methods or what would I need to change exactly?
    Last edited by patriotsfan; 06-05-2011 at 11:12 PM.

  7. #7
    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 patriotsfan View Post
    Well I have checkboxes that change the JtextArea dynamically and recalculate the invoice when Selected/deselected. I think it would be alot more difficult to use instance fields but maybe I will try to fix after I have everything workind right. Any ideas on formatting the text though?
    It would be easier in the long run to use instance fields. I'd have the GUI extract the data from its textfields and pass them as parameters to the calculate method which could then return a String that the GUI would then place in the JTextArea, so actually it wouldn't even be that difficult.

    So if I use the String.format() Would I only need to apply that to my toString methods or what would I need to change exactly?
    Myself I would use a StringBuilder and build my display string with this using String.format to add the data that you're currently adding to the JTextField. Then when done, I'd get the String from the StringBuilder via its toString() method and add it to the JTextField.

  8. #8
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default

    So how would you suggest I handle checkboxes? Currently through itemstatechanged from the GUI checkboxes they send to invoice a boolean true or false if is checked. According to true or false, then invoice sets the string data for each field and calculates how much the invoice is for 8 checkboxes.
    Last edited by patriotsfan; 06-06-2011 at 12:00 AM.

  9. #9
    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 patriotsfan View Post
    So how would you suggest I handle checkboxes?
    One way: If you had a calculate method with parameters, you'd pass in a boolean that represented the state of each check box.

  10. #10
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default

    Okay the String formatter did fix my problem and I did remove static variables lik eyou said, now I have one more problem. I have the calculate method doing the calculations and since they are local to that method when I call the toString, the strings are null and no calculations are sent, how can I fix this?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Move the definitions of the String variables to be class variables (vs local)
    If the String variable was local, how did your code compile? Do you have two definitions for the String variable?
    global and local

  12. #12
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default

    I had a global string variable set to null, then a calculate method which changes the string depending on boolean variable. But when I call the toString to print it says null.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Where do you assign a value to the String variable?
    If it is null, you are not setting that variable.
    Is there another with the same name at a different scope?

  14. #14
    patriotsfan is offline Member
    Join Date
    May 2011
    Posts
    38
    Rep Power
    0

    Default

    Invoice class contains calculate method and I call the toString to set the text of a JTextArea and it is done so correctly. Then from my main class I create a new Invoice object and I call invoice's tostring but all values are not set. they are still the same values when they were declared.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    I create a new Invoice object
    If its new it won't have what was in the old one.
    Why do you create a new one instead of using the old one?

Similar Threads

  1. RTF (rich text format) Formatting
    By runedog48 in forum Advanced Java
    Replies: 6
    Last Post: 04-19-2011, 11:43 PM
  2. Text format in JTextArea
    By goodlily in forum AWT / Swing
    Replies: 4
    Last Post: 04-10-2011, 11:57 PM
  3. Text/String format..??
    By Neullson in forum New To Java
    Replies: 14
    Last Post: 07-23-2010, 08:51 AM
  4. Format some text with Java
    By vampire in forum New To Java
    Replies: 0
    Last Post: 02-18-2010, 06:45 AM
  5. Text Format Error
    By MrFish in forum New To Java
    Replies: 2
    Last Post: 01-13-2010, 01:06 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
  •