swebert
Swing
1 Answer(s)      5 years and 6 months ago
Posted in : Swing AWT
hi sir i have a code like this:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AnotherPrintDemo extends JFrame {
DrawingCanvas canvas;

JButton setUpButton = new JButton("Page Setup");

JButton printButton = new JButton("Print");

JButton cancelButton = new JButton("Cancel");

public AnotherPrintDemo() {
super();
Container container = getContentPane();

canvas = new DrawingCanvas();
container.add(canvas);

JPanel panel = new JPanel(new GridLayout(1, 3));

ButtonListener buttonListener = new ButtonListener();
setUpButton.addActionListener(buttonListener);
panel.add(setUpButton);

printButton.addActionListener(buttonListener);
panel.add(printButton);

cancelButton.addActionListener(buttonListener);
panel.add(cancelButton);

container.add(BorderLayout.SOUTH, panel);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(650, 275);
setVisible(true);
}

class ButtonListener implements ActionListener {
PrinterJob printJob;

PageFormat pageFormat;

PrintableCanvas printableCanvas;

ButtonListener() {
printJob = PrinterJob.getPrinterJob();
pageFormat = printJob.defaultPage();
}

public void actionPerformed(ActionEvent e) {
JButton tempButton = (JButton) e.getSource();

if (tempButton.equals(setUpButton)) {
pageFormat = printJob.pageDialog(pageFormat);
printJob.validatePage(pageFormat);
} else if (tempButton.equals(printButton)) {
printableCanvas = new PrintableCanvas(pageFormat);
printJob.setPrintable(printableCanvas);

boolean ok = printJob.printDialog();
if (ok) {
try {
printJob.print();
} catch (Exception pe) {
System.out.println("Printing Exception Occured!");
pe.printStackTrace();
}
}
} else if (tempButton.equals(cancelButton)) {
printJob.cancel();
}
}
}

public static void main(String arg[]) {
new AnotherPrintDemo();
}
}

class DrawingCanvas extends JPanel {
String Str;
Font font;

FontMetrics fontMetrics;

int w, h;

DrawingCanvas() {
setBackground(Color.white);
setSize(400, 275);

w = this.getWidth();
h = this.getHeight();

font = new Font("Dialog", Font.BOLD, 50);
fontMetrics = getFontMetrics(font);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;

paintContent(g2D, w, h);
}

public void paintContent(Graphics2D g2D, int w, int h) {
g2D.setFont(font);

g2D.drawString(Str, 0,
(float) (0.5 * h - 1.25 * fontMetrics.getHeight()));

}
}

class PrintableCanvas implements Printable {
DrawingCanvas canvas;

PageFormat pageFormat;

public PrintableCanvas(PageFormat pf) {
pageFormat = pf;
}

public int print(Graphics g, PageFormat pageFormat, int pageIndex)
throws PrinterException {
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}

Graphics2D g2D = (Graphics2D) g;

canvas = new DrawingCanvas();

canvas.paintContent(g2D, (int) pageFormat.getImageableWidth(),
(int) pageFormat.getImageableHeight());

// successful printing of the page
return Printable.PAGE_EXISTS;
}
}

so In the DrawingCanvas class there is String Variable Called Str so I want to send value to the String Variable from other JFrame is it Possible
how can it be done

Advertisement
View Answers

March 27, 2010 at 11:36 AM


Hi Friend,

You haven't initialize the String variable Str therefore you got that error.

For more information, visit the following link:

http://www.roseindia.net/java/example/java/swing/get_jtextfield_value_from_anothe.shtml

Thanks









Related Tutorials/Questions & Answers:
SWING
SWING  A JAVA CODE OF MOVING TRAIN IN SWING
swing
swing  Write a java swing program to delete a selected record from a table
Advertisements
swing
swing  How to make swing component auto-resizable when JFrame resize
Swing
Swing  Write a java swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
Swing
Swing  Write a java swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
swing-awt - Swing AWT
swing-awt  Hi, Thanks for replying to my question...I'm getting some confusion to add action events in my application(Rich Text Editor).How to add action events? Thank U
java swing - Swing AWT
java swing  how i can insert multiple cive me exampleolumn and row in one JList in swing?plz g  Hi Friend, Please clarify your question. Thanks
swing/awt - Swing AWT
swing/awt  How to create richtexteditor using swings...?I'm very much new to swings....It's urgent.....Thank u...   hello read this book you get idea; JFC Swing Tutorial, The: A Guide to Constructing GUIs, Second
What is Swing?
What is Swing?  What is Java Swing? Where to get the tutorials of Swing? I am beginner in Java Swing programming and trying to find the tutorials of Swing. Tell me the urls to learn swing. Thanks   Hi, Swing is Java
Swing paint - Swing AWT
Swing paint  hi, i want to print something on window using swing applet.. m doing dis.. protected void paintComponent(Graphics g... the Swing Applet, use html file with the following code: Java Applet Demo
SWING FRMES
SWING FRMES  hai SIR? HOW TO DESIGN swing Frames send source code
SWING FRMES
SWING FRMES  hai SIR? HOW TO DESIGN swing Frames send source code
Swing Program
Swing Program  Write a java swing program to calculate the age from given date of birth
swing sms
swing sms  HOW TO SEND SMS MESSAGE FROM SWING USING SQL DATABASE
swing to applet
swing to applet  hi how i can isplay a java swing into applet java thanks
java swing - Swing AWT
java swing  how to add image in JPanel in Swing?  Hi Friend, Try the following code: import java.awt.*; import java.awt.image....: http://www.roseindia.net/java/example/java/swing/ Thanks
java swing - Swing AWT
java swing   how i can insert in JFrame in swing?  Hi Friend, Try the following code: import java.awt.*; import javax.swing.*; import java.awt.event.*; class FormDemo extends JFrame { JButton ADD; JPanel
java swing - Swing AWT
java swing   Iam developing a java web browser.Actually my code works fine ie. i can load a web page without proxy.But in my place i have only proxy servers through which i should visit any web site.So, i want my browser also
SWING - Swing AWT
SWING  how can i insert image in Jpanel by extending class with JFrame in swing?  Hi Friend, Try the following code: import java.awt.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import
Jva swing
Jva swing   How to create the model form like "Notepad
printing in swing
printing in swing  How can i print payslip in swing
java swing
java swing  view the book details using swing
AWT & SWING
AWT & SWING  What is diffennce between AWT & SWING
Java swing
Java swing  Does Swing contains any heavy weight component
Java swing
Java swing  what are the root classes of all classes in swing
swing program
swing program  Write a java swing program to getname and email id. Display the message if email id contains the name of theuser
Swing question - Swing AWT
Swing question  I want to move a string in JFrame similarily as done by marquee tag in HTML.The string should move from right to left. How can I do this?  Hi Friend, Try the following code: import java.awt.
SWING - Swing AWT
SWING  how to insert image and components such as JLabel,JButton,JTextfield in JFrame in swing?  Hi Friend, Try the following code: import java.io.*; import java.awt.*; import javax.swing.*; import
swing - Swing AWT
swing   how to create JTabbedPane.when i choose one jtabbedpane it will appear JTextField,resultMessages.pls give me any examples  Hi Friend, Try the following code: import javax.swing.*; import java.awt.
swing with jdbc - Swing AWT
swing with jdbc  Hi, i m developing the desktop application using swing.i want to use table structure for that.also its DB specific application. i have some problems with swing using jdbc. 1. the values entered in table
java swing
java swing  what is java swing   Swing is a principal GUI toolkit for the Java programming language. It is a part of the JFC (Java... the following link: Swing Tutorials