Results 1 to 3 of 3
  1. #1
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    450
    Rep Power
    6

    Default How to make print function for single label?

    Hello to all?

    is it possible to print single label in JFrame.

    In my application i want to print one Label that holds the address of the customer.i am trying to make print.
    but i was able to print full frame and not able to print label only...


    Send your solution to me.
    Mak
    (Living @ Virtual World)

  2. #2
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    i can think of two options to do this:

    1) write only normal text

    get the Text from the label and write it like normal text
    How to Print Text (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Other Swing Features)

    2) make your label Printable ;)

    you must extend the JLabel class and make it printable

    Java Code:
    class MyLabel extends JLabel implements Printable{
    		
    	public MyLabel(String text){
    		super(text);
    	}
    
    	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) 	throws PrinterException {
    		
                    Graphics2D graphics2D = (Graphics2D) graphics;
                    graphics2D.drawString(this.getText(), 72, 72);
                    return (PAGE_EXISTS);
    		}
    		
    }
    the 72,72 is the position (x,y) which I choose to draw the text .... you can put there what you want

    hope it helped :P

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Mak- GUI and printing are separate concepts. While what Wolfcro described is slick, it only solves the immediate question. I doubt it would allow you to print two labels at once.

    Again, Java is not made for this sort of programming. The controller class should take the data from the label and pass it to a printing class that knows how to print the data in the correct format.

Similar Threads

  1. Replies: 5
    Last Post: 08-26-2008, 04:43 PM
  2. First SWT Label
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:38 PM
  3. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:04 AM
  4. Example of SWT Label
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 01:02 PM
  5. Replies: 0
    Last Post: 08-06-2007, 07:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •