Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

easy printing

Posted on 2004-09-29
10
Medium Priority
?
188 Views
Last Modified: 2010-03-31
Hi experts,

I need a very simple way to get a menuitem "Print" to print all the text in an internal frame

thank you
0
Comment
Question by:Samooramad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12183855
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12183864
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 2000 total points
ID: 12183919
// Printing Method

     private void print()
     {
     int exit = JOptionPane.showConfirmDialog
     (null,"Are you sure you want to print ?",
     "Timeport . . .",JOptionPane.YES_NO_OPTION);
 
     if(exit==JOptionPane.YES_OPTION)
     {
     printPage(t1.getText());
     }
     }

     public void printPage(String textToPrint) {

     PrinterJob job = PrinterJob.getPrinterJob();
     PageFormat landscape = job.defaultPage();

     landscape.setOrientation(PageFormat.LANDSCAPE);
     Book bk = new Book();
     bk.append(new PaintCover(), job.defaultPage());

     job.setPageable(bk);

     if (job.printDialog()) {
     try {
     job.print();
     }catch (Exception exc){
     System.out.println(exc);
     }
     }
     }

     class PaintCover implements Printable{
     Font fnt = new Font("Helvetica-Bold", Font.PLAIN, 12);

     public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
     g.setFont(fnt);
     g.setColor(Color.black);
     g.drawString(t1.getText(), 100, 200);
     return Printable.PAGE_EXISTS;
     }
     }
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 15

Expert Comment

by:Javatm
ID: 12185238
> sudhakar_koundinya

Nice one, that was from my old codes befor I think it can work in somepoint but not all the time.
Printing in java is definetly hard to do w/ any knowledge in it. What text component
are you using ? is it JTextPane ?
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12186933
Yeah,

I copy and pasted that code from your old threads :-) . I realised that it is giving problems when i tested at my end. Just checking for right solution.
0
 

Author Comment

by:Samooramad
ID: 12188659
Hi everyone,
I appritiate the help but I really felt lost reading the articles. I had no idea where to start. I really need something very basic.
sudhakar I will try the code you posted

thanks everyone
0
 

Author Comment

by:Samooramad
ID: 12188667
is there anything I need to change in the code to get it to work with my program?
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12203364
This sample may helps you. Although I am not sure 100%. This just tries to print the  graphics shapes from JFrame

import java.awt.geom.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterJob;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

public  class PrintTest extends JPanel implements Printable, ActionListener {


    final static Color bg = Color.white;
    final static Color fg = Color.black;
    final static Color red = Color.red;
    final static Color white = Color.white;

    final static BasicStroke stroke = new BasicStroke(2.0f);
    final static BasicStroke wideStroke = new BasicStroke(8.0f);

    final static float dash1[] = {10.0f};
    final static BasicStroke dashed = new BasicStroke(1.0f,
                                                      BasicStroke.CAP_BUTT,
                                                      BasicStroke.JOIN_MITER,
                                                      10.0f, dash1, 0.0f);
    final static JButton button = new JButton("Print");

    public PrintTest() {
        setBackground(bg);
        button.addActionListener(this);
    }


   public void actionPerformed(ActionEvent e) {
        if (e.getSource() instanceof JButton) {  
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(this);
        if (printJob.printDialog()) {
            try {
                printJob.print();  
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        }
}
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        drawShapes(g2);
}
public void drawShapes(Graphics2D g2){
        Dimension d = getSize();
        int gridWidth = 400 / 6;
        int gridHeight = 300 / 2;

        int rowspacing = 5;
        int columnspacing = 7;
        int rectWidth = gridWidth - columnspacing;
        int rectHeight = gridHeight - rowspacing;

        Color fg3D = Color.lightGray;

        g2.setPaint(fg3D);
        g2.drawRect(80, 80, 400 - 1, 310);
        g2.setPaint(fg);

        int x = 85;
        int y = 87;


        // draw Line2D.Double
        g2.draw(new Line2D.Double(x, y+rectHeight-1, x + rectWidth, y));
        x += gridWidth;

        // draw Rectangle2D.Double
        g2.setStroke(stroke);
        g2.draw(new Rectangle2D.Double(x, y, rectWidth, rectHeight));
        x += gridWidth;

        // draw  RoundRectangle2D.Double
        g2.setStroke(dashed);
        g2.draw(new RoundRectangle2D.Double(x, y, rectWidth,
                                            rectHeight, 10, 10));
        x += gridWidth;

        // draw Arc2D.Double
        g2.setStroke(wideStroke);
        g2.draw(new Arc2D.Double(x, y, rectWidth, rectHeight, 90,
                                 135, Arc2D.OPEN));
        x += gridWidth;

        // draw Ellipse2D.Double
        g2.setStroke(stroke);

        g2.draw(new Ellipse2D.Double(x, y, rectWidth, rectHeight));
        x += gridWidth;

        // draw GeneralPath (polygon)
        int x1Points[] = {x, x+rectWidth, x, x+rectWidth};
        int y1Points[] = {y, y+rectHeight, y+rectHeight, y};
        GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
                                              x1Points.length);
        polygon.moveTo(x1Points[0], y1Points[0]);
        for ( int index = 1; index < x1Points.length; index++ ) {  
            polygon.lineTo(x1Points[index], y1Points[index]);
        };
        polygon.closePath();

        g2.draw(polygon);

        // NEW ROW
        x = 85;
       y += gridHeight;

        // draw GeneralPath (polyline)

        int x2Points[] = {x, x+rectWidth, x, x+rectWidth};
        int y2Points[] = {y, y+rectHeight, y+rectHeight, y};
        GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
                                               x2Points.length);
        polyline.moveTo (x2Points[0], y2Points[0]);
       for ( int index = 1; index < x2Points.length; index++ ) {
            polyline.lineTo(x2Points[index], y2Points[index]);
        };

        g2.draw(polyline);
        x += gridWidth;

        // fill Rectangle2D.Double (red)
        g2.setPaint(red);
        g2.fill(new Rectangle2D.Double(x, y, rectWidth, rectHeight));
        g2.setPaint(fg);
        x += gridWidth;

        // fill RoundRectangle2D.Double
        GradientPaint redtowhite = new GradientPaint(x,y,red,x+rectWidth, y,white);
        g2.setPaint(redtowhite);
        g2.fill(new RoundRectangle2D.Double(x, y, rectWidth,
                                            rectHeight, 10, 10));
        g2.setPaint(fg);
        x += gridWidth;

        // fill Arc2D
        g2.setPaint(red);
        g2.fill(new Arc2D.Double(x, y, rectWidth, rectHeight, 90,
                                 135, Arc2D.OPEN));
        g2.setPaint(fg);
        x += gridWidth;

        // fill Ellipse2D.Double
        redtowhite = new GradientPaint(x,y,red,x+rectWidth, y,white);
        g2.setPaint(redtowhite);
        g2.fill (new Ellipse2D.Double(x, y, rectWidth, rectHeight));
        g2.setPaint(fg);
        x += gridWidth;
        // fill and stroke GeneralPath
        int x3Points[] = {x, x+rectWidth, x, x+rectWidth};
        int y3Points[] = {y, y+rectHeight, y+rectHeight, y};
        GeneralPath filledPolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
                                                    x3Points.length);
        filledPolygon.moveTo(x3Points[0], y3Points[0]);
        for ( int index = 1; index < x3Points.length; index++ ) {
            filledPolygon.lineTo(x3Points[index], y3Points[index]);
        };
        filledPolygon.closePath();
        g2.setPaint(red);
        g2.fill(filledPolygon);
        g2.setPaint(fg);
        g2.draw(filledPolygon);
    }


    public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
        if (pi >= 1) {
            return Printable.NO_SUCH_PAGE;
        }
        drawShapes((Graphics2D) g);
        return Printable.PAGE_EXISTS;
    }

   public static void main(String s[]){
        WindowListener l = new WindowAdapter() {
                public void windowClosing(WindowEvent e) {System.exit(0);}
                public void windowClosed(WindowEvent e) {System.exit(0);}
        };
        JFrame f = new JFrame();
        f.addWindowListener(l);
        JPanel panel = new JPanel();
        panel.add(button);
        f.getContentPane().add(BorderLayout.SOUTH, panel);
        f.getContentPane().add(BorderLayout.CENTER, new PrintTest());
        f.setSize(580, 500);
        f.show();
    }

}
0
 

Author Comment

by:Samooramad
ID: 12211374
great! thank you
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:
Suggested Courses

670 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