开发者

Printing a Jtable as Table into file

开发者 https://www.devze.com 2023-02-09 06:53 出处:网络
I was in the process of creating an application for Finance Management.So I am trying to get the final job getting the summary printed.

I was in the process of creating an application for Finance Management.So I am trying to get the final job getting the summary printed. Ths summary page consists of different tables and each followed by two labels and text field. So it would be great if someone can help me with some light into this.

All I want is print a populated Jtable onto file as it is.

I am not an expert with Java and Im using Netbeans for coding the gui.

Also regarding the fileformat i think it has to .rtf for supporting lines etc.Hope there wont be difficulty in creating rtf file from this.

Ps: Im not really particular with format .rtf I just want a format which can support Columns and Tables.

One more thing I wanted to add is that I want to create receipts from custom templates.I was thinking of using .rtf files as templates and adding neccessary values to the file. But after editing the file in java, it doesnt seem to be looking fine. It would be better if someone could suggest me a suitable format

Edit:

Ive managed to get a readymade code for printing jtable to pdf and it's working well and good. Code is as follows

Docu开发者_开发问答ment document = new Document(PageSize.A4); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\Users\arun\Desktop\jTable.pdf"));

  document.open();

  PdfContentByte cb = writer.getDirectContent();

  cb.saveState();
  Graphics2D g2 = cb.createGraphicsShapes(500, 500);

  Shape oldClip = g2.getClip();
  g2.clipRect(0,0, 500, 500);

  table.print(g2);
  g2.setClip(oldClip);

  g2.dispose();
  cb.restoreState();
  JOptionPane.showMessageDialog(null,"done","done",JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
  System.err.println(e.getMessage());
}
document.close();

But the table is being printed close to the left side of page.I wanted to leave a left margin and also heading of each table column is to be printed.

I tried document.setmargins() .But it is not working. Regarding the headings of column, I have not clue.


You can pull out the data by retrieving the TableModel from the JTable via jTable.getModel(), iterate over the contents using model.getRowCount(), model.getColumnCount() and model.getValueAt(row, col), and finally print the values to an ordinary FileWriter if you don't want any 3rd party tools.

See JTable, TableModel, FileWriter, BufferedWriter in the API docs.


  1. A third party tool like iText will be helpful in creating rtf files. Especially if you want to create tables.
  2. I would suggesting writing the data from the classes that contain your model rather than trying to pull it out of the GUI (JTable).
  3. I highly recommend against using a GUI Builder to generate applications when you are not already familiar with Swing. (Even then I recommend against it, just not as strongly).
0

精彩评论

暂无评论...
验证码 换一张
取 消