Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6096533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:56:18+00:00 2026-05-23T12:56:18+00:00

// Program to print simple text on a Printer import javax.swing.*; import java.awt.*; import

  • 0
// Program to print simple text on a Printer

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.awt.print.*;

class Printer extends JPanel implements Printable  {

  JButton print;

  Printer() {
    buildGUI();
    hookUpEvents();
  }

  public void buildGUI() {
    JFrame fr = new JFrame("Program to Print on a Printer");
    JPanel p = new JPanel();
    print = new JButton("Print");
    setPreferredSize( new Dimension ( 200,200 ) );
    p.setBackground( Color.black );
    fr.add(p);
    p.add( print , BorderLayout.CENTER );
    fr.pack();
    fr.setVisible( true );
  }

  public void hookUpEvents() {
    print.addActionListener( new ActionListener() {
      public void actionPerformed( ActionEvent ae ) {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable( new Printer() );
        boolean doPrint = job.printDialog();
        if( doPrint ) {
          try {
            job.print();
          } catch( PrinterException exc) {
            System.out.println( exc );
          }
        } else {
          System.out.println("You cancelled the print");
        } 
      }
    });
  }

  public int print( Graphics g , PageFormat pf , int pageIndex) throws PrinterException{
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    g.drawString( "testing..." , 100 , 100 );
    return PAGE_EXISTS;
  }

  public static void main( String args[] ) {
    new Printer();
  }
}

After the command java Printer the output is :

enter image description here

But as i click print , along with the following window :

enter image description here

i again get the former window. Why does this happen ?

When does the print method of Printable interface get called in this program?

Why does my window size come small when i have set =200,200.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:56:18+00:00Added an answer on May 23, 2026 at 12:56 pm

    All right, so you had three questions:

    First: The reason why your window is small is because you’re using jf.pack() along with a border layout, which makes your JFrame the size required to display all the component it contains without extra space around. You could set the layout to null and lose the jf.pack() part, but this is not usually a recommended practice.

    Second: The window pops up a second time because you’re creating a second instance of the same object in your listener there:

    job.setPrintable( new Printer() );
    

    The “new Printer()” part creates another Printer object, which calls the UI creation again, etc.

    You could create an inner class instead of an anonymous class to be able to use “this” to refer to the current Printer object.

    Your hookUpEvents() method would be something like this:

    public void hookUpEvents() {
      MyActionListener mal = new MyActionListener();
      print.addActionListener(mal); {
    }
    

    Then, somewhere else in the same class create the inner class like this:

    private class MyActionListener implements ActionListener{
      public void actionPerformed(ActionEvent ae) {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(Printer.this);
        boolean doPrint = job.printDialog();
         if( doPrint ) {
           try {
            job.print();
           }  
           catch( PrinterException exc) {
               System.out.println( exc );
           }
         }  
         else {
            System.out.println("You cancelled the print"); 
          } 
        }
    }
    

    Third: The print() method is called when you type: “job.print()” since you’re overriding the method from the interface. The one in the interface is never actually called since yours is called instead, which is what you want since you defined what it had to do.

    I hope this answers your questions now.

    Edit: I just tested something similar and I think you could just type Printer.this in the anonymous class to make it work instead of creating an inner class, which would have you change way less code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

// Program to print simple text on a Printer import javax.swing.*; import java.awt.*; import
Description | A Java program to read a text file and print each of
I write a simple program to print a image in JSF.... I have one
I'm writing a Java program and need to analyze small chunks of text (3-4
I'm trying to read from a simple text file. Whenever I run the program
I need to write a simple terminal-based program that should, Read some text from
I need simple program in C++ that will open text file and going line
simple question: why this doesnt work? test = 2 print subprocess.check_output([program, -v, -a5, -t%i,
Is there a program that will print a nicely formatted tree from XML data?
I want to write a program that would print every combination of a set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.