Show images in some columns in jtable

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;
        }
    }
}
+3
source share
3 answers

JTable knows Icon / ImageIcon.Class and then no extra effort is required

+4
source

( ) . @mKorbel, . - : .

, SSCCE .

+4

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

}

+1
source

All Articles