how to write print function for a frame

hello!

how to write print function for a frame to print contains in that frame....my frame have 3 list box, a canvas fucntion.....i would like to print it all out by clicking "print" function....

thnx for reply......*^^*
bhuey_lingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jerchCommented:

PrintJob pjob = yourFrame.getToolkit().getPrintJob(yourFrame, "Printing Test", null);

if (pjob != null) {
 Graphics pg = pjob.getGraphics();
 if (pg != null) {
  yourFrame.printAll(pg);  // <-- use printall
  pg.dispose(); // flush page
}
pjob.end(); }


printAll draws all its components in Graphics pg.

Jerson
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bhuey_lingAuthor Commented:
hello.....

i find few error

70:can't make static reference to method java.awt.toolkit.getToolkit() in class java.awt.window.

70:undentify variable: Graphframe1

75:can't make static reference to method void printAll(java.awt.Graphics) in class java.awt.component

thnx for reply

hlb
0
jerchCommented:
Can you show me some lines in your code?




0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

bhuey_lingAuthor Commented:
i'm sorry.......i forget to attach my program.........

here r my program( i may use not the latest code to write like for the awt)


import java.awt.*;                                                                                                                                                 import java.awt.*;
import java.applet.*;


public class GraphFrame1 extends Frame{
  double[] x,y;
  int indexes;
  boolean flags;
  List listGain;
  List listIp;
  List listOp;
  Graphics g;
  GraphDisplay graph;      
      
  public GraphFrame1(String title, boolean isapp){
    super(title);
    setBackground(Color.lightGray);
    init();
  }
      
  public void start(double[] inputX, double[] outputY, int index, boolean flag){
    x=inputX;
    y=outputY;
    indexes=index;
    flags=flag;
    init();            
    graph.repaint();
  }
            
  public void init(){
    MenuBar mBar = new MenuBar();
    this.setMenuBar(mBar);
   
    Menu file = new Menu("File");
    file.add(new MenuItem("Print..."));
    file.addSeparator();
    file.add(new MenuItem("Quit"));
    mBar.add(file);
    Menu help = new Menu("Help");
    help.add(new MenuItem("About"));
    mBar.add(help);
          
    listIp=new List(25,true);
    listOp=new List(25,true);
    listGain=new List(25,true);
    Panel p = new Panel();
    p.setLayout(new FlowLayout());
    p.add(listIp);
    p.add(listOp);
    p.add(listGain);
    listIp.addItem("Input(V):");
    listOp.addItem("Output(V):");
    listGain.addItem("Gain(Ip/Op)[dB]:");
    add();
                        
    graph = new GraphDisplay(g,x,y,indexes,flags);
    graph.setBackground(Color.black);
    setLayout(new BorderLayout());
   
    add("Center",graph);
    add("East", p);
    graph.repaint();
  }

  public boolean handleEvent(Event event){
    switch(event.id){
      case Event.ACTION_EVENT:
      if (event.target instanceof MenuItem){
            if(((String)event.arg).equals("Print...")){
              PrintJob pjob = GraphFrame1.getToolkit().getPrintJob(GraphFrame1, "Printing Test", null);

          if (pjob != null) {
            Graphics pg = pjob.getGraphics();
            if (pg != null) {
              GraphFrame1.printAll(pg);  // <-- use printall
              pg.dispose(); // flush page
            }
            pjob.end();
          }
        }
            if(((String)event.arg).equals("Quit")){
              hide();
          removeAll();
          dispose();
        }
            
            else if (((String)event.arg).equals("About")){
              InfoDialog d;
              d=new InfoDialog(this, "About Virtual LAb",
                    "This demo was written by hlBoey\n"+
                    "Copyright(c) 2000 UTM");
              d.show();
        }
      }
      break;
     
      case Event.WINDOW_DESTROY:
        hide();
        removeAll();
        dispose();
      break;
    }  
     return true;  
  }
        
  public void add(){
    double[] gain = new double[40];
    double[] dB=new double[40];
    int[] gain1 = new int[40];
    for(int i=0; i<indexes; i++){
      if(y[i]==0.)    listGain.addItem("      -");
      else{
        gain[i]=(double)((int)(1000.*y[i]/x[i])/1000.);
        dB[i]=(double)((int)(1000.*
         20*Math.log(gain[i])/Math.log(10))/1000.);
        listGain.addItem(""+dB[i]);
      }
      listIp.addItem(""+x[i]);
      listOp.addItem(""+y[i]);
    }
  }
}
0
jerchCommented:
Change GraphFrame1 to this in line 70 and 75.

line 70:
this.getToolkit().getPrintJob(GraphFrame1, "Printing Test", null);

line 75:
this.printAll(pg);

Hope this works...

You can't use Class name to access an instance method.
Jerson
0
bhuey_lingAuthor Commented:
hello!

i still got few problem.....there is no compile error but runtime error....

following r some of the error..

at java.security.AccessControlContext.checkPermission(Compiled Code)

java.lang.securityManager.checkPrintJobAccess(securityManager.java:1290)

sun.awt.windows.WTookit.getPrintJob(Wtookit.java:)

graphFrame1.handleEvent

java.awt.window.postEvent(Compiled Code)

and so on.....

thanks for reply.....
0
jerchCommented:
If you're developing an applet, you wouldn't be able to print. You need to have your applet signed (See www.verisign.com for more details).  And if you're developing an application, it suppose to work. I don't think there's a security restriction for this.  But for a work
around, try this. Maybe it will work.

These are the steps:

Define a policy file which contains:

grant {
permission java.security.AllPermission;
};

and save it as mypolicy

and run your program like this

java -Djava.security.policy=mypolicy YourProgram

or you can add these line in your main() method.

Properties props = System.getProperties();

props.setProperty("java.security.policy", "mypolicy");

System.setProperties(props);

Don't forget to import java.util.*

Hope this helps.

Jerson
0
bhuey_lingAuthor Commented:
hello..

my programme r an apllet..is that means i can use those latest code u give ...r u mean that then i have no other choice but have applet signned.....is it cost money ....actually wat applet signned means...is it....after signned it.....any user can use print function of the applet i develope or every user have to sign the applet in their own PC?

thnx for reply....
0
jerchCommented:
If you get your applet signed, some security restrictions will be removed such as reading the local file of the user, printing in the user's machine etc.  Yes, you have to pay certain amount for your applet to get signed.

check www.verisign.com

Jerson
0
bhuey_lingAuthor Commented:
thnx......
hlb
0
jerchCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.