2014-10-16 2 views
4
PrinterJob job = PrinterJob.getPrinterJob(); 
    job.setJobName("jPanel2"); 
    job.setPrintable(new Printable() { 
     public int print(Graphics pg, PageFormat pf, int pageNum) { 
      if (pageNum > 0) { 
       return Printable.NO_SUCH_PAGE; 
      } 
      Graphics2D g2 = (Graphics2D) pg; 
      g2.translate(pf.getImageableX(), pf.getImageableY()); 
      jPanel2.paint(g2);`enter code here` 
      return Printable.PAGE_EXISTS; 
     } 
    }); 

위 코드를 사용하여 jPanel 내용을 인쇄했습니다. 하지만 jTable에있는 데이터를 인쇄하고 싶습니다. 내가 어떻게 할 수 있니?Java를 사용하여 하나의 노트에 jTable 내용 인쇄

+0

JTable에는 자체 프린터 인터페이스가 있습니다. JavaDocs를 확인하십시오. – MadProgrammer

답변

0
public void printJavaComponent() { 
PrinterJob job = PrinterJob.getPrinterJob(); 
job.setJobName("Print Java Component"); 

job.setPrintable (new Printable() {  
    public int print(Graphics g, PageFormat pageFormat, int pageIndex) { 
     if (pageIndex > 0) { 
      return(NO_SUCH_PAGE); 
     } else { 
      Graphics2D g2d = (Graphics2D)g; 
      g2d.translate(pageFormat.getImageableX(), 
      pageFormat.getImageableY()); 

      component_name_to_be_printed.paint(g2d); 

      return(PAGE_EXISTS); 
     } 
    } 
}); 

if (job.printDialog()) { 
    try { 
     job.print(); 
    } catch (PrinterException e) { 
     System.err.println(e.getMessage()); 
    } 
} 

}

내가 당신을 도와줍니다 생각이 코드를 시도하십시오.

2

이 시도 :

PrintRequestAttributeSet set = new HashPrintRequestAttributeSet(); 
set.add(OrientationRequested.LANDSCAPE); 
resultFxTable.print(JTable.PrintMode.FIT_WIDTH, header, footer, false, set, false); 
3
//bill printing 
    JFrame frame = new JFrame("Bill"); 
    frame.getContentPane().setBackground(Color.WHITE); 
    frame.getContentPane().setLayout(new FlowLayout()); 
    frame.setPreferredSize(new Dimension(400, 750)); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JScrollPane jp = new JScrollPane(); 
    frame.add(jp); 
    Container pane = frame.getContentPane(); 
    pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
    JLabel button; 
    pane.setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 
    //natural height, maximum width 
    c.fill = GridBagConstraints.HORIZONTAL; 
    button = new JLabel("Qty"); 
    c.weightx = 0.5; 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.gridx = 0; 
    c.gridy = 2; 
    pane.add(button, c); 

    button = new JLabel("Item"); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.weightx = 0.5; 
    c.gridx = 1; 
    c.gridy = 2; 
    pane.add(button, c); 

    button = new JLabel("Amount"); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.weightx = 0.5; 
    c.gridx = 2; 
    c.gridy = 2; 
    pane.add(button, c); 

    button = new JLabel("XYZ"); 
    button.setForeground(Color.red); 
    button.setFont(button.getFont().deriveFont(22.0f)); 
    button.setHorizontalAlignment(SwingConstants.CENTER); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.ipady = 40;  //make this component tall 
    c.weightx = 0.0; 
    c.gridwidth = 2; 
    c.gridx = 1; 
    c.gridy = 0; 
    pane.add(button, c); 

    button = new JLabel("Ph.:4466266621"); 
    button.setForeground(Color.red); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.ipady = 40;  //make this component tall 
    c.weightx = 0.0; 
    c.gridwidth = 2; 
    c.gridx = 1; 
    c.gridy = 1; 
    pane.add(button, c); 

    button = new JLabel("Reciept.: " + rn); 
    button.setForeground(Color.red); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.ipady = 40;  //make this component tall 
    c.weightx = 0.0; 
    c.gridwidth = 2; 
    c.gridx = 2; 
    c.gridy = 1; 
    pane.add(button, c); 

    JSeparator js = new JSeparator(); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.ipady = 40;  //make this component tall 
    c.weightx = 0.0; 
    c.gridwidth = 3; 
    c.gridx = 0; 
    c.gridy = 3; 
    pane.add(js, c); 
    try { 
     String sql = "SELECT * FROM order_detail"; 
     rs = st.executeQuery(sql); 
     while (rs.next()) { 
      j = 0; 
      button = new JLabel("" + rs.getInt("Quantity")); 
      c.weightx = 0.0; 
      c.fill = GridBagConstraints.HORIZONTAL; 
      c.gridx = j++; 
      c.gridy = i; 
      pane.add(button, c); 
      button = new JLabel(rs.getString("Name")); 
      c.fill = GridBagConstraints.HORIZONTAL; 
      c.weightx = 0.0; 
      c.gridx = j++; 
      c.gridy = i; 
      pane.add(button, c); 

      button = new JLabel("" + rs.getInt("cost")); 
      c.fill = GridBagConstraints.HORIZONTAL; 
      c.weightx = 0.0; 
      c.gridx = j++; 
      c.gridy = i; 
      pane.add(button, c); 
      i++; 
     } 

     js = new JSeparator(); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 40; 
     c.weightx = 0.0; 
     c.gridwidth = 3; 
     c.gridx = 0; 
     c.gridy = i++; 
     pane.add(js, c); 

     button = new JLabel("Total : " + summedval); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 1;  //aligned with button 2 
     c.gridy = i;  //third row 
     c.ipady = 0;  //reset to default 
     c.weighty = 0.5; //request any extra vertical space 
     c.insets = new Insets(5, 0, 0, 0); //top padding 
     pane.add(button, c); 

     frame.pack(); 
     frame.setVisible(true); 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
    PrinterJob job = PrinterJob.getPrinterJob(); 
    job.setJobName("frame"); 
    job.setPrintable(new Printable() { 
     public int print(Graphics pg, PageFormat pf, int pageNum) { 
      if (pageNum > 0) { 
       return Printable.NO_SUCH_PAGE; 
      } 

      Graphics2D g2 = (Graphics2D) pg; 
      g2.translate(pf.getImageableX(), pf.getImageableY()); 
      frame.paint(g2); 
      return Printable.PAGE_EXISTS; 
     } 
    }); 
    boolean ok = job.printDialog(); 
    if (ok) { 
     try { 
      job.print(); 
     } catch (PrinterException ex) { 

     } 
    } 
    frame.setVisible(false);