I have a scrolling Jtable in which one of the columns will either be blank or contain an Image. I can get the column to show the Image Location as a string, but when I try and render the cell as an Image Icon then the program crashes when trying to paint the table.
My code is as follows, any advice would be appreciated.
package Collection.Print;
import Collection.DataAccess;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.Component;
import java.awt.Dimension;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class PrintList extends JPanel {
private List<String[]> PrintData ;
private String ImageLoc ;
public PrintList(List<String[]> ImpData) {
setPrintData(ImpData) ;
JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(900, 350));
table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Set up column and cell rendering.
initRender(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
private void setPrintData (List<String[]> inPrintData) {
PrintData = inPrintData ;
}
private List<String[]> getPrintData () {
return PrintData ;
}
private void setCurrImage (String inImageloc) {
ImageLoc = inImageloc ;
}
private String getCurrImage () {
return ImageLoc ;
}
private void initRender(JTable table) {
MyTableModel model = (MyTableModel)table.getModel();
TextAreaRenderer textAreaRenderer = new TextAreaRenderer();
DefaultTableCellRenderer Rightrenderer = new DefaultTableCellRenderer() ;
Rightrenderer.setHorizontalAlignment(JLabel.RIGHT) ;
DefaultTableCellRenderer Leftrenderer = new DefaultTableCellRenderer() ;
Leftrenderer.setHorizontalAlignment(JLabel.LEFT);
DefaultTableCellRenderer Centrerenderer = new DefaultTableCellRenderer() ;
Centrerenderer.setHorizontalAlignment(JLabel.CENTE R);
for (int j = 0; j < PrintData.size(); j++) {
Object image = model.getValueAt(j, 2);
setCurrImage ((String)image) ;
table.getColumnModel().getColumn(2).setCellRendere r(new ImageRenderer());
image = model.getValueAt(j, 3);
setCurrImage ((String)image) ;
table.getColumnModel().getColumn(3).setCellRendere r(new ImageRenderer());
}
table.getColumnModel().getColumn(0).setPreferredWi dth(70);
table.getColumnModel().getColumn(1).setPreferredWi dth(280);
table.getColumnModel().getColumn(2).setPreferredWi dth(80);
table.getColumnModel().getColumn(3).setPreferredWi dth(80);
table.getColumnModel().getColumn(4).setPreferredWi dth(100);
table.getColumnModel().getColumn(5).setPreferredWi dth(100);
table.getColumnModel().getColumn(6).setPreferredWi dth(85);
table.getColumnModel().getColumn(7).setPreferredWi dth(50);
table.getColumnModel().getColumn(7).setCellRendere r(Rightrenderer);
table.getColumnModel().getColumn(1).setCellRendere r(textAreaRenderer);
}
class ImageRenderer extends DefaultTableCellRenderer {
JLabel lbl ;
public Component getTableCellRendererComponent( JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column){
String currImage = getCurrImage() ;
lbl = new JLabel();
lbl.setHorizontalAlignment(JLabel.CENTER);
lbl.setText((String) value);
if (value != null ||
value.toString().compareToIgnoreCase("") != 0) {
lbl.setIcon(new ImageIcon(getClass().getResource(currImage)));
}
return lbl;
}
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Date",
"Description",
"Image",
"",
"Format",
"State",
"Country",
"Value"};
private Object[][] data = setData();
//populate the data for Table
private Object[][] setData() {
List printdata = getPrintData() ;
int pds = printdata.size() ;
Object[][] data1 = new Object[pds][8] ;
for (int j = 0; j < pds; j++) {
String[] printline = new String[10] ;
printline = (String[])printdata.get(j);
data1[j][0] = printline[6] ;
if (printline[10].compareToIgnoreCase("") == 0 ) {
data1[j][1] = printline[1] ;
} else {
data1[j][1] = printline[1] + ", " + printline[10];
}
//Get the full path to the Images
try {
if (Integer.parseInt(printline[7]) != 0 ) {
data1[j][2] = DataAccess.getImageLoc(Integer.parseInt(printline[7]));
} else {
data1[j][2] = null ;
}
if (Integer.parseInt(printline[8]) != 0 ) {
data1[j][3] = DataAccess.getImageLoc(Integer.parseInt(printline[8]));
} else {
data1[j][3] = null ;
}
} catch (Exception e) {
System.out.println("Exception " +
e.getStackTrace());
}
data1[j][4] = printline[2] ;
data1[j][5] = printline[3] ;
data1[j][6] = printline[4] ;
data1[j][7] = printline[9] ;
}
return data1 ;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int b, int c) {
return getValueAt(b, c).getClass();
}
}
}