views:

873

answers:

1

Hi I have created the following code. The problem is the image is not displaying in the JTable column.

I extend the DefaultTableModel and override the method getColumnClass. It was suggested this was one way to do it. Any clues? the code is below.

//package javaapplication12;

import javax.swing.*;

public class NewJFrame2 extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public NewJFrame2() {
        initComponents();
    }


    public class imageTableModel extends javax.swing.table.DefaultTableModel

   {
       imageTableModel(Object[][] data, Object[] columnNames)
       {
           super(data,columnNames);
       }

@Override
        public Class getColumnClass(int column)
         {

            if (column == 0)
         {

                return ImageIcon.class;
        }
            return Object.class;
    // other code; default to Object.class
       }
    }


    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new imageTableModel(
            new Object [][] {
                {new ImageIcon("simply.jpg"), "pp"},

            },
            new String [] {
                "image", "name"
            }
        ));
        jScrollPane1.setViewportView(jTable1);


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame2().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration

}
+1  A: 

This is the correct way to do it, but your jpg isn't being found correctly. Change your code to look it up as a resource URL and it should find it correctly. The following change worked perfectly on my machine:

    jTable1.setModel(new imageTableModel(
        new Object [][] {
            {new ImageIcon(getClass().getClassLoader().getResource("simply.jpg")), "pp"},

        },
        new String [] {
            "image", "name"
        }
    ));
MrWiggles
You are right....thanks MrWiggles
Feel free to mark this as the correct answer :)
MrWiggles