3

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;
        }
    }
}
mkumar118
  • 442
  • 5
  • 12

3 Answers3

4

Set the column class (for each column) as needed. As mentioned by @mKorbel, see also How to Use Tables - Concepts: Editors and Renderers.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • thanks @andrew for your reply. i posted an SSCCE of the code. please have a look. – mkumar118 May 09 '12 at 13:04
  • That code lacks imports, and a main that displays it. It is not an SSCCE. – Andrew Thompson May 09 '12 at 23:39
  • ok, i created the SSCCE and and it works :( this is sad, because in the SSCCE everything is fine, the icon is displayed, but in the actual code, instead of the icon, the path to the icon is displayed. I double checked, everything is the same in both of these places. i know i am missing something very basic from java, but i dont know what. please help. – mkumar118 May 14 '12 at 08:05
  • ok i found my mistake. after setting the image icon (which was happening correctly), i was in some other place doing some other stuff with the cell renderer (like center the text in the column etc.), and this was the problem. removing it took care of the error. thanks! – mkumar118 May 14 '12 at 13:05
  • @mkumar this is wrong way, never to set path or load Image / ImageIcon / Icon from TableModel, load this Object before – mKorbel May 14 '12 at 13:44
4

JTable knows Icon / ImageIcon.Class, then then no required any additional effort

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

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;
    }
}

}

mkumar118
  • 442
  • 5
  • 12
  • congratulation for your answer and reply. Could you help me to implement your code, but in a defaulttable (of type javax.swing.JFrame)?? – strange_098 Apr 29 '14 at 15:12