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
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.
Thanks again Daestrum - The change has been made. Printing directly from a text file would be the way for me to go. I see I have some reading to do. Where did you find the documentation for this? I have been flying blind. There can't be a lot of coding involved, but finding a sample code snippet has so far eluded me.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
This is a good starting point
http://docs.oracle.com/javase/tutorial/2d/printing/swing.html
Lots to read :)

If you look at the end of the source I posted - it contains the code for file printing, I just have not implemented it in the library yet.(the copy paste screwed up the format , but it's still readable)
Its tricky getting it to print to the correct printer, it doesn't default to the default printer. You have to interrogate the list to find the correct printer.
Maybe I will just throw a JFrame up and let user decide which printer.
Also the text file must end with a CRLF, or else it won't print the last line of the file.

Will upload the new version tomorrow for you to test.

What have I let myself in for :) I now find I am using java function I never knew existed a few hours ago.

If anyone feels brave enough to offer some coding help, I would appreciate it.
 
Last edited:
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
@ Daestrum – I tested your library using your jPrintTest. I had one failure and two successes. Printing to a HP Laser failed, in that while the paper was fed through the printer, no text was printed. I then tried printing to two HP Injets and in both cases there was success.

I’ve done a lot of reading of the Swing tutorials. Yes, it does cause one to think – “what the hey!!!”. I look forward to testing the next version of your library. I would very much like to have the ability to print the contents of a text file directly from an app. At one time the Windows PRINT command could be used, but now on most modern computers, PRINT will be useless, because it needs a printer port like LPT1 or COM1 etc., whereas most printers nowadays are connected either to USB ports or over a TCP/IP connection.

Regards
 
Upvote 0

dilettante

Active Member
Licensed User
Longtime User
Here is another take on this. Instead of trying to get too fancy, it prints via WordPad using Shell in the manner you wanted to print via Print.exe:

<path to wordpad>\wordpad.exe /p "<path and file>"

My understanding is that if you can jump through all of the hoops to use javax.print then it would be possible to raise a printer selection dialog to obtain a desired printer name, which would allow you to use:

<path to wordpad>\wordpad.exe /pt "<path and file>" "printer name"

Some really bizarre design decisions seem to have been made when many of the official Java libraries were created.

However as things are in my demo it just prints to the user's default printer.


Why WordPad and not Notepad? Well here we get down to B4J (Java) compromise #1: the notion that text file lines should be LF delimited rather than CRLF. Notepad doesn't cope well, while Wordpad shrugs it off.

There are other frustrating B4J compromises that I believe will ultimately hurt it as a language for many programmers. Some stem from B4J's javaesque nature, others due to B4J implementation decisions that probably go back to B4A, and others are due to design decisions made when 3rd party contributed B4J libraries were designed. There are comments in the code of this small demo that call out some of these issues that result in a lot of extra code required to accomplish trivial things.

Feel free to ignore these comments. They are not there as criticisms or because I hope or expect things to change, but as reminders to myself when I have to reinvent these wheels in future programs.


The demo:

This program has a Form with 3 buttons and a hacked-together "status bar" (there must be a better way, even if a JavaFX Label could have borders we'd be far ahead of the game).

Click the first button and a text file "report" is created as a temporary text file.

Click the second button to build the path to WordPad and shell it to print the temporary file.

Click the third button to remove the temporary file.


In case anyone is curious, it also shows one quick and dirty way to retrieve the value of an environment variable.
 

Attachments

  • sshot.png
    sshot.png
    13.6 KB · Views: 246
  • Print Via WordPad.zip
    2.1 KB · Views: 234
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
If anyone is interested in printing HTML files using native Windows components I have created a small exe that will do this. It has been created using AutoIT which is an excellent freeware tool. It can be found with a Google search.

The attached zip file contains the source and the compiled exe. Place the exe in your Objects folder and run it using the jShell library. See the B4J code below. When distributing your app include the PrintHTML.exe in your app folder.

Best regards

B4X:
Sub menuitemPrint_Action
    Dim js As Shell
    Dim params As List
    params.Initialize
    params.Add("")
    js.Initialize("js", "PrintHTML.exe", params)
    js.WorkingDirectory = File.DirApp
    js.Run(-1) 'run with no timeout
 End Sub
 

Attachments

  • PrintTextHTML.zip
    258.4 KB · Views: 328
Upvote 0

stevel05

Expert
Licensed User
Longtime User
This thread is moving on a pace now, there is lot's to try which I'll do in the morning. Thanks to all for sharing.

@dilettante
even if a JavaFX Label could have borders we'd be far ahead of the game

Add this to the label's style field in Scene Builder :

B4X:
-fx-border-color:blue;-fx-border-width:5%;

More info is available here : http://docs.oracle.com/cd/E17802_01...api/javafx.scene/doc-files/cssref.html#region

It even looks as though you may be able to create something similar to a 9 patch using -fx-border-image
 
Upvote 0

dilettante

Active Member
Licensed User
Longtime User
Add this to the label's style field in Scene Builder :

B4X:
-fx-border-color:blue;-fx-border-width:5%;

Thank you, works fine. Oddly I was sure I couldn't get those to work yesterday, but clearly I entered them incorrectly or otherwise tripped over myself.

Adding a little padding helps too:

B4X:
-fx-border-color: black; -fx-border-width: 1px; -fx-padding: 3px
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Hi everybody, I would like to print a Tableview with some leading information instead of just Table:Table1. like applied filters for instance. Is it possible to eliminate the form feed between two printer.print commands so I can print the text first and then the table without FF?
bste regards
Georg
 
Upvote 0
Top