How to Print entire Jtable

hi

I have used Jtable.print() command to print the JTAble but problem is that it is printing only that data which is visible row wise i.e.

if in a row one column's data is shown like

nam...

then it is printing like that only

i want to know how to print all data irrespective of how it is viewable on screen in table

please help me out

its urgent

thanks

[415 byte] By [@34345a] at [2007-11-27 11:02:09]
# 1

package table;

/*

* PrintTableDemo.java

*/

import java.awt.*;

import java.awt.event.*;

import java.awt.print.*;

import javax.swing.*;

import javax.swing.table.*;

public class PrintTableDemo extends JFrame {

private JButton btPrint;

private JTable table;

private JTable tableToPrint;

public PrintTableDemo() {

super("PrintTableDemo");

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setSize(190,150);

setLocationRelativeTo(null);

table = new JTable();

btPrint = new JButton();

table.setModel(new DefaultTableModel(

new Object [][] {{"this should print entirely", null}},

new String [] {"Title 1", "Title 2"}

) {

Class[] types = new Class [] {String.class, String.class};

boolean[] canEdit = new boolean [] {false, false};

public Class getColumnClass(int columnIndex) {

return types [columnIndex];

}

public boolean isCellEditable(int rowIndex, int columnIndex) {

return canEdit [columnIndex];

}

});

getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);

btPrint.setText("Print...");

btPrint.addActionListener(new ActionListener() {

public void actionPerformed(final ActionEvent evt) {

btPrintActionPerformed(evt);

}

});

getContentPane().add(btPrint, BorderLayout.NORTH);

}

private void btPrintActionPerformed(final ActionEvent evt) {

tableToPrint = new JTable(table.getModel());

tableToPrint.setSize(500,1500);

tableToPrint.getColumnModel().getColumn(0).setWidth(200);

tableToPrint.getColumnModel().getColumn(1).setWidth(200);

try {

tableToPrint.print();

} catch (PrinterException ex) {

ex.printStackTrace();

}

}

public static void main(final String args[]) {new PrintTableDemo().setVisible(true);}

}

Andre_Uhresa at 2007-7-29 12:41:27 > top of Java-index,Desktop,Core GUI APIs...
# 2

thanks for this

but can you tell me how not to fix it rather ir should set automatically acc to data

can it be possible any wrap up of text is available or naot

and wats the use of this

{

Class[] types = new Class [] {String.class, String.class};

boolean[] canEdit = new boolean [] {false, false};

public Class getColumnClass(int columnIndex) {

return types [columnIndex];

}

public boolean isCellEditable(int rowIndex, int columnIndex) {

return canEdit [columnIndex];

}

because if i m anot using these statements then also it is working

@34345a at 2007-7-29 12:41:27 > top of Java-index,Desktop,Core GUI APIs...
# 3

> and wats the use of this

> {

> Class[] types = new Class []

> {String.class, String.class};

> boolean[] canEdit = new boolean [] {false,

> false};

> public Class getColumnClass(int

> columnIndex) {

>return types [columnIndex];

> public boolean isCellEditable(int

> rowIndex, int columnIndex) {

>return canEdit [columnIndex];

read the api.

getColumnClass(int columnIndex) /*Returns the most specific superclass for all the cell values in the column.*/

isCellEditable(int rowIndex, int columnIndex) /*Returns true if the cell at rowIndex and columnIndex is editable.*/

Yannixa at 2007-7-29 12:41:27 > top of Java-index,Desktop,Core GUI APIs...
# 4

> thanks for this

> but can you tell me how not to fix it rather ir

> should set automatically acc to data

> can it be possible any wrap up of text is available

> or not

You must do your own calculation routine. Maybe this thread can give you some hints although it is meant to serve for visible JTable:

http://forum.java.sun.com/thread.jspa?forumID=54&threadID=527301

Line wrapping does not seem to be available in the JTable print function.

Andre_Uhresa at 2007-7-29 12:41:27 > top of Java-index,Desktop,Core GUI APIs...