Reputation: 481
I am making a JTable
where the first two columns contain strings and the rest of the columns contain icons (specifically, objects of the class ImageIcon
). I know how to do either, but how do I mix both in 1 table such that some columns return strings while others return icons?
--EDIT--
explanation for code: data is a 2D string array. For the first two columns, i want them to be displayed as-it-is in the table. For all the rest of the columns, there are only two possible values, "Y" or "N". Now i want an ImageIcon to be displayed if there is a "Y", otherwise just leave it blank if there is a "N".
(in case it helps to know, i am drawing a comparison table where i want a tick mark icon to be displayed if the value is "Y" otherwise just leave the cell empty if the value is "N")
right now the output is like this:
value of PATH_TO_ICON ("//home//....") in case of "Y"
"javax.swing.ImageIcon@288e509b" in 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;
}
}
}
Upvotes: 3
Views: 2651
Reputation: 168825
Set the column class (for each column) as needed. As mentioned by @mKorbel, see also How to Use Tables - Concepts: Editors and Renderers.
Upvotes: 4
Reputation: 481
This is the correct code.
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class MainTest extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
MainTest mt = new MainTest();
mt.doThis();
}
public void doThis(){
String PATH_TO_ICON = "//home//icon.png";
String[] headers = { "A", "B", "File1", "File2" };
String[][] data = { { "1", "abc", "Y", "N" }, { "2", "def", "Y", "Y" } };
JScrollPane scrollpane = new JScrollPane();
JTable table = new JTable();
table.setModel(new MyTableModel(data, headers, PATH_TO_ICON));
scrollpane.setViewportView(table);
// layout
this.add(scrollpane);
this.setLocationRelativeTo(null);
this.setMinimumSize(new Dimension(400,300));
this.setPreferredSize(new Dimension(600,400));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private Object[][] data;
private String[] headers;
private String PATH_TO_ICON;
public MyTableModel(String[][] data, String[] headers, String PATH_TO_ICON) {
super();
this.data = data;
this.headers = headers;
this.PATH_TO_ICON = PATH_TO_ICON;
}
@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;
}
}
}
Upvotes: 1
Reputation: 109813
JTable knows Icon / ImageIcon.Class, then then no required any additional effort
Upvotes: 4