开发者

Display ImageIcons in jTables

I have tried using the code below to display ImageIcons in JTable. But when I run the program the jTable just displays an empty space. the p1.getPicture(); comes from the database. So basically, is there any way to modify the below code such that there is a way to开发者_StackOverflow中文版 display the imageicon in the picture???

package Tables;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.table.AbstractTableModel;

import java.util.ArrayList;
import Entity.*;

public class ProfileTable extends AbstractTableModel{
    private static final long serialVersionUID = 1L;
    private int rowCount, colCount;
    private String[] columnNames = {"ClickId", "Name", "Gender", "Website", "Hobbies","Favourite Food", "DP"};
    private Object [][] data;
    private ImageIcon [][] icons;

    public ProfileTable(ArrayList<Profile> listOfObjects) {
        rowCount = listOfObjects.size();
        colCount = columnNames.length;
        data = new Object[rowCount][colCount];
        //icons = new ImageIcon[rowCount][colCount];
        for (int i = 0; i < rowCount; i++) {
           /*Copy an ArrayList element to an instance of MyObject*/
            Profile p1 = (Profile)(listOfObjects.get(i)); 
            p1.retrieveProfile();
            data[i][0] = p1.getClickId();            
            data[i][1] = p1.getFirstName();
            data[i][2] = p1.getGender();
            data[i][3] = p1.getWebsite();
            data[i][4] = p1.getHobbies();
            data[i][5] = p1.getFood();
            icons[i][6] = new ImageIcon(p1.getPicture());
        }              
    } 
    @Override
    public int getColumnCount() {
        // TODO Auto-generated method stub
        return colCount;
    }
    @Override
    public int getRowCount() {
        // TODO Auto-generated method stub
        return rowCount;
    }
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        // TODO Auto-generated method stub
        return data[rowIndex][columnIndex];
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }
}


Override getColumnClass in the TableModel to return an ImageIcon for the 6th column:

@Override
public Class getColumnClass(int columnIndex) {
    if(columnIndex == 6){
        return ImageIcon.class;
    }
    return Object.class;
}


Use this it is more simple but set icon for all rows in column, but you can always modify it for cell you are needed:

public Class TableRender extend Jtable{
private void setComboColumn(JTable table, TableColumn Column,
            String filepath) {
        ImageIcon icon = new ImageIcon(filepath);
        sportColumn.setCellEditor(new DefaultCellEditor(icon));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setToolTipText("icon");
        Column.setCellRenderer(renderer);
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜