简体   繁体   中英

passing arguments to implemented method java

I have a class that implements Printable. I'm trying to somehow get values from text fields into the print method but can't seem to work out how to do it.

public class TreePrint extends javax.swing.JFrame implements Printable{

Then this is the action for when user has selected the items to print:

private void btnPrintActionPerformed(java.awt.event.ActionEvent evt)                                         
{
    goPrint();  
}

public void goPrint() 
{
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new TreePrint());
   // boolean doPrint = job.printDialog();
    job.print();
}

@Override  
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {  
    Koks koksSelected = (Koks) combobox_skirne.getSelectedItem();  
    System.out.println(combobox_skirne.getSelectedItem());  
    if (page > 0) {  
        return NO_SUCH_PAGE;  
    }  
    System.out.println(koksSelected.getSkirne());    
    return PAGE_EXISTS;  
}

The problem is that I can't get the selected item or text value in the print method. I can get them in the go print or btnPrintActionPerformed, but in the print() it always shows the selected value that was there when the program was first run. I can post more code if needed, but it is quite long. Im sure this is a basic problem with implemented methods but I couldn't find an answer to it.

This is a similar (the same?) question: Passing Parameters to the print method (JAVA)

If the parameters a available when you create the TreePrint object, add a new constructor with this fields. If not, set the parameters before you print with a public method in TreePrint . Example:

private TreePrint tp;

public void goPrint() 
{
    ...
    this.tp = new TreePrint();
    job.setPrintable(this.tp);
    ...
}

@Override  
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {  
    ...
    this.tp.setXYZParameters(para1, para2, paraN);
    ...
}

TreePrint example:

private Integer para1;
private Integer para2;
private Integer paraN;

public void setXYZParameters(...) {...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM