Last
revision of this document: |
This
step of the tutorial
* reads the content of the TextArea and
*
prints the text out to a printer.
Preface:
There
is a lot of functionality for printing offered in Java (since Version
1.4).
This tutorial shows a very basic method of printing in Java
which will also work with Java Version 1.1 and later.
Credits:
I
took the guideline (in German) for a simple print example from:
http://www.addisonwesley.de/Service/Krueger/kap15004.htm.
JS_Base02f, Step 5 – Run SQL-commands against the database completed – and its prerequisites too.
Basic knowledge of Java programming language; particular the usage of libraries.
Code
for processing after the click onto button 'Print the report':
As
described in steps before, the 'click' leads to a call of the method
protected
static void handleEvent(JS_DBLoad
parmCallingFrame, ActionEvent parmActionEvent)
.
To
react to a click onto button 'Print the report', a method must be
called when the 'ActionCommand' shows a click onto the button,
What
method will be called is implemented with the following line of
code:
public
class JS_DBLoad__Action_Handler
{protected
static void handleEvent(JS_DBLoad parmCallingFrame,
ActionEvent parmActionEvent){
/*
String
cmd = parmActionEvent.getActionCommand().trim();
*
Extract the action-command (in form of a String) that triggered the
Event from the Event */ /*
*
Compare the String with the action-command and call the adjacent
method */
if
(cmd.equals(„btn_Select“))
processSelectFile(parmCallingFrame);
if
(cmd.equals(„btn_Start“))
processDBLoad(parmCallingFrame);
if
(cmd.equals(„btn_Printt“))
processPrint(parmCallingFrame);}
protected
static void
processSelectFile(
{JS_DBLoad
parmCallingFrame)/*
The
implementation of the above code will lead to an error as the method
protected
static void
processSelectFile(
JS_DBLoad
parmCallingFrame)
is not found.
The code for it will be implemented in the
class JS_DBLoad__Action_Handler
.
The development is documented in several steps.
First,
the messages written to the TextArea 'Report' on the GUI are read
and split up into single lines:
protected
static void
}processPrint(
{JS_DBLoad
parmCallingFrame)/*
*
Content of the TextArea on the GUI as a text-stream */
String
strTextAreaContent = parmCallingFrame.get_txt_Report().getText();/*
Position of the 'NewLine' character after the first line within the
text-stream */
int
intNewLinePosition;/*
First line within the stream (Text till the 'NewLine' character.
*
This line will be printed */ String
strLineToBePrinted;
/*
*
Loop to split up the text-stream into single lines;
* Extra
code for the first line - to catch the case that the text-stream is
empty;
* then a while-loop for the following lines */
intNewLinePosition
= strTextAreaContent.indexOf(„\n“);
while
(intNewLinePosition >= 0) {/*
Text before the
} 'NewLine'
character is split-off */
strLineToBePrinted
= strTextAreaContent.substring(0,
intNewLinePosition
);/*
Text after the
'NewLine'
character is kept */
strTextAreaContent
=
strTextAreaContent.substring(intNewLinePosition
+ 1
);//
//
Code for printing is inserted here;
// temporarily the single
line is displayed on the
GUI parmCallingFrame.get_txt_Report().append(„
//Single
Line split off
: „ + strLineToBePrinted + „\n“);
/*
Position of the next
*/ 'NewLine'
character
intNewLinePosition
= strTextAreaContent.indexOf(„\n“);
To
see if the code was typed error-free it is a good time now to see an
intermediate result.
Please remind to save the code just typed
before running the application by selecting
>Run>Run
As>Java Application.
Do not forget to 'Select Input
File' and select the (till now empty) file with the SQL-commands -
otherwise the XML-file with the parameters will not be loaded
!
Click the button'Start
SQL execution' thereafter.
To save some space the screenshot is
skipped.
Now,
the code for selecting a printer and printing the Lines is
implemented.
protected
static void
}processPrint(
{JS_DBLoad
parmCallingFrame)/*
/*
End
*
Content of the TextArea on the GUI as a text-stream */
String
strTextAreaContent = parmCallingFrame.get_txt_Report().getText();/*
Position of the 'NewLine' character after the first line within the
text-stream */
int
intNewLinePosition;/*
First line within the stream (Text till the 'NewLine' character.
*
This line will be printed */ String
strLineToBePrinted;
/*
*
Variables used for printing the text */
/* Page- and Line-Counter
to start a new page and print the page-number on top of the sheet /
/*
Printer-specific variables */int
intPageCount = 1;
int
intLineCount = 1; Frame
f =
/*new
Frame();
PrintJob pJob =
f.getToolkit().getPrintJob(f, „JS_DBLoad
- Printer Selection“
,
null
);
Graphics
g = pJob.getGraphics();/*
*
Print the header with the page-number
*/ g.setColor(
Color.black
);
g.setFont(new
Font(“Arial“
,
Font.PLAIN
, 12));
g.drawString(“page
- “
+
Integer.toString(intPageCount) + “
-“,
300, 200
);
*
Loop to split up the text-stream into single lines;
* Extra
code for the first line - to catch the case that the text-stream is
empty;
* then a while-loop for the following lines */
intNewLinePosition
= strTextAreaContent.indexOf(„\n“);
while
(intNewLinePosition >= 0) {/*
Text before the
} 'NewLine'
character is split-off */
strLineToBePrinted
= strTextAreaContent.substring(0,
intNewLinePosition
);/*
Text after the
'NewLine'
character is kept /
strTextAreaContent
=
strTextAreaContent.substring(intNewLinePosition
+ 1
);/*
Print the single line in another font; vertical position calculated
by the LineCount
*/
g.setColor(
Color.black
);
g.setFont(
new
Font(“Courier“
,
Font.PLAIN
,
9));
g.drawString(strLineToBePrinted,
20, 30 + intLineCount*12
);
/*
Increase
the LineCount
*/
/*
Position of the next
intLineCount++;
*/ 'NewLine'
character
intNewLinePosition
= strTextAreaContent.indexOf(„\n“);
the
printing - this starts the printing on paper
*/
g.dispose();
pJob.end();
To
see the effect of the implementation run the program.
Please
remind to save the code just typed before running the application
again by selecting
>Run>Run
As>Java Application.
Do not forget to 'Select Input
File' and select the (till now empty) file with the SQL-commands -
otherwise the XML-file with the parameters will not be loaded
!
Click the button'Start
SQL execution' thereafter to fill the TextArea with content.
After
clicking the button 'Print the Report' the printer-dialog shows
up:
Among
others, this dialog allows the selection of a printer - if there are
more than one installed.
As
the SQL-commands in the input-file were only a few till now, the
output fitted to one page.
To be prepared for a longer list, the
printing of more than one page is implemented now.
protected
static void
}processPrint(
{JS_DBLoad
parmCallingFrame)/*
/*
End
*
Content of the TextArea on the GUI as a text-stream */
String
strTextAreaContent = parmCallingFrame.get_txt_Report().getText();/*
Position of the 'NewLine' character after the first line within the
text-stream */
intNewLinePosition;
int
/*
First line within the stream (Text till the 'NewLine' character.
*
This line will be printed */ String
strLineToBePrinted;
/*
*
Variables used for printing the text */
/* Page- and Line-Counter
to start a new page and print the page-number on top of the sheet /
/*
Printer-specific variables */int
intPageCount
= 1;
int
intLineCount
= 1; Frame
f =
/*new
Frame();
PrintJob pJob =
f.getToolkit().getPrintJob(f, „JS_DBLoad
- Printer Selection“
,
null
);
Graphics
g = pJob.getGraphics();/*
*
Print the header with the page-number
*/ g.setColor(
Color.black
);
g.setFont(new
Font(“Arial“
,
Font.PLAIN
,
12));
g.drawString(“page
- “
+ Integer.toString(intPageCount) + “
-“,
300, 200
);
*
Loop to split up the text-stream into single lines;
* Extra
code for the first line - to catch the case that the text-stream is
empty;
* then a while-loop for the following lines */
intNewLinePosition
= strTextAreaContent.indexOf(„\n“);
while
(intNewLinePosition
>= 0) {/*
Text before the
}
'NewLine'
character
is split-off */
strLineToBePrinted
=
strTextAreaContent.substring(0, intNewLinePosition
);/*
Text after the
'NewLine'
character
is kept /
strTextAreaContent
=
strTextAreaContent.substring(intNewLinePosition
+ 1
);/*
Print the single line in another font; vertical position calculated
by the LineCount
*/
g.setColor(
/*
Color.black
);
g.setFont(new
Font(“Courier“
,
Font.PLAIN
,
9));
g.drawString(strLineToBePrinted,
20, 30 + intLineCount*12
);
/*
Increase
the LineCount
*/
intLineCount++;
See
if the lines overflow page and create new page if necessary
*/
if
(intLineCount > 50) {
}intLineCount
= 1;
intPageCount++;
/*
Dispose
the graphic and open a new one; this causes a new page in the
PrintJob
*/
g.dispose()
;
g
= pJob.getGraphics()
;
/*
Print
the headline with the page-number (in another font)
*/ g.setColor(
Color.black
);
g.setFont(new
Font(“Arial“
,
Font.PLAIN
, 12));
g.drawString(“page
- “
+
Integer.toString(intPageCount) + “
-“,
300, 200
);
/*
Position of the next
*/ 'NewLine'
character
intNewLinePosition
= strTextAreaContent.indexOf(„\n“);
the
printing - this starts the printing on paper
*/
g.dispose();
pJob.end();
top.
Print
the output in different colors:
As
this step did not grew pretty long, there is some nice little extra
implemented:
The printed text is colored depending on the kind of
information.
The
lines to be printed are inspected of the occurence of defined
substrings and the color is selected according to the
result.
protected
static void
}processPrint(
{JS_DBLoad
parmCallingFrame)/*
/*
End
*
Content of the TextArea on the GUI as a text-stream */
String
strTextAreaContent = parmCallingFrame.get_txt_Report().getText();/*
Position of the 'NewLine' character after the first line within the
text-stream */
intNewLinePosition;
int
/*
First line within the stream (Text till the 'NewLine' character.
*
This line will be printed */ String
strLineToBePrinted;
/*
*
Variables used for printing the text */
/* Page- and Line-Counter
to start a new page and print the page-number on top of the sheet /
/*
Printer-specific variables */int
intPageCount
= 1;
int
intLineCount
= 1; Frame
f =
/*new
Frame();
PrintJob pJob =
f.getToolkit().getPrintJob(f, „JS_DBLoad
- Printer Selection“
,
null
);
Graphics
g = pJob.getGraphics();/*
*
Print the header with the page-number
*/ g.setColor(
Color.black
);
g.setFont(new
Font(“Arial“
,
Font.PLAIN
,
12));
g.drawString(“page
- “
+ Integer.toString(intPageCount) + “
-“,
300, 200
);
*
Loop to split up the text-stream into single lines;
* Extra
code for the first line - to catch the case that the text-stream is
empty;
* then a while-loop for the following lines */
intNewLinePosition
= strTextAreaContent.indexOf(„\n“);
while
(intNewLinePosition
>= 0) {/*
Text before the
}
'NewLine'
character
is split-off */
strLineToBePrinted
=
strTextAreaContent.substring(0, intNewLinePosition
);/*
Text after the
'NewLine'
character
is kept /
strTextAreaContent
=
strTextAreaContent.substring(intNewLinePosition
+ 1
);/*
Print the single line in another font; vertical position calculated
by the LineCount */
/*
Standard color */
g.setColor(
/*
Color.black
);
/*
color for the SQL-commands read from the input-file */
g.setFont(
if
(strLineToBePrinted.compareTo(
g.setColor(“input-line:“
)
>= 0) Color.blue
);
/*
color if the SQL-command was executed */
if
(strLineToBePrinted.compareTo(
g.setColor(“SQL-command
executed“
) >= 0)
Color.green
);
/*
color if the SQL-command was in error */
if
(strLineToBePrinted.compareTo(
g.setColor(“
)
>= 0) SQL-command
error
“Color.red
);
new
Font(“Courier“
,
Font.PLAIN
,
9));
g.drawString(strLineToBePrinted,
20, 30 + intLineCount*12
);
/*
Increase
the LineCount
*/
intLineCount++;
See
if the lines overflow page and create new page if necessary
*/
if
(intLineCount > 50) {
}intLineCount
= 1;
intPageCount++;
/*
Dispose
the graphic and open a new one; this causes a new page in the
PrintJob
*/ g.dispose()
;
g
= pJob.getGraphics()
;
/*
Print
the headline with the page-number (in another font)
*/ g.setColor(
Color.black
);
g.setFont(new
Font(“Arial“
,
Font.PLAIN
,
12));
g.drawString(“page
- “
+ Integer.toString(intPageCount) + “
-“,
300, 200
);
/*
Position of the next
*/ 'NewLine'
character
intNewLinePosition
= strTextAreaContent.indexOf(„\n“);
the
printing - this starts the printing on paper
*/
g.dispose();
pJob.end();
To
see if the code was typed completely error-free, see the final
result.
Please remind to save the code just typed before running
the application by selecting
>Run>Run
As>Java Application.
Do not forget to 'Select Input
File' and select the (till now empty) file with the SQL-commands -
otherwise the XML-file with the parameters will not be loaded
!
Click the button'Start
SQL execution' thereafter ans also print out the result.
To save
some space the screenshot is skipped.
top.
Next
Step:
Pack
the project into a JAR (JAva-Archive)-file and run the application
out of this file.
JS_Base02h,
Step 7 - Pack the project into a JAR-file and run it.