I have a JTable with 2 columns. The first column is to store ImageIcons and the second one to store a String.
I want to set the second column editable but not the first one.
Full code: https://pastebin.com/7qge1PVc
Here is a sample of my code:
File[] files = chooser.getSelectedFiles(); //Image files array
String[] columnNames = {"Image", "Description"};
Object[][] data = new Object[files.length][2]; //To fill with images and descriptions
int count = 0;
for(File imatge: files){
if(accept(imatge)){
imgBanknote = new ImageIcon( new ImageIcon(imatge.getAbsolutePath()).getImage().getScaledInstance(150, 120, Image.SCALE_SMOOTH));
data[count][0] = imgBanknote;
data[count][1] = imatge;
count++;
}
}
DefaultTableModel model = new DefaultTableModel(data, columnNames){
// Returning the Class of each column will allow different
// renderers to be used based on Class
@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
@Override
public boolean isCellEditable(int row, int column){
return column != 0;
}
};
taula.setModel(model); //Set model to JTable
taula.setPreferredScrollableViewportSize(taula.getPreferredSize());
The problem is the getColumnClass
method that I use to render the image, this make the second column not editable. I have no idea how to resolve.
Solved!
The problem is data[count][1] = imatge;
.
I was adding a File in the table, and a file in a JTable isn't editable.
To resolve the problem I've replaced data[count][1] = imatge;
to data[count][1] = imatge.getName();
, now it's a String and it's editable.