some help with printing two JTables in 1 page.

Hey all,

I want to print a report that will print two tables on 1 page.

the page is portrait.

now i figured out how to print the table on the page,

but i need to resize the table.

It takes up the whole width of the page.

How can i resize the table?

I tried table.setSize() and setPrefferedSize();

but they didn't work.

[375 byte] By [avdzma] at [2007-11-27 6:43:53]
# 1

Here is the code,

import java.awt.geom.RectangularShape;

import java.util.Enumeration;

import javax.swing.*;

import java.awt.*;

import java.awt.print.*;

import java.awt.event.*;

import javax.swing.event.TableColumnModelListener;

import javax.swing.event.TableModelListener;

import javax.swing.table.DefaultTableColumnModel;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableColumn;

import javax.swing.table.TableColumnModel;

import javax.swing.table.TableModel;

import java.awt.geom.Rectangle2D;

/*

* LiakasBookingPage.java

*

* Created on June 6, 2007, 11:08 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

/**

*

* @author Alexandros Vangelatos

*/

public class LiakasBookingPage extends JPanel implements Printable {

/** Creates a new instance of LiakasBookingPage */

public LiakasBookingPage(MouseListener ml) {

super(null);

this.addMouseListener(ml);

String header[] = {"Column1","Column2","Column3","Column4","Column5","Column6","Column7","Column8"};

DefaultTableModel tmodel = new DefaultTableModel(16,8);

tmodel.setColumnIdentifiers(header);

table = new JTable(tmodel);

table.setBounds(20,50,50,50);

table.setPreferredSize(new Dimension(50,50));

table.setValueAt("Hello",0,0);

table.setValueAt("Hello",2,1);

table.setValueAt("Hello",4,2);

table.setValueAt("Hello",6,3);

table.setValueAt("Hello",8,4);

table.setValueAt("Hello",10,5);

table.setValueAt("Hello",12,6);

table.setValueAt("Hello",14,7);

//add(table);

}

public int print(Graphics g , PageFormat pageFormat, int pageIndex){

if(pageIndex >=1){

return Printable.NO_SUCH_PAGE;

}

Graphics2D g2d = (Graphics2D)g;

//table.setPreferredSize(new Dimension(50,50));

//table.setMaximumSize(new Dimension(20,25));

//table.setBackground(Color.BLUE);8

g.translate(0,30);

//table.getGraphics().create(0,60,50,30);

g.setClip(0,0,500,200);

table.print(g);

return Printable.PAGE_EXISTS;

}

private JTable table;

}

avdzma at 2007-7-12 18:14:54 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hey all,

Well I figured out a way on how to control the width of the table.

The only working solution I found was to set the Maximum width size of each Column.

This is the only solution that worked, anything that i did to the table it didn't work.

If someone has a better idea please let me know.

I'll post the code later once everything is working.

avdzma at 2007-7-12 18:14:54 > top of Java-index,Desktop,Core GUI APIs...