Java 如何将图像插入JTable单元格
有人能告诉我如何将图像添加到Java表格单元格中的正确方向吗。或者在前面创建图像图标:Java 如何将图像插入JTable单元格,java,swing,icons,jtable,Java,Swing,Icons,Jtable,有人能告诉我如何将图像添加到Java表格单元格中的正确方向吗。或者在前面创建图像图标: ImageIcon icon = new ImageIcon("image.gif"); table.setValueAt(icon, row, column); 或者,您可以尝试覆盖图标字段的渲染器: static class IconRenderer extends DefaultTableCellRenderer { public IconRenderer() { super(); } pu
ImageIcon icon = new ImageIcon("image.gif");
table.setValueAt(icon, row, column);
或者,您可以尝试覆盖图标字段的渲染器:
static class IconRenderer extends DefaultTableCellRenderer {
public IconRenderer() { super(); }
public void setValue(Object value) {
if (value == null) {
setText("");
}
else
{
setIcon(value);
}
}
JTable已经为图标提供了默认的呈现器。您只需要告诉表在给定列中存储了哪些数据,以便它可以选择适当的渲染器。这是通过重写getColumnClass(…)方法完成的: 1-将标签添加到jtable(为此创建类) 2-编码jButton以添加图像
DefaultTableModel m = (DefaultTableModel) jTable1.getModel();
jTable1.getColumn("image").setCellRenderer(new LabelRendar()); // call class
JLabel lebl=new JLabel("hello");
lebl.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/bslogo120.png"))); // NOI18N
m.addRow(new Object[]{"", "","",lebl});
我创建了自己的类来实现TableCellRenderer。我可以从JLabel扩展这个类,但我更喜欢保持它的独立性,并将JLabel“label”用作类组件
public class GLabel implements TableCellRenderer{
//The JLabel that is used to display image
private final JLabel label = new JLabel();
/**
*
* @param text
* @param image
*/
public GLabel(String text, ImageIcon image) {
label.setText(text);
label.setIcon(image);
}
public GLabel(){}
public JLabel getLabel() {
return label;
}
/**
*
* @param table the JTable that is asking the renderer to draw; can be null
* @param value the value of the cell to be rendered.
* It is up to the specific renderer to interpret and draw the value.
* For example, if value is the string "true", it could be rendered as a string or it could be rendered as a check box that is checked.
* null is a valid value
* @param isSelected true if the cell is to be rendered with the selection highlighted; otherwise false
* @param hasFocus if true, render cell appropriately. For example, put a special border on the cell, if the cell can be edited, render in the color used to indicate editing
* @param row the row index of the cell being drawn. When drawing the header, the value of row is -1
* @param column the column index of the cell being drawn
* @return
*/
@Override
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
GLabel gLabel = (GLabel)value;
return (Component) gLabel.getLabel();
}
}
我创建了一个新的DefaultTableModel对象。我重写getColumnClass()方法以在运行时传递适当的类
private final DefaultTableModel tblmodel = new DefaultTableModel() {
/**
* This method is called by table cell renderer.
* The method returns class of the cell data. This helps the renderer to display icons and
* other graphics in the table.
*/
@Override
public Class getColumnClass(int column)
{
for(int i = 0; i < tblmodel.getRowCount(); i++)
{
//The first valid value of a cell of given column is retrieved.
if(getValueAt(i,column) != null)
{
return getValueAt(i, column).getClass();
}
}
//if no valid value is found, default renderer is returned.
return super.getColumnClass(column);
}
};
我为GLabel类设置了默认渲染器
jtable.setDefaultRenderer(GLabel.class, new GLabel());
我创建了新的GLabel对象
GLabel glabel = new GLabel("testing", new ImageIcon("c://imagepath"));
最后,我使用TableModel的addRow(Object[]rowData)方法将GLabel添加到JTable中。另一个例子是:setIcon?定义在哪里?@Buffalo setIcon((ImageIcon)值)@gumuruh链接断开了是的,它工作了!谢谢问题:为什么设置PreferredScrollableViewPortSize行?没有它似乎可以正常工作。@StefanReich,它与在表中显示图标无关。进行打包()时,框架的大小不同。取决于您是否使用该方法的要求。@camickr啊,这是关于pack(),好的。(1-)不要向TableModel添加组件。模型应仅包含数据,并使用渲染器显示数据。(1-)不要向TableModel添加组件。模型应仅包含数据,并使用渲染器显示数据。
JTable jtable = new JTable(tblmodel);
jtable.setDefaultRenderer(GLabel.class, new GLabel());
GLabel glabel = new GLabel("testing", new ImageIcon("c://imagepath"));