Results 1 to 6 of 6
  1. #1
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Question help with printing a JTable

    I need help printing a JTable, i used the .print() method, but when i do that, it prints a empty rectangle, this is my code right here:
    Java Code:
    package jtable;
    
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.print.PrinterException;
    import java.text.MessageFormat;
    
    import javax.swing.*;
    
    public class table extends JFrame{
    
    	/**
    	 * @param args
    	 */
    	
    	JTable table;
    	public static void main(String[] args) {
    		table gui = new table();
    		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		gui.setSize(600, 200);
    		gui.setTitle("Table test");
    		gui.setVisible(true);
    		
    	}
    	
    	public table(){
    		setLayout( new FlowLayout() );
    		
    		String[] columnNames = {"Naam", "Achternaam", "Geboorte jaar"};
    		
    		Object[] [] data = {
    				{"Freek", "van der Toorn", "1993"},
    				{"Jesse", "Frerichs", "1993"}
    		};
    		
    		table = new JTable(data, columnNames);
    		table.setPreferredScrollableViewportSize(new Dimension(500, 50));
    		table.setFillsViewportHeight(true);
    		
    		JScrollPane scrollPane = new JScrollPane(table);
    		add(scrollPane);
    		
    		
    		MessageFormat header = new MessageFormat("Page {0,number,integer}");
    		try {
    			table.print(JTable.PrintMode.FIT_WIDTH, header, null);
    		} catch (PrinterException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}	
    		
    	}
    
    }
    Am i doing something wrong here?
    I hope you guys can help me

  2. #2
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default Re: help with printing a JTable

    it just shows the outlines of a rectangle, about half the width of a A4 piece of paper and at the top op the page in the center

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default Re: help with printing a JTable

    Are you printing before the table has been displayed in the GUI?

  4. #4
    L19htn1n9 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default Re: help with printing a JTable

    it says so in the code, first i create the JTable, then i add its content, and then i add the JTable to the screen and lastly i try to print it

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,328
    Rep Power
    25

    Default Re: help with printing a JTable

    But it hasn't be set visible yet. Add code to do the print AFTER the GUI is visible.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: help with printing a JTable

    Explicitly setting the table's size may also do it.
    Java Code:
    table.setSize(table.getPreferredSize());
    db

Similar Threads

  1. Replies: 9
    Last Post: 08-19-2011, 12:59 AM
  2. JTable printing with larger fonts
    By Singing Boyo in forum New To Java
    Replies: 0
    Last Post: 07-07-2011, 08:31 AM
  3. printing string backwards and printing every other
    By droidus in forum New To Java
    Replies: 22
    Last Post: 03-10-2011, 09:17 AM
  4. printing large jtable
    By j2me64 in forum Advanced Java
    Replies: 1
    Last Post: 04-27-2010, 02:46 PM
  5. Problem in printing JTable values
    By shanssat in forum AWT / Swing
    Replies: 3
    Last Post: 02-04-2009, 08:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •