I am printing a Jpanel and its working fine, but now I need the print dialog box, which always appear when the print button is clicked. It suppose to go like, when user press print button the default printer should start printing the job with out coming up with a print dialog box. Here is the code, I am using,
Paper paper = new Paper();
paper.setImageableArea(0, 0, 153, 243);
paper.setSize(243, 154);
PageFormat format = new PageFormat();
format.setPaper(paper);
format.setOrientation(PageFormat.LANDSCAPE);
printjob.setPrintable(printable, format);
if (printjob.printDialog() == false)
return;
try {
printjob.print();
} catch (PrinterException ex) {
System.out.println("NO PAGE FOUND." + ex);
}
Thanks.
did you call setPrintService() on printjob?
Or try using printjob.getPrinterJob() to get an instance associated with the default printer.
Related
This question might have been asked here for couple of times, but I found some of them unsolved Retrieving image from mysql column to jlabel or to jtable.
Desired Output:
if I click any specific row containing image then it should display the image inside the JLabel, but if the row does not contain image then the label should display "No Photo Available".
Below is the lines of codes which I'm currently using to display the image from JTable to the jlabelPhoto.
if(EmpDBTable.getValueAt(getData, 12) != null){
try {
byte[] byteArray = (byte[]) EmpDBTable.getValueAt(getData, 12);
ByteArrayInputStream bais = newByteArrayInputStream(byteArray);
BufferedImage bImg = ImageIO.read(bais);
ImageIcon icon = new ImageIcon(bImg.getScaledInstance(jLabelPhoto.getWidth(), jLabelPhoto.getHeight(), Image.SCALE_SMOOTH));
jLabelPhoto.setIcon(icon);
bais.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}else{
jLabelPhoto.setText("No Photo Available");
}
This code allows to me display image into the label properly with scale, but the problem is if I click the row that has no image in it then the previous image still appearing on the label.
Here is the attached link of the video for proper illustration. https://vimeo.com/user101485383/review/351884404/57a10f86af
Your problem could possibly because you forgot to set the JLabel's Icon to null in the else block:
} else {
jLabelPhoto.setText("No Photo Available");
jLabelPhoto.setIcon(null); // need to add this
}
If this doesn't fix the problem then you will likely need to debug the if block condition:
if(EmpDBTable.getValueAt(getData, 12) != null) {
Perhaps the data held is not an image, but also not null, but since you hold all the data and the code, it will be up to you to debug this if need be.
I want to use print product information on label printer. I have Zebra TLP 2844 This printer support EPL2 programming language. When I want to print non-ASCII character printer just print "?" instead of the correct character. What I did?
First I created a simple text file with the below content
əğüçşıö
and press CTRL+P to print. Output is ok printer print all characters normally.
But when I use EPL2 commands like below
N
A220,120,0,4,1,1,N,"əğçşıüö"
P1
My printer gives the below output
??ç??üö
How can I solve this problem? I use java print api to send command to my printer.
And this my code
PrinterJob pj = PrinterJob.createPrinterJob();
Doc doc = new SimpleDoc(sb.toString().getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
boolean result = false;
javax.print.PrintService printService = PrintServiceLookup.lookupPrintServices(null, null)[0];
try {
printService.createPrintJob().print(doc, null);
} catch (PrintException e) {
e.printStackTrace();
}
PrinterJob pj = PrinterJob.createPrinterJob();
Doc doc = new SimpleDoc(sb.toString().getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
boolean result = false;
javax.print.PrintService printService = PrintServiceLookup.lookupPrintServices(null, null)[0];
try {
printService.createPrintJob().print(doc, null);
} catch (PrintException e) {
e.printStackTrace();
}
Its not an issue with the code it's an issue with the printer. Most printers don't include those characters in their standard font set. You either need to use the character's hex to call them or, if they're not included on the font set on the printer, download a different font.
I am trying to send some text to a printer. I need just the text printed, wrapped at the page margin and flowing to another page if necessary.
Here is a minimal example of what I am doing now:
#FXML
private void print() {
TextArea printArea = new TextArea(textArea.getText());
printArea.setWrapText(true);
printArea.getChildrenUnmodifiable().forEach(node -> node.setStyle("-fx-background-color: transparent"));
printArea.setStyle("-fx-background-color: transparent");
PrinterJob printerJob = PrinterJob.createPrinterJob();
if (printerJob != null && printerJob.showPrintDialog(textArea.getScene().getWindow())) {
if (printerJob.printPage(printArea)) {
printerJob.endJob();
// done printing
} else {
// failed to print
}
} else {
// failed to get printer job or failed to show print dialog
}
}
What ends up printing is a gray background that seems to be the control itself, along with the scrollbar. Am I approaching this the wrong way? I feel like I'm fighting against the API by tweaking and printing a control instead of just sending the text to be printed.
The example image below was taken from my cell phone camera, so the white paper ends up looking a bit light-gray, but you can still see the gray background from the control and the scrollbar.
Instead of a TextArea, print a TextFlow:
private void print() {
TextFlow printArea = new TextFlow(new Text(textArea.getText()));
PrinterJob printerJob = PrinterJob.createPrinterJob();
if (printerJob != null && printerJob.showPrintDialog(textArea.getScene().getWindow())) {
PageLayout pageLayout = printerJob.getJobSettings().getPageLayout();
printArea.setMaxWidth(pageLayout.getPrintableWidth());
if (printerJob.printPage(printArea)) {
printerJob.endJob();
// done printing
} else {
System.out.println("Failed to print");
}
} else {
System.out.println("Canceled");
}
}
Notice that the TextFlow's maxWidth needs to be set using the PrinterJob's page layout, after the print dialog has been shown.
We are trying to print through java on dot matrix printer. We have created the Paper object with custom size. But when it goes to printer, it doesn't take the custom size. It takes either 11 inches or 12 inch size. Below is the code which we are using. Please suggest the solution.
PrinterJob job = PrinterJob.getPrinterJob(); // Get a PrinterJob.
PageFormat format = job.defaultPage(); // Get the default page format, then ask the user to customize it.
Paper paper = format.getPaper(); // Note: Custom size of paper should be supported by attach Printer.
paper.setSize((PaperWidth*72),
(PaperHeight*72)); // Set Custom size of the Paper.
paper.setImageableArea(MarginLeft*72, MarginTop*72,
paper.getWidth() - MarginRight*72 - MarginLeft*72,
paper.getHeight()- MarginBottom*72 - MarginTop*72);
System.out.println(paper.getHeight());
format.setPaper(paper); // Set the paper.
PageFormat pf = job.validatePage(format);
Book bk = new Book(); // Set up a book, with exact no. of pages to be printable.
bk.append(new TestClass(), pf, numPages);
job.setPageable(bk); // Pass the book to the PrinterJob
////// OR set printable without book.
//// job.setPrintable(new TestClass(),format);
if (job.printDialog()) // Put up the dialog box
{
try
{
job.print(); // Print the job if the user didn't cancel printing.
}
catch (PrinterException ex)
{
}
How can I print duplex in java!?
Here's my code, but it didn't work - it only works in microsoft word (so the printer can do it)
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(2));
pras.add(Sides.DUPLEX);
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.defaultPage();
Paper temp = pf.getPaper();
temp.setImageableArea(1, 3, temp.getWidth(), temp.getHeight());
pf.setPaper(temp);
if (pj.printDialog(pras)) {
try {
pj.setPrintable(this, pf);
pj.print(pras); // Drucken
return true;
} catch (Exception PrintException) {
.....
}
I tried to give only the attributes to print, only to the dialog, etc. etc. etc.
It print's the document 2 times, but not duplex! (also tried Sides.TWO_SIDED_LONG_EDGE..)
The Problem was my printer,... it always turn the option for duplex to be disabled.