Inserting an Image into a JTable

2019-09-13 03:39发布

问题:

I'm having an issue inserting images into a JTable. Ive searched and found there are 2 ways. You can either override the getcolumnclass of the tablemodel or create your custom tablecellrender.

I chose to make my own cell renderer with this code:

public class MyRenderer extends DefaultTableCellRenderer {

    /*
     * @see TableCellRenderer#getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int)
     */
    public Component getTableCellRendererComponent(JTable table, Object value,
                                                 boolean isSelected, boolean hasFocus, 
                                                 int row, int column) {
      ImageIcon icon = new ImageIcon("Images/green.png");
      setText((String)value);
      setIcon(icon);
      return this;
    }
}

In the method where I fill in my table's fields, I am adding the images with this line..

laneTable.getColumnModel().getColumn(4).setCellRenderer(new MyRenderer());

The problem is that the images are showing up as blank white space. Its definitely rendering something because when you select the row, all of the fields highlight blue except for the 4th column(the one with the images) is still white. I cannot for the life of me figure out why the images are not showing. I've adjusted the row height in all sorts of ways to see if maybe the image was there, but out of view or something. Doesn't seem to be the case.

I also tried to do the override method but I guess I didn't do the syntax correctly because netbeans was giving syntax errors, and I have yet to find a decent example on how to do it.

Thanks.

edit: I've attempted the easier way, overriding the class using the code below but the cells are still blank. And I have tested inserting images into labels and those work, so the path is correct.

           @Override
        public Class getColumnClass(int column)
        {

            if (column == 4)
            {

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

回答1:

I chose to make my own cell renderer with this code..

Why reinvent the wheel? As you can see you are having problems, so just use the default renderer and override the getColumnClass() method.

Also, you would NEVER read the image in the rednering code.

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableIcon extends JFrame
{
    public TableIcon()
    {
        ImageIcon aboutIcon = new ImageIcon("about16.gif");
        ImageIcon addIcon = new ImageIcon("add16.gif");
        ImageIcon copyIcon = new ImageIcon("copy16.gif");

        String[] columnNames = {"Picture", "Description"};
        Object[][] data =
        {
            {aboutIcon, "About"},
            {addIcon, "Add"},
            {copyIcon, "Copy"},
        };

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable( model )
        {
            //  Returning the Class of each column will allow different
            //  renderers to be used based on Class
            public Class getColumnClass(int column)
            {
                return getValueAt(0, column).getClass();
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        TableIcon frame = new TableIcon();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }

}

Update:

Maybe this is easier to understand:

public Class getColumnClass(int column)
{
//  return getValueAt(0, column).getClass();
    return (column == 0) ? Icon.class : Object.class;
}