Results 1 to 15 of 15
- 06-05-2011, 10:06 PM #1
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
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.
- 06-05-2011, 10:24 PM #2
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
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.
- 06-05-2011, 10:40 PM #3
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
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.javaLast edited by patriotsfan; 06-05-2011 at 10:48 PM.
-
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.
-
For the benefit of all, I've downloaded the file that you've linked to and will post it here:
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.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"; } }
- 06-05-2011, 11:08 PM #6
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
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.
-
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.
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.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?
- 06-05-2011, 11:54 PM #8
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
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.
-
- 06-06-2011, 01:03 AM #10
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
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?
- 06-06-2011, 01:08 AM #11
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
- 06-06-2011, 01:14 AM #12
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
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.
- 06-06-2011, 01:19 AM #13
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?
- 06-06-2011, 01:41 AM #14
Member
- Join Date
- May 2011
- Posts
- 38
- Rep Power
- 0
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.
- 06-06-2011, 02:29 AM #15
Similar Threads
-
RTF (rich text format) Formatting
By runedog48 in forum Advanced JavaReplies: 6Last Post: 04-19-2011, 11:43 PM -
Text format in JTextArea
By goodlily in forum AWT / SwingReplies: 4Last Post: 04-10-2011, 11:57 PM -
Text/String format..??
By Neullson in forum New To JavaReplies: 14Last Post: 07-23-2010, 08:51 AM -
Format some text with Java
By vampire in forum New To JavaReplies: 0Last Post: 02-18-2010, 06:45 AM -
Text Format Error
By MrFish in forum New To JavaReplies: 2Last Post: 01-13-2010, 01:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks