?
Solved

how to write print function for a frame

Posted on 2000-02-10
11
Medium Priority
?
261 Views
Last Modified: 2010-04-16
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......*^^*
0
Comment
Question by:bhuey_ling
  • 6
  • 5
11 Comments
 
LVL 4

Accepted Solution

by:
jerch earned 400 total points
ID: 2509944

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
 

Author Comment

by:bhuey_ling
ID: 2510738
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
 
LVL 4

Expert Comment

by:jerch
ID: 2510765
Can you show me some lines in your code?




0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:bhuey_ling
ID: 2510789
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
 
LVL 4

Expert Comment

by:jerch
ID: 2510826
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
 

Author Comment

by:bhuey_ling
ID: 2523119
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
 
LVL 4

Expert Comment

by:jerch
ID: 2525269
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
 

Author Comment

by:bhuey_ling
ID: 2532036
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
 
LVL 4

Expert Comment

by:jerch
ID: 2533250
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
 

Author Comment

by:bhuey_ling
ID: 2533834
thnx......
hlb
0
 
LVL 4

Expert Comment

by:jerch
ID: 2533848
:-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month4 days, 3 hours left to enroll

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question