How to Insert Image into Jtable Cell

How to render an image to a JTable cell

Besides the fact that you are not even properly overriding the getTableCellRendererComponent method, you don't even need a custom renderer to display an image in a column

From How to Use Tables. Here's a list of types with default pre-configured renderers

  • Boolean — rendered with a check box.
  • Number — rendered by a right-aligned label.
  • Double, Float — same as Number, but the object-to-text translation is performed by a NumberFormat instance (using the default number format for the current locale).
  • Date — rendered by a label, with the object-to-text translation performed by a DateFormat instance (using a short style for the date and time).
  • ImageIcon, Icon — rendered by a centered label.
  • Object — rendered by a label that displays the object's string value.

So you can put add an ImageIcon to the table and it will be rendered as such, given you properly override the getColumnClass()

Also from tutorial:

To choose the renderer that displays the cells in a column, a table first determines whether you specified a renderer for that particular column. If you did not, then the table invokes the table model's getColumnClass method, which gets the data type of the column's cells. Next, the table compares the column's data type with a list of data types for which cell renderers are registered

So say you have a DefaultTableModel with three columns and you want an ImageIcon in the last column. You would do something like this

DefaultTableModel model = new DefaultTableModel(...) {
@Override
public Class<?> getColumnClass(int column) {
switch (column) {
case 2: return ImageIcon.class;
default: return String.class
}
}
};
JTable table = new JTable(model);

Then by just adding an ImageIcon to the third column, it will be rendered as such

String colOneDate = "Data";
String colTwoData = "Data";
ImageIcon colThreeIcon = new ImageIcon(...);
model.addRow(new Object[] { colOneData, colTwoData, colThreeIcon });

You will probably also want to set the column widths and height accordingly, to the size of the image. For that you can see any of these questions

How to display a picture in a JTable?

You need to override the getColumnClass(int col) method in your TableModel so that it returns ImageIcon.class for the columns you want to display an ImageIcon, and assign the ImageIcon directly to fila[2]:

public void loading() {
try {
String[]title = {"First Name","Last Name","Picture"};
String sql="select * from users";
model = new DefaultTableModel(null,title){
@Override
public Class<?> getColumnClass(int column) {
if (column==2) return ImageIcon.class;
return Object.class;
}
}
st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
Object[]fila = new Object[4];
while(rs.next()){
fila[0] = rs.getString("fna");
fila[1] = rs.getString("lna");
fila[2] = new ImageIcon(rs.getBytes("pic"));
model.addRow(fila);
}
tbl.setModel(model);
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}

Can't add image using ImageIcon to jTable cell

You need to override getColumnClass() on the table model, and return ImageIcon.class for the column with the ImageIcon. If you don't, the renderer will show the toString(), as the default column class type will be Object. See How to use Tables: Editors and Renderers.

For example

ImageIcon icon=new ImageIcon(getClass().getResource("exit.png"));
String[] columns={"Page No","Chapter","Image"};
Object[][] rows={{1,4,icon},{2,7,icon}};

DefaultTableModel model = new DefaultTableModel(rows, columns) {
@Override
public Class<?> getColumnClass(int column) {
switch(column) {
case 0:
case 1: return Integer.class;
case 2: return ImageIcon.class;
default: return Object.class;
}
}
};
JTable table = new JTable(model);


Related Topics



Leave a reply



Submit