I create JTablewhere the first two columns contain rows, and the remaining columns contain icons (in particular, class objects ImageIcon). I know how to do this, but how can I combine both tables in 1 table so that some columns return rows and others return icons?
- EDIT--
explanation for the code: the data is an array of two-dimensional strings. For the first two columns, I want them to appear as-is-in the table. For all other columns, there are only two possible values: "Y" or "N". Now I want ImageIcon to be displayed if there is "Y", otherwise just leave the field blank if there is "N".
(in case this helps to find out, I draw a comparison table where I want the label icon to be displayed if the value is “Y” otherwise it will simply leave the cell empty if the value is “N”)
Right now, the output is as follows: the
value PATH_TO_ICON ("// home // ....") in the case of "Y"
" javax.swing.ImageIcon@288e509b " in the case of "N"
class MyTableModel extends AbstractTableModel {
private Object[][] data;
private String[] headers;
public MyTableModel(String[][] data, String[] headers) {
super();
this.data = data;
this.headers = headers;
}
@Override
public int getColumnCount() {
return headers.length;
}
@Override
public int getRowCount() {
return data.length;
}
@Override
public Object getValueAt(int row, int col) {
if (col < 2) {
return data[row][col];
} else {
if (data[row][col].equals("Y")) {
return new ImageIcon(PATH_TO_ICON);
} else if(data[row][col].equals("N")) {
return new ImageIcon();
} else return null;
}
}
@Override
public Class<?> getColumnClass(int col) {
if (col < 2) {
return String.class;
} else {
return ImageIcon.class;
}
}
}
source
share