在jtable的某些列中显示图像

时间:2012-05-07 12:33:04

标签: java swing jtable imageicon

我正在制作一个JTable,前两列包含字符串,其余列包含图标(特别是类ImageIcon的对象)。我知道怎么做,但是我如何在1个表中混合使得某些列返回字符串而其他列返回图标?

- EDIT--
代码说明:数据是一个二维字符串数组。对于前两列,我希望它们在表格中显示为原样。对于所有其余列,只有两个可能的值,“Y”或“N”。现在我想要一个ImageIcon如果有一个“Y”就会显示,否则如果有一个“N”就把它留空。

(如果它有助于知道,我正在绘制一个比较表,如果值为“Y”,我想要显示刻度线图标,否则如果值为“N”则将单元格留空)

现在输出是这样的:
在“Y”的情况下PATH_TO_ICON(“// home // ....”)的值 “N”

的情况下为“javax.swing.ImageIcon@288e509b”
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 个答案:

答案 0 :(得分:4)

根据需要设置列类(对于每列)。如@mKorbel所述,另请参阅How to Use Tables - Concepts: Editors and Renderers

如果您无法从这些提示中对其进行排序,我建议您尽快发布SSCCE

答案 1 :(得分:4)

JTable知道Icon / ImageIcon.Class,然后不需要任何额外的努力

答案 2 :(得分:1)

这是正确的代码。

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

}