Question

I want to apply a renderer on a cell of my JTable, to do so I created a class named myRenderer :

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class MyRenderer extends DefaultTableCellRenderer {

public Component getTableCellRendererComponent(JTable table, ImageIcon icon) {

setIcon(icon);
return this;
}
}

And I use this piece of code to apply the renderer on the cell :

MyRenderer renderer = new MyRenderer();

renderer.getTableCellRendererComponent(table, icon);

table.getColumnModel().getColumn(6).setCellRenderer(renderer);

The problem is that the rebderer is applied on all the cells in the column 6, and I want it to be applied on one cell only (row/column) but I don't know how to do so ?

Thanks in advance

Was it helpful?

Solution

Besides the fact that you are not even properly overriding the getTableCellRendererComponent method, you don't even need a custom renderer to display an image in a column

From How to Use Tables. Here's a list of types with default pre-configured renderers

  • Boolean — rendered with a check box.
  • Number — rendered by a right-aligned label.
  • Double, Float — same as Number, but the object-to-text translation is performed by a NumberFormat instance (using the default number format for the current locale).
  • Date — rendered by a label, with the object-to-text translation performed by a DateFormat instance (using a short style for the date and time).
  • ImageIcon, Icon — rendered by a centered label.
  • Object — rendered by a label that displays the object's string value.

So you can put add an ImageIcon to the table and it will be rendered as such, given you properly override the getColumnClass()

Also from tutorial:

To choose the renderer that displays the cells in a column, a table first determines whether you specified a renderer for that particular column. If you did not, then the table invokes the table model's getColumnClass method, which gets the data type of the column's cells. Next, the table compares the column's data type with a list of data types for which cell renderers are registered

So say you have a DefaultTableModel with three columns and you want an ImageIcon in the last column. You would do something like this

DefaultTableModel model = new DefaultTableModel(...) {
    @Override
    public Class<?> getColumnClass(int column) {
        switch (column) {
            case 2: return ImageIcon.class;
            default: return String.class
        }
    }
};
JTable table = new JTable(model);

Then by just adding an ImageIcon to the third column, it will be rendered as such

String colOneDate = "Data";
String colTwoData = "Data";
ImageIcon colThreeIcon = new ImageIcon(...);
model.addRow(new Object[] { colOneData, colTwoData, colThreeIcon });

You will probably also want to set the column widths and height accordingly, to the size of the image. For that you can see any of these questions

OTHER TIPS

You can use getTableCellRendererComponent function in as described below. The icon, row and coulmn to display icon, you can set from out side using a setter methode

import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;


  public class MyRenderer extends DefaultTableCellRenderer {

public MyRenderer() {
    // TODO Auto-generated constructor stub
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
            row, column);

    int neededRow=0; // set the needed row here in which the icon to be dispayed
    int neededcolumn=0; // set the needed column here in which the icon to be dispayed

    if(row==neededRow && column==neededcolumn)
    {
        setIcon(icon);
    }

    return this
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top