How to change printed JTable font size? - java

Basically, I use JTable's method print(JTable.PrintMode.FIT_WIDTH, null, null, true, null, true, null); to print this table. However, on some computers this printed table on A4 sheet is quite small, so I thought I need to set its fonts larger, but I don't know how to do that.
In order to make table font larger, I created a custom CustomPrintable class: class CustomPrintable implements Printable { and then in its method print:
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
...
Graphics gCopy = graphics.create();
gCopy.setFont(gCopy.getFont().deriveFont(20f)); //make font larger, but no success
int retVal = delegate.print(gCopy, format, pageIndex);
...
}
This approach did not help and printed table is still to small. Can you help me with this? How can I change font of the printed table?

Related

jTable not coloring row correctly

i have a problem, basically my program looks like this:
the thing is, it works, it is supposed to color the rows that have "N" in green, but the first time it loads the values, as you can see, the first row have bits of white, but for some reason if i click the list it fixes the issue, i need the rows to be colored properly without the user needing to click on the list to fix the issue, this is my Render code:
public class Render extends JTable {
#Override
public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
int columnIndex){
Component componente = super.prepareRenderer(renderer, rowIndex, columnIndex);
String val = getValueAt(rowIndex, columnIndex).toString();
if(val.equals("N")){
componente.setBackground(Color.GREEN);
}
return componente;
}
}
I figured i could use Repaint(); in the MouseMoved event in the JTable, but i think is not a proper way to fix it... Any help is appreciated, cheers!
Start by changing
String val = getValueAt(rowIndex, columnIndex).toString();
to
String val = getValueAt(rowIndex, 3).toString();
This will ensure that no matter what column the cell represents, you are checking the appropriate columns value - this is why the leading cells are white
You should also consider providing a default color for when the column values doesn't match
if (val.equals("N")) {
componente.setBackground(Color.GREEN);
} else {
componente.setBackground(getBackground());
}

Issues with passing arguments to the print function in Java

public int print(Graphics g, PageFormat pf, int page, String customer_name)
throws PrinterException {
System.out.println("The value of customer name:"+customer_name);
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
String x = layout.get("");
System.out.println("The value of x is\n"+x);
/* Now we perform our rendering */
g.drawString("Customer Name: "+customer_names, 100, 100);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
I want to call this method from another class, while passing an additional argument to it, customer_name. I call this method from another class as follow:
Printer print = new Printer(); //making an object to access that class Printer.java
PageFormat page = job.defaultPage();
print.print(<I have no idea what to put here for graphics>, page, 5, customer_name_field.getText());
When I call the method print.print, I gives the message that it requires Graphics, PageFormat, int, String. But what should I put for Graphics, I have no idea?
It's not working because it looks like you're going about it wrong:
Your print method has an extra String parameter tacked to the end which prevents it from being a true method override for a class that implements Printable.
You're trying to call your print method directly scrounging around for a Graphics context when you shouldn't consider doing this.
If you just want to print some text then you need to follow the first sections of the Printing Tutorial. Your print method above does not conform with a Printable's print(...) method override. Please do yourself a favor and follow the tutorial. I've given you the link.
Consider creating a class that implements Printable, passing your String as a single parameter to the class's constructor, and use this to set an instance field. The print(...) method should match that found in the tutorial, should have an #Override annotation, and most important will never be called directly by you. Your PrinterJob instance will do the printing behind the scenes.
Note, that if your goal is to print a Swing GUI, then the steps are different, since Swing GUI's carry much of the innate machinery for printing within them.
Depends on the class you're in, there's a high chance that the UI framework element you are currently using already provides you with the getGraphics() function.
You might even want to override the paint() function of the class you're using, and call this method you have written from that. It depends on your use-case.
This is my github project path where i did the code for same. https://github.com/knikam/Mobile_shop_management/blob/master/src/mobile_shop_mangment/Sell_mobile.java
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
String x = layout.get("");
System.out.println("The value of x is\n"+x);
/* Now we perform our rendering */
String customer_namescustomer_names.getText();**This is work for me**
g.drawString("Customer Name: "+customer_names, 100, 100);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}

Printing multiple JTables as one job -- Book object only prints 1st table

For a number of reasons, I'm trying to combine the output of multiple JTables into a single print job. After tearing my hair out trying to build PDFs, and combing the Java API, I settled on the Book class. My printing code currently looks like this.
try {
PrinterJob printer = PrinterJob.getPrinterJob();
//Set 1/2 " margins and orientation
PageFormat pf = printer.defaultPage();
pf.setOrientation(PageFormat.LANDSCAPE);
Paper paper = new Paper();
double margin = 36; // half inch
paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
pf.setPaper(paper);
Book printJob = new Book();
// Note for next line: getAllTables() returns an ArrayList of JTables
for (JTable t : getAllTables() )
printJob.append(t.getPrintable(PrintMode.FIT_WIDTH, null, null), pf,2);
printer.setPageable(printJob);
System.out.println(printJob.getNumberOfPages());
if (printer.printDialog())
printer.print();
} catch (PrinterException e) {
e.printStackTrace();
}
Primary Problem: Only the output from the "first" table is printing. I've made sure the for loop iterates correctly, and debugging statements have shown that every table is being added to the Book. Changing order of tables has no effect. Changing the print mode to PrintMode.NORMAL, does appear result in pieces of the other tables being printed. However, I run into a slew horizontal pagination problems, as the table width frequently exceeds page width (and it still doesn't explain why PrintMode.FIT_WIDTH isn't working)
A secondary question: How can I detect the correct number of pages in each printable? Most of my tables are two pages long, so for the moment, I'm just adding 2 pages each time I append. I read "somewhere" that using Book.UNKNOWN_NUMBER_OF_PAGES as the page number will fix this problem, but that only leads to an IndexOutOfBounds exception in the API's code. I've considered calling print myself until I get NO_PAGE_EXISTS, but I'd need a Graphics object with the proper page dimensions (and I have no idea how to get that).
Lastly: If the Book approach is hopeless, how else can I combine the output of multiple JTables (ie. multiple printables) into a single job? I looked into exporting the table as a PDF, but JTable's built-in pagination is so nice, I'd rather not have to do it myself. My last resort is to just give up and use iText's built in table function to construct a copy of the table.
Edit 3: Per my comment below, I got this working by generating a printable, determining the # of pages, and then generating a new one. I also modified Durendal's wrapper to spare us the hassle of iterating over each page. The code from the wrapper is
class PrintableWrapper implements Printable
{
private Printable delegate;
private int offset;
public PrintableWrapper(Printable delegate, int offset) {
this.offset = offset;
this.delegate = delegate;
}
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
return delegate.print(graphics, pageFormat, pageIndex-offset);
}
}
I put Durendal's code for determining number of pages in its own function
public int getNumberOfPages(Printable delegate, PageFormat pageFormat) throws PrinterException
{
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
int result = delegate.print(g, pageFormat, numPages);
if (result == Printable.PAGE_EXISTS) {
++numPages;
} else {
break;
}
}
return numPages;
}
After I create the book object, my printing code looks like this
int totalPages = 0;
for (DragNDropTable t : getAllTables() )
{
int pages = getNumberOfPages(t.getPrintable(PrintMode.FIT_WIDTH, null, null), pf);
Printable p = t.getPrintable(PrintMode.FIT_WIDTH, null, null);
printJob.append(new PrintableWrapper(p,totalPages), pf, pages);
totalPages += pages;
}
printer.setPageable(printJob);
if (printer.printDialog())
printer.print();
And it works like a charm!
Edit 2: (you can skip this) I tried Durendal's answer. While I'm printing enough pages, multipage printables are printing the last page multiple times (once for every page in the printable). This is the same problem I discussed in my 1st edit (below), and I have no idea why this is happening, and my debugging statements are saying that it's printing all of the pages correctly, but the last page of a multipage printable is printed in place of each page. Code is attached. Insight is appreciated (and repayed with virtual cookies)
try {
PrinterJob printer = PrinterJob.getPrinterJob();
//Set 1/2 " margins and orientation
PageFormat pf = printer.defaultPage();
pf.setOrientation(PageFormat.LANDSCAPE);
Paper paper = new Paper();
double margin = 36; // half inch
paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
pf.setPaper(paper);
Book printJob = new Book();
// Note for next line: getAllTables() returns an ArrayList of JTables
for (JTable t : getAllTables() )
{
Printable p = t.getPrintable(PrintMode.FIT_WIDTH, null, null);
int pages = getNumberOfPages(p, pf);
for (int i=0; i < pages; i++)
printJob.append(new PageWrapper(p,i), pf);
}
printer.setPageable(printJob);
System.out.println(printJob.getNumberOfPages());
if (printer.printDialog())
printer.print();
} catch (PrinterException e) {
e.printStackTrace();
}
public int getNumberOfPages(PageFormat pageFormat) throws PrinterException
{
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
int result = delegate.print(g, pageFormat, numPages);
if (result == Printable.PAGE_EXISTS)
++numPages;
else
break;
}
return numPages;
}
I'm using the unmodified PageWrapper that Durendal gave below.
Edit 1: (You can skip this) Dovetailing off of Durendal's answer, I tried to make a wrapper that spares us the chore of iterating over the pages ourselves. Unfortunately, it doesn't seem to work correctly on multipage printables, printing the same page multiple times in the document. I post it, simply because someone may get it to work, and it's slightly more convenient to use.
class PrintableWrapper implements Printable
{
private Printable delegate;
private int offset;
public PrintableWrapper(Printable delegate, int offset) {
this.offset = offset;
this.delegate = delegate;
}
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
return delegate.print(graphics, pageFormat, pageIndex-offset);
}
public int getNumberOfPages(PageFormat pageFormat) throws PrinterException
{
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
int result = delegate.print(g, pageFormat, numPages);
if (result == Printable.PAGE_EXISTS)
++numPages;
else
break;
}
return numPages;
}
}
My printing code now looks like this (after I set the page format)
Book printJob = new Book();
int totalPages = 0;
for (DragNDropTable t : getAllTables() )
{
Printable p = t.getPrintable(PrintMode.FIT_WIDTH, null, null);
PrintableWrapper pw = new PrintableWrapper(p, totalPages);
totalPages += pw.getNumberOfPages(pf);
printJob.append(pw, pf,pw.getNumberOfPages(pf));
}
printer.setPageable(printJob);
if (printer.printDialog())
{
printer.print();
}
I had the very same Problem recently. The Book class by itself is useless, because if you add Printables to it, when the Book is printed it will pass the pageIndex from the Book to the Printable at pageIndex.
That is in most cases not what you want.
Create a simple Printable Wrapper that can remember a pageIndex to be used for the Printable delegate and add those to the Book:
class PageWrapper implements Printable {
private Printable delegate;
private int localPageIndex;
public PageWrapper(Printable delegate, int pageIndex) {
this.localPageIndex = pageIndex;
this.delegate = delegate;
}
#Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
return delegate.print(graphics, pageFormat, localPageIndex);
}
}
Now you need to iterate trough each page of each Printable/Pageable and add a wrapper instance (that knows the pageIndex into its delegate) to the Book. That solves the problem that Book passes the wrong pageIndex to the printables added to it (its easier for Pageables than Printables).
You can detect the number of Pages in a Printable by printing it ;) Yes, I'm serious:
Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).createGraphics();
int numPages = 0;
while (true) {
int result = printable.print(g, pageFormat, numPages);
if (result == Printable.PAGE_EXISTS) {
++numPages;
} else {
break;
}
}
Its important you obtain your PageFormat instance from the actual PrinterJob you want to print to, because the number of pages depends on the page format (paper size).

how to print contents of a jpanel as text?

I'm using netbeans and I have the code to print a jpanel and it works fine. However, its output is that it prints the jpanel as an image. This is not ideal for me as it also prints a faded gray color in the background which is the size of the jpanel. I only want to print the text in the jpanel which consists of many jlabels. Is there a way to print the contents of a jpanel as text and not an image?
This is the code that I used to print the contents of my jpanel
PrinterJob job = PrinterJob.getPrinterJob();
job.setJobName("jPanel13");
job.setPrintable (new Printable() {
public int print(Graphics pg, PageFormat pf, int pageNum){
if (pageNum > 0){
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D) pg;
g2.translate(pf.getImageableX(), pf.getImageableY());
jPanel13.paint(g2);
return Printable.PAGE_EXISTS;
}
});
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
}
This is a printscreen of the jpanel that I need to print. In my program, the text values of the jlabels can be changed by the user. The jlabels texts will then be printed to fill in the blank lines a form. The form will already have writing on it; it is only the blank lines that I will fill-up when printing. That is why it is important that the jlabels are positioned in that way.
http://i.stack.imgur.com/kPKWe.jpg
If the JPanel background is the only problem, you can call print() on it instead of paint(), and override printComponent() to do nothing (the default just calls paintComponent()).
Alternately, just set the background color to white while printing.

Show a tooltip above a cell in a JTable

I need to show a tooltip above (or below :) a cell when the user enter a wrong value in it (see the image below).
I have a tooltip, but I need a Point to display it at the right position, so I want to get a cell position. Do you know how to get this?
BUT, if you have a better solution to realize this behaviour, I'm open to all proposition (especially for the fact that the tooltip is not bind with the cell/Jtable/Panel and if I move/close/minmize my window the tooltip is display at the same position)
Thanks,
Damien
Please refer below code snippet, and you'll get the solution
JTable table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (c instanceof JComponent) {
JComponent jc = (JComponent) c;
jc.setToolTipText(getValueAt(row, column).toString());
}
return c;
}
};
If you want to only show the specific cell, all you have to do is change the column param in the params of getValueAt(...) method to a specific column which contains that cell
You have an example of such feature in the Swing components visual guide.
Edit: In fact, it is not really a tooltip that you need here, as the tooltip need to have the cursor positionned over the cell. You want to display the tooltip even if the cursor is outside the cell, right?
Anyway, an alternative solution is to change the background of the cell when the value entered by the user is invalid (in orange or red for example), and then add a "real" tooltip (using the link I provided) in order to give the user a complete error message.
Just use below code while creation of JTable object.
JTable auditTable = new JTable(){
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);
try {
//comment row, exclude heading
if(rowIndex != 0){
tip = getValueAt(rowIndex, colIndex).toString();
}
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}
return tip;
}
};
Try getCellRect
Use the following code to get the value for the correct row if using RowSorter:
jc.setToolTipText(getValueAt(convertRowIndexToModel(row), column).toString());

Categories