Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: my jtable image dosen't appear

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Thanked 0 Times in 0 Posts

    Default my jtable image dosen't appear

    Greeting all,
    i want to convert the JTable to an Image using the method "createImage", but the jtable image doesn't appears i tried to figure out but no result at all..
    thanks for any help.

            public class JTableImageCreator extends JPanel
         public JTableImageCreator() {
           public Dimension getPreferredSize() {
            return new Dimension(250,200);
          public void paintComponent(Graphics2D g) {
           BufferedImage table = createImage(getTable());
            g.drawImage(table, 0, 0, 100, 100 , null);
        public JTable getTable()
         String[] columns = {"ColumnA", "ColumnB", "ColumnC"};
               Object[][] data = {
                  {"A", new Integer(60), new Boolean(true)},
                  {"B", new Integer(60), new Boolean(true)},
                  {"C", new Integer(60), new Boolean(true)},
                  {"D", new Integer(60), new Boolean(true)} };
        return new JTable(data,columns);
         * This method takes a JTable and returns a BufferedImage of it
         * @param table 
         * @return BufferedImage of JTable
        public static BufferedImage createImage(JTable table)
            //get the table header component
            JTableHeader tableHeaderComp = table.getTableHeader();
            //Calculate the total width and height of thr table iwth the header
            int totalWidth = tableHeaderComp.getWidth() + table.getWidth();
            int totalHeight = tableHeaderComp.getHeight() + table.getHeight();
            //create a BufferedImage object of total width and height
            BufferedImage tableImage = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
            //get the graphics object from the image
            Graphics2D g2D = (Graphics2D)tableImage.getGraphics();
            //paint the table header on image graphics
            //now translate the origin to (0, height of table header)
            g2D.translate(0, tableHeaderComp.getHeight());
            //now paint the table on the image graphics
            //return the table image
            return tableImage; 
         public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
        private static void createAndShowGUI() {
            System.out.println("Created GUI on EDT? "+
            JFrame f = new JFrame("Swing Paint Demo");
           // f.add(new JTableImageCreator());
            f.getContentPane().add(new JTableImageCreator());
    Last edited by Learner; September 22nd, 2011 at 11:05 AM.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: my jtable image dosen't appear

    That is NOT the way to do painting. I suggest you read this: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    But really, what you're looking for is probably a table renderer. They are covered here: How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: my jtable image dosen't appear

    Check the methods you override - there is one in particular which is not actually overriding the parent method (eg paintComponent). For what its worth, using the @Override annotation would have caught this at compile time.

    I do hope this example was for demonstration purposes only - as while I don't see anything wrong with drawing to the BufferedImage portion of the code, drawing the JPanel is a different matter entirely - as recreating the table, changing the table to an image, and drawing said image for every call to paintComponent is not a great way to render the JTable in a JPanel.
    Last edited by copeg; September 22nd, 2011 at 11:28 AM.

Similar Threads

  1. JTable
    By adra in forum Java Theory & Questions
    Replies: 5
    Last Post: August 30th, 2011, 07:05 AM
  2. Replies: 1
    Last Post: August 18th, 2011, 06:48 AM
  3. JTable
    By timmin in forum AWT / Java Swing
    Replies: 3
    Last Post: April 3rd, 2011, 12:10 PM
  4. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  5. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM

Tags for this Thread