B4J Question How to Print Text

William Hunter

Active Member
Licensed User
Longtime User
I have been looking for a way to print a plain text file from a B4J project. I have found reference to the JTextComponentprinting API in The Java Tutorials at the webpage below:

http://docs.oracle.com/javase/tutorial/uiswing/misc/printtext.html#layout

Is it possible to use this API in a B4J project? If so, how would this be done? Does anyone have a simple demo to share?
 

William Hunter

Active Member
Licensed User
Longtime User
Thank you for your response giga. I have looked at PrintHtml previously, and have printed the contents of a txt file with PrintHtml by changing the txt extension of the file to html. The down side of this is that DHTML Editing Control must be installed on any machine on which the B4J app is to be run. This would rarely be the case.

I had hoped to find something native to Java that would be similar to the Windows ShellExecute function of shell32.dll. I would like to avoid dependency problems, but haven’t had any luck so far.

Regards
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
You should be able to use print.exe to print a text file.
Thank you for the response Erel and steve105. Print.exe is a print utility going back to the days of MS-DOS. It’s not all that useful today due to a number of limitations. As far as I know, it will only output to LPT and COM ports. Those of us with USB printers are out of luck. Even when used to print to a parallel or serial printer, you can’t simply output to the default printer. You must direct output to a specific port. I don’t see it as a practical means of printing from within an application.

I have been able to print the contents of an HTML file from my app using the PrintHtml utility. In printing a multiline text PrintHtml cannot see the line breaks, and prints all text as one line. When I open the HTML file in Notepad++ it displays correctly as multiline text, and Notepad++ will also print the text as multiline.

In that I’m using File.WriteList I have no means of replacing line characters, if that is what PrintHtml requires. So, I think I will have to give up on this. Hopefully B4J will include a more refined print utility in a future release.

Happy New Year
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I've looked at Print.exe a little, apparently you can use 'Net Use' to map a USB or even network printer to an LPT port. I've not tried it yet though but it still may be useful for plain text.

HTML ignores linefeeds and requires the <br> tag to throw a line if you can add that where you want the line breaks.

Happy new year to you too.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
HTML ignores linefeeds and requires the <br> tag to throw a line if you can add that where you want the line breaks.
Thanks for the info steve105. We have a Rube Goldberg solution to the problem. I appended "<br/>" to each item as it was added to the listwiew. I now have multiline printing. The only downside is, that appearing at the end of each item in the listview we now have <br/>. Oh well, it’s not like it’s a money making venture. I still bet Erel will have a more useful printing utility sometime down the road. He just on keeps delivering the goodies.

Best regards
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You could use the addsingleline2 or addtwolines2 methods and add the <br> tag to the returned value if that's how you are building your document. The <br> tag wouldn't then appear in the Listview but would be returned when an item is clicked.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
Just curious Are you starting and ending the <br> <br/>. As you may know in a html document the br won't show when run.
Hello giga. I am using a listview in my app to display the contents of a HTML(text only) file. The listview is the only place where <br/> can be seen, as I have appended it to each item as it was added to the listview. It is not displayed when the HTML file is viewed in a browser, nor does it appear in printed output.

Regards
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
You could use the addsingleline2 or addtwolines2 methods and add the <br> tag to the returned value if that's how you are building your document. The <br> tag wouldn't then appear in the Listview but would be returned when an item is clicked.
Thank you for your response steve105. I tried your suggestion, but get the error message - Unknown member: addsingleline2. The addsingleline2 method does not seem to be available in the jCore library for B4J

Regards
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Oops, sorry wrong platform. However, I haven't quite run out of ideas yet. You could keep a map with the altered versions and look it up when an item is selected, Map by item number.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I have a little library called jPrint.
It works on the principal that text swing components can print, therefore all I do is take the JavaFX object, grab the text then put into a swing textarea and print that. It even displays a dialogue box to select printer etc.
Which I believe is what your original question asked.
 
Last edited:
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
@ steve105 – Thank you for all your efforts. The last suggestion is a little beyond my skill level. I prefer to use the KISS method. “Keep It Simple Stupid”. Any reference to stupid is entirely self-directed. :rolleyes:

@ Daestrum – Yes, that was my original question. Your jPrint library would be of interest to me. If you would care to share this library, along with a short demo explaining its use that would be greatly appreciated. :)

Regards
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I am re-writing the lib at the moment, it was freezing the b4j prog, so I now run the print routine on a separate thread.
I can give you the source of the lib - if you know java its easy to add the bits you need. At present it just handles textareas, but has the logic in it to recognize most javafx items.
I have attached the lib to this post - the lib isn't finished so cannot as yet go into the library forum.
This is a taster if you like.
 

Attachments

  • jPrintLib.zip
    2.8 KB · Views: 286
  • jPrintTest.zip
    1 KB · Views: 329
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
I am re-writing the lib at the moment, it was freezing the b4j prog, so I now run the print routine on a separate thread.
I can give you the source of the lib - if you know java its easy to add the bits you need. At present it just handles textareas, but has the logic in it to recognize most javafx items.
I have attached the lib to this post - the lib isn't finished so cannot as yet go into the library forum.
This is a taster if you like.
Thank you Daestrum - I have downloaded both zip files and will take a look. Whether I will have any success in modifying your library in any way is another question. I have only completed one library using Eclipse, and that was a rough ride. There is nothing about Eclipse that is familiar to me. I never posted my library as its function is now incorporated into a standard B4A library.

When you have completed re-writing your library I'm sure it will be a useful addition to the B4J additional libraries section. Thank you again for letting me have a look at this. Printing from B4J is a subject that most members would find of interest.

Regards
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
It sounds harder than it is.
This is the lib source so far, nothing fancy. Just need examples of text items to be able to add them.
 

Attachments

  • jPrintSource.zip
    1.1 KB · Views: 291
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
It sounds harder than it is.
This is the lib source so far, nothing fancy. Just need examples of text items to be able to add them.
Thanks again Daestrum - I will take a serious look at this. If I can get the contents of a text file to load into the TextArea this would be all I would need.

Happy New Year
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
It's possible to print directly from a text file without having to load it into a textfield, or so the documentation says.

Happy New Year

Just noticed a bug in that library source I posted.
look for 'this.jt' it should just be 'jt' remove the 'this.' or else it remembers the last thing it printed and causes an exception.
 
Last edited:
Upvote 0
Top