printing jframe content in core java

I want to print Jframe like our word document.

I have one jframe on jframe i have Jpanel ond on that panel i have j label . In jlabel i written a letter i want to print
that letter so how can I print that letter.

Posted On: Sunday 21st of October 2012 11:58:06 PM Total Views:  494
View Complete with Replies

Related Messages:

make all components visble, when jframe size changed horizontally or vertically   (268 Views)
Hi I have developed user interface in swing with two panels, lower panel and upper panel, both uses FormLayout and used BorderLayout to add these panels to JFrame. When the JFrame size is changed horizontally or vertically some components are not visible in JFrame going out. Please help how to minimize the size of all the components to make visble, when JFrame size is changed. Many
problem in printing swing components?   (541 Views)
i have a registration page, and after registration of a student i need to print the tc (transfer certificate), in the (Photo1430.jpg) image format, (in the same format shown in the image). field values are coming from the database.(ie. value of name, value of dob, value of place so on...) i am doing something like this: import java.awt.*; import java.awt.event.*; import java.awt.print.*; import javax.swing.*; public class PrintUIWindow implements Printable, ActionListener { JFrame frameToPrint; JLabel lblStuName, lblBirthPlace, lblDob, lblLastSch, lblLastExam, lblYearOdAdd,lblStudyingStandard; JLabel d, d1, d2, d3, d4; static JPanel panel; Box horiz1, horiz2, horiz3, horiz4, horiz5, horiz6; public PrintUIWindow() { panel = new JPanel(); lblStuName = new JLabel("1.) Name of Student (in Full)"); lblBirthPlace = new JLabel("2.) Place of Birth"); lblDob = new JLabel("3.) Date of Birth, Month and year (in figures)"); lblLastSch = new JLabel("4.) Last School College Attended"); lblLastExam = new JLabel("5.) Last Examination Passed"); d1 = new JLabel(" : ........................................................."); d2 = new JLabel(" : ........................................................."); d3 = new JLabel(" : ........................................................."); d4 = new JLabel(" : ........................................................."); d = new JLabel(" : ........................................................."); horiz1 = Box.createHorizontalBox(); horiz1.add(lblStuName); horiz1.add(d); horiz2 = Box.createHorizontalBox(); horiz2.add(lblBirthPlace); horiz2.add(d1); horiz3 = Box.createHorizontalBox(); horiz3.add(lblDob); horiz3.add(d2); horiz4 = Box.createHorizontalBox(); horiz4.add(lblLastSch); horiz4.add(d3); horiz5 = Box.createHorizontalBox(); horiz5.add(lblLastExam); horiz5.add(d4); Box vert = Box.createVerticalBox(); vert.add(Box.createVerticalStrut(100)); vert.add(horiz1); vert.add(Box.createVerticalStrut(30)); vert.add(horiz2); vert.add(Box.createVerticalStrut(30)); vert.add(horiz3); vert.add(Box.createVerticalStrut(30)); vert.add(horiz4); vert.add(Box.createVerticalStrut(30)); vert.add(horiz5); panel.add(vert); } public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { /* We have only one page, and 'page' is zero-based */ return NO_SUCH_PAGE; } /* * User (0,0) is typically outside the imageable area, so we must translate * by the X and Y values in the PageFormat to avoid clipping */ Graphics2D g2d = (Graphics2D) g; g2d.translate(pf.getImageableX(), pf.getImageableY()); /* Now print the window and its visible contents */ frameToPrint.print(g); /* tell the caller that this page is part of the printed document */ return PAGE_EXISTS; } public void actionPerformed(ActionEvent e) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); boolean ok = job.printDialog(); if (ok) { try { job.print(); } catch (PrinterException ex) { /* The job did not successfully complete */ } } } public static void main(String args[]) { JFrame f = new JFrame("Print TC"); JButton printButton = new JButton("Print TC"); printButton.addActionListener(new PrintUIWindow()); panel.add("South", printButton); f.add(panel); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); } }
Java code printing junk characters in PDF   (463 Views)
I have a JAVA code that reads a PDF file from remote server. It then merges it with an existing PDF file and print the resultant. However, sometimes I observe that the PDF printed contains gibbersih data. It doesn't happen always. Is it some issue with the fonts used in PDF Or some inherent issue of Java, while printing PDF files. I use a print job object to print PDF file. Logic for printing the PDF file is as following private boolean printDocument(String fileName){ System.out.println("printComplianceDocument : Begin"); boolean result = false; PDDocument document = null; PDPage page = new PDPage(); try { document = PDDocument.load(fileName); PrinterJob pjob = PrinterJob.getPrinterJob(); PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet(); attr_set.add(Sides.DUPLEX); PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attr_set); for(PrintService service : services){ if (service.getName().equalsIgnoreCase(SystemProp.getInstance().getWorkStation().getDfltRptPrinter().getAddress().trim())){ System.out.println("Printing Document to printer: " + service.getName()); pjob.setPrintService(service); pjob.setPageable(document); pjob.print(attr_set); result = true; break; } } //Try printing to Default Printer if appropriate Print Service is not found if(!result){ PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService(); System.out.println("Printing Document to Default Printer: " + defaultPrintService.getName()); pjob.setPrintService(defaultPrintService); pjob.setPageable(document); pjob.print(attr_set); result = true; } } catch (IOException e) { // Exception } catch (PrinterException e) { // Exception } System.out.println("printDocument : End"); return result; } Please share your ideas and suggest solutions if you could.
how to print a jframe ?   (255 Views)
i have a requirement , which is exact like a form on the internet , in that you fill the form and you get a printable form but , this is for desktop app i have a fully designed app or jframe which accepts certain input , gives appropriate output , but the problem is this frame is designed with company logo on a jlabel and also has a jtable for my requirement, i can pull the value from jtable and store it in a excel file and can print that excel file using Desktop class but what about images on the frame , how to print that images or copy that images in excel file does any one have ever find urself in such a situation
printing JTables - concatenating? what about the panels?   (655 Views)
I am printing a JTable in this manner: StringBuffer sb = new StringBuffer(); PrintRequestAttributeSet attribs = new HashPrintRequestAttributeSet(); attribs.add(new Copies(1)); attribs.add(OrientationRequested.PORTRAIT); int xx = 1; //left and right margin int yy = 1; //top and bottom margin. Note that bottom margin cannot be less than 15 mm int ww = 845; //Width int hh = 599; //Height int units = MediaPrintableArea.MM; attribs.add(new MediaPrintableArea(xx, yy, ww, hh, units)); String title = "My Table"; sb.append(title); MessageFormat footer = new MessageFormat("Page {0}"); getPayrollHeadcountList().getTable().setPreferredSize(new Dimension(500, 500)); getPayrollHeadcountTotalList().getTable().setPreferredSize(new Dimension(500, 100)); try { getPayrollHeadcountList().getTable().print(PrintMode.FIT_WIDTH, header, footer, true, attribs, false); //getPayrollHeadcountTotalList().getTable().print(PrintMode.FIT_WIDTH, header, footer, true, attribs, false); } catch (PrinterException pe) { Debug.trace(pe); }
Simple text printing with pagebreaks   (590 Views)
I have a report displayed in a GUI application as a simple text within JTextComponent. My goal is to print it but with page breaks at certain points. I have inserted \f at those places, however when I print page breaks are ignored and I get a weird character printed in those places where \f was and the rest of the text is just printed out without page breaks. One thing I v noticed is that if I output the report to a file and open it with MS Word page breaks are recognised and report is displayed correctly. However, if I try to open it with notepad page breaks are not recognised. I have tried converting the text to RTF but the \f character was still ignored. I would really appreciate if anyone shares their experience with something similar and how did you solve it cheers
printing a jtable   (580 Views)
I've got a swing JTable of about 20 rows ans 40 columns that I want to print on paper. I tried the table.print() method but that doesn't work, I just get one big empty cell on the paper. I tried an example from another site and that one works perfectly ( Does anyone know what's going wrong Is my table too large for the paper and do I need to resize it, and if so how
JTable printing problem   (630 Views)
I have sample of JTable with data that i would like to be right aligned, also orange background color cell renderer is applied, and font is set to be Tahoma BoldItalic with font size of 13. Problems that arise are, grid lines are over cell text, and also applied background color looks strange when PDF printer is used and document is zoomed. Interesting thing is that when font is set to be even grid lines doesn't intersect cell text. import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; 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.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.JTable.PrintMode; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; public class TablePrintTest extends JFrame { private static final long serialVersionUID = 1L; private static final Object[] COLUMN_NAMES = { "Column1", "Column2", "Column3" }; private static final Object[][] CELL_VALUES = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; private JButton printButton; private JTable table; public TablePrintTest() { super("TablePrintTest"); createGui(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void createGui() { table = new JTable(new DefaultTableModel() { private static final long serialVersionUID = 1L; @Override public Class getColumnClass(int columnIndex) { return Number.class; } @Override public int getColumnCount() { return COLUMN_NAMES.length; } @Override public int getRowCount() { return CELL_VALUES.length; } @Override public String getColumnName(int column) { return COLUMN_NAMES[column].toString(); } @Override public Object getValueAt(int row, int column) { return CELL_VALUES[row][column]; } }); table.setDefaultRenderer(Number.class, new DefaultTableCellRenderer() { private static final long serialVersionUID = 1L; @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { JLabel label = (JLabel) super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column); label.setHorizontalAlignment(SwingConstants.RIGHT); return label; } public java.awt.Color getBackground() { return; }; }); table.setFont(new Font("Tahoma", Font.ITALIC | Font.BOLD, 13)); printButton = new JButton("Print"); printButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Printable printable = table.getPrintable(PrintMode.NORMAL, null, null); PrinterJob pJob = PrinterJob.getPrinterJob(); pJob.setPrintable(printable); if (pJob.printDialog()) { try { pJob.print(); } catch (PrinterException e1) { e1.printStackTrace(); } } } }); JPanel contentPane = new JPanel(new BorderLayout()); contentPane.add(printButton, BorderLayout.NORTH); contentPane.add(new JScrollPane(table), BorderLayout.CENTER); setContentPane(contentPane); pack(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new TablePrintTest().setVisible(true); } }); } }
Problem in printing JTable values   (588 Views)
Dear Friends, I have to print the JTable values (on paper using print dialogue box). I'm using jtableObj.print to print the table values. But jtableObj.print takes more time to print the table values because it prints the values as a image(approximately 5 mins to print 10 pages). My code is , boolean complete = tableObj.print(mode, header, footer, showPrintDialog, null, interactive, null);
Printing jframe in A5 paper   (312 Views)
I want to print jframe contain in A5 paper.I've print it.But i don't know how to set A5 paper. this is my printing code. PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); boolean ok = job.printDialog(); if (ok) { try { job.print(); } catch (PrinterException ex) { } } Can any one help me. Thank.
Over writting JA\Panel content   (140 Views)
Hi Friends, I've created a JFrame with Layout as Border Layout.At the center of Border layout i've placed a JPanel.Moment this JFrame is opened, at the center you can see a JPanel consisting of textboxes waiting for the user id and password to be inpued. Now my problem is ,when user logs in ,a new screen should be painted at the Center area of the JFrames content pane over writing the existing content. This can be achieved if you could place 'n' number of JPanels inadvance and depending the condition show the interested JPanel by hidding rest of the JPanels. My criteria is ,I want to over write during the run time.There is no limit for the subsequent screens overwriting each other. How this can be achieved......
printing on A4 paper   (655 Views)
The paper object creates a letter sized piece of paper with one inch margins. Is there any way to set different margins to allow a page designed to fit on letter to also print on A4 by reducing the margins Or any other ideas ------------------
HTMLEditorKit SCRIPT content tag disappears after read   (162 Views)
I am trying to read and display an HTML Document using JEditorPane and HTMLEdiorKit. I am overridding javax.swing.text.html.HTMLDocument.HTMLReader class to read HTML document. My javascript content(functions) disappers when I read the html document. It only displays the content of the script disappears. I tried to register script tag with a tag action class, still I can't see content. I tried to use the orginal HTMLReder. It didn't help.
printing html and images using JEditorPane   (512 Views)
hi i am working on printing multiple html and images(png) in a single batch.. i am able to print all in a single batch if html files are single page files but if they are multipage files i am unable to print them. i am rendering html to a JEditorPane. i can print multiple multipage html files in a single batch but unable to do html files and images in a single batch .. anyone can help me thanx in advance -if you want i can provide code bye madhu
How to disable maximise button of jframe   (244 Views)
actually am developing application and i want to disable maximize button of jframe... i am using this to enable JFrame.setDefaultLookAndFeelDecorated(true); now i want to disable the maximize button
changing contents of a combo box   (138 Views)
Anyone have an example of modifing the contents of a JComboBox on the fly Let's say I have a combo box with a list of the numbers from 1 through 10. Later on I want that same comboBox to have the letters of the alphabet.
How do you having a varying content in a JFrame's status bar?   (138 Views)
I have a swing application, in which I have a title bar. In this application, when I do any lengthier operation, lets say, fetch some records based on a complex criteria, I want my status bar to say "Processing Request" and should refresh to "Processing completed." when the page gets painted again. Right now, though I have 2 messages, I seeing the final message alone, only after the operation gets completed. Till such time, I only see the background of the application itself. I tried implementing a JTextField to have the status bar text, but the same result. Can you tell me how I can do it [ September 30, 2004: Message edited by: SrinivasanRG Gopal ]
printing data from JEditor Pane   (424 Views)
I have a program in which the data is shown in JEditor Pane. I have used HTMl tags to get the deed format. Now i want to print it. how should i proceed :roll:
printing html file   (518 Views)
hello, i have a html file. i want to print it. i using jeditorpane for showing the html file. because html file is large enough that i cannot show it whole in jeditorpane. so i used the container jscrollpane. when i want to print the document showing in the jscrollpane it print only the current shwing document not the whole document. please help me to solve this.
Delay in displying contents of JDialog   (123 Views)
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.List; import java.util.ArrayList; public class SendEmailForm extends JDialog { private JLabel to, cc, bcc, subject, account; private JTextField toTxt, ccTxt, bccTxt, subjectTxt; private JTextArea messageTxt; private JButton send; private JComboBox accountBox; private JScrollPane scroll; private GridBagLayout gbl; private GridBagConstraints gbc; public SendEmailForm() { //Declaring instance variables to = new JLabel("To: "); cc = new JLabel("CC: "); bcc = new JLabel("BCC: "); subject = new JLabel("Subject: "); account = new JLabel("Select an Account: "); toTxt = new JTextField(20); ccTxt = new JTextField(20); bccTxt = new JTextField(20); subjectTxt = new JTextField(20); messageTxt = new JTextArea(20,50); messageTxt.setLineWrap(true); scroll = new JScrollPane(messageTxt); scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); accountBox = new JComboBox(); accountBox.addItem("Yahoo"); accountBox.addItem("GMail"); accountBox.addItem("MSN"); //accountBox.addItem("Yahoo"); //accountBox.addItem("Yahoo"); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); send = new JButton("Send"); send.addActionListener(new SendButtonAction()); buttonPanel.add(send); //Creating thr GUI gbl = new GridBagLayout(); gbc = new GridBagConstraints(); this.setLayout(gbl); gbc.gridx = 1; gbc.gridy = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.BOTH; this.add(account,gbc); gbc.gridx = 2; gbc.gridy = 1; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.BOTH; this.add(accountBox,gbc); gbc.gridx = 1; gbc.gridy = 2; gbc.weightx = 0.0; gbc.fill = GridBagConstraints.BOTH; this.add(to,gbc); gbc.gridx = 2; gbc.gridy = 2; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.BOTH; this.add(toTxt,gbc); gbc.gridx = 1; gbc.gridy = 3; gbc.weightx = 0.0; gbc.fill = GridBagConstraints.BOTH; this.add(bcc,gbc); gbc.gridx = 2; gbc.gridy = 3; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.BOTH; this.add(bccTxt,gbc); gbc.gridx = 1; gbc.gridy = 4; gbc.weightx = 0.0; gbc.fill = GridBagConstraints.BOTH; this.add(cc,gbc); gbc.gridx = 2; gbc.gridy = 4; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.BOTH; this.add(ccTxt,gbc); gbc.gridx = 1; gbc.gridy = 5; gbc.weightx = 0.0; gbc.fill = GridBagConstraints.BOTH; this.add(subject,gbc); gbc.gridx = 2; gbc.gridy = 5; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.BOTH; this.add(subjectTxt,gbc); gbc.gridx = 1; gbc.gridy = 6; gbc.gridwidth = 2; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.BOTH; this.add(scroll,gbc); gbc.gridx = 2; gbc.gridy = 7; gbc.fill = GridBagConstraints.BOTH; gbc.insets = new Insets(0,500,0,0); this.add(buttonPanel,gbc); this.setTitle("Send Emails"); this.setVisible(true); this.pack(); this.setLocationRelativeTo(null); this.validate(); } private class SendButtonAction implements ActionListener { public void actionPerformed(ActionEvent ae) { //Retreiving the user name and password List userData = new ArrayList(); EmailDBConnector emailCon = new EmailDBHandler(); userData = emailCon.getUserNameAndPassword(accountBox.getSelectedItem().toString().trim()); String userName = userData.get(0).toString(); String password = userData.get(1).toString(); System.out.println(userName); System.out.println(password); ProgressMonitor pm = new ProgressMonitor(); SendEmail sendEmail = new SendEmail(toTxt.getText(),userName.trim(), bccTxt.getText()); String result = sendEmail.send(); //pm.dispose(); JOptionPane.showMessageDialog(null, result); } } private class ProgressMonitor extends JDialog { JLabel text; public ProgressMonitor() { text = new JLabel("Sending..Please wait..."); JPanel centerPanel = new JPanel(); centerPanel.setLayout(new FlowLayout()); centerPanel.add(text); this.setLayout(new BorderLayout()); this.add(centerPanel,"Center"); this.setLocationRelativeTo(null); this.pack(); setVisible(true); } } }