How to Insert Image into JTable Cell - java

Can someone point me in the right direction on how to add an image into Java Table cell.

JTable already provides a default renderer for icons. You just need to tell the table what data is stored in a given column so it can choose the appropriate renderer. This is done by overriding the getColumnClass(...) method:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableIcon extends JPanel
{
public TableIcon()
{
Icon aboutIcon = new ImageIcon("about16.gif");
Icon addIcon = new ImageIcon("add16.gif");
Icon copyIcon = new ImageIcon("copy16.gif");
String[] columnNames = {"Picture", "Description"};
Object[][] data =
{
{aboutIcon, "About"},
{addIcon, "Add"},
{copyIcon, "Copy"},
};
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
// Returning the Class of each column will allow different
// renderers to be used based on Class
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};
JTable table = new JTable( model );
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Table Icon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TableIcon());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}

Either create the imageicon up front:
ImageIcon icon = new ImageIcon("image.gif");
table.setValueAt(icon, row, column);
Or you can try overriding the renderer for your icon field:
static class IconRenderer extends DefaultTableCellRenderer {
public IconRenderer() { super(); }
public void setValue(Object value) {
if (value == null) {
setText("");
}
else
{
setIcon(value);
}
}

1- add label to jtable ( create class for this)
class LabelRendar implements TableCellRenderer{
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
return (Component)value;
}
}
2- code jButton to add image
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});

I created my own class that implements TableCellRenderer. I can extend this class from JLabel, but I have preferred to keep it independent and used JLabel 'label' as a class component.
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();
}
}
I created a new DefaultTableModel object. I overrides getColumnClass() method to pass appropriate Class at runtime.
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);
}
};
I created JTable object using DefaultTableModel I created.
JTable jtable = new JTable(tblmodel);
I set default renderer for GLabel class
jtable.setDefaultRenderer(GLabel.class, new GLabel());
I created new GLabel object.
GLabel glabel = new GLabel("testing", new ImageIcon("c://imagepath"));
Finally, I used addRow(Object[] rowData) method of TableModel to add GLabel to the JTable.

Related

How can I modify this code in order for it to show image in jTable from database? [duplicate]

I am able to set the column's header but not able to set icon in all the rows of first column of JTable.
public class iconRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent(JTable table,Object obj,boolean isSelected,boolean hasFocus,int row,int column){
imageicon i=(imageicon)obj;
if(obj==i)
setIcon(i.imageIcon);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(JLabel.CENTER);
return this;
}
}
public class imageicon{
ImageIcon imageIcon;
imageicon(ImageIcon icon){
imageIcon=icon;
}
}
and below lines in my BuildTable() method.
public void SetIcon(JTable table, int col_index, ImageIcon icon){
table.getTableHeader().getColumnModel().getColumn(col_index).setHeaderRenderer(new iconRenderer());
table.getColumnModel().getColumn(col_index).setHeaderValue(new imageicon(icon));
}
How can we set it for all rows of first columns? I have tried with for loop but didnt get yet for rows to iterate to set icon. Or is there any other way?
There is no need to create a custom render. JTable already supports an Icon renderer. YOu just need to tell the table to use this renderer. This is done by overriding the getColumnClass(...) method of the table model:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableIcon extends JPanel
{
public TableIcon()
{
Icon aboutIcon = new ImageIcon("about16.gif");
Icon addIcon = new ImageIcon("add16.gif");
Icon copyIcon = new ImageIcon("copy16.gif");
String[] columnNames = {"Picture", "Description"};
Object[][] data =
{
{aboutIcon, "About"},
{addIcon, "Add"},
{copyIcon, "Copy"},
};
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
// Returning the Class of each column will allow different
// renderers to be used based on Class
public Class getColumnClass(int column)
{
switch (column)
{
case 0: return Icon.class;
default: return super.getColumnClass(column);
}
}
};
JTable table = new JTable( model );
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Table Icon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TableIcon());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
You are just using iconRenderer for the render of your header. Also set the Column's Cell Reneder to be an instance of iconRenderer as well. Call setCellRenderer on the column.
http://download.oracle.com/javase/6/docs/api/javax/swing/table/TableColumn.html#setCellRenderer(javax.swing.table.TableCellRenderer)
Side note: Java coding standards specify that class names should start with capital letters, so iconRenderer should be IconRenderer instead.
I know the post is a little old but it's never too late ...
I will post here how to insert an icon without using a DefaultTableCellRenderer class, I use this for when I will only show an icon on the screen in a simple way not very elaborate.
I do it in a simple way ... I always create some tablemodel creators in the classes that I inherit. I usually pass by parameter the list of titles and types of objects.
Method that creates the tablemodel in the upper class:
protected void createTableModel(String[] columns, Class[] types){
String[] vetStr = new String[columns.length];
boolean[] vetBoo = new boolean[columns.length];
Arrays.fill(vetStr, null);
Arrays.fill(vetBoo, false);
table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] { vetStr },
columns
) {
boolean[] canEdit = vetBoo;
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
}
Inherited class constructor:
"See that here I set the type as ImageIcon.class for the column"
.... constructor....
super("Balança");
String[] columns = {"#", "Nome", "Porta", "Padrão"};
Class[] types = {Long.class, String.class, String.class, ImageIcon.class};
**strong text**super.createTableModel(columns, types);
When I list the items on the tablemodel there I show the image.
list.forEach( obj -> {
tableModel.addRow(new Object[]{
obj.getId(),
obj.getName(),
obj.getPort(),
(obj.getId() == Global.standardScale)?
new ImageIcon(getClass().getResource("./br/com/valentin/img/accept.png")): ""
});
});

How to display progress column in Java Table

So I am creating a project which has three columns; one is a check box column the second one is string (words form a neo4j database) and the third for progress bars.
All the columns are displayed and work fine but the progress bar column is invisible as it seems. Here is some code:
//CONSTRUCTOR
public BiogramTableJSedit2Jan9()
{
//*************************************************
//* SETTING UP THE FORM *
//*************************************************
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(200,200,800,300);
setTitle("Netword Data Table");
getContentPane().setLayout(null);
//*************************************************
//********************************************
//CREATING TABLE BLOCK *
//********************************************
//ADD SCROLLPANE
JScrollPane scroll=new JScrollPane();
scroll.setBounds(70,80,600,200);
getContentPane().add(scroll);
//THE TABLE
final JTable table=new JTable();
scroll.setViewportView(table);
//THE MODEL OF THE TABLE
DefaultTableModel model=new DefaultTableModel()
{
//****************************************
//* SETTING TABLE COLUMNS BLOCK *
//****************************************
public Class<?> getColumnClass(int column)
{
switch(column)
{
case 0: // |This is the first column
return Boolean.class; // |First column is set to Boolean as it will contain check boxes
case 1: // |This is the second column
return String.class; // |Second column set to String as it will contain strings
case 2:
return JProgressBar.class; // |This is for the progress bar column (IN PROGRESS - NOT DISPLAYED YET...)
default:
return String.class; // |The table is set to String as default
}
}
};
//Create and run the query in the table
neoQuery= Q1();
resultVariable = session.run(neoQuery.get());
//ASSIGN THE MODEL TO TABLE
table.setModel(model);
model.addColumn("Select"); // |Column for check boxes
model.addColumn("Bigrams"); // |Column for Bigrams
table.getColumn("Status").setCellRenderer(new ProgressCellRender());
//**********************************************
ProgressWorker worker = new ProgressWorker(model); //HAVE AN ERROR HERE
worker.execute();
This is the progress renderer class:
//a table cell renderer that displays a JProgressBar
public class ProgressCellRender extends JProgressBar implements TableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
int progress = 0;
if (value instanceof Float) {
progress = Math.round(((Float) value) * 100f);
} else if (value instanceof Integer) {
progress = (int) value;
}
setValue(progress);
return this;
}
}
Now you can see that I am using getColumn in order to display this column but when I run it you can see the first and second but not the progress bar column. I want it to look similar to this:
I also has a progress worker class that is not fully implemented.
private static class ProgressWorker extends SwingWorker<Void, Integer>
{ //Swing worker class for updating the progress bar
private BiogramTableJSedit2Jan9 model;
private final JProgressBar progress; //declaration for progress bar
public ProgressWorker(JProgressBar model)
{
this.progress = model;
}
#Override
protected Void doInBackground() throws Exception {
return null;
}
I would appreciate if anyone can explain to me why the thirst column is not being displayed. Thanks for any replies in advance.
Because your posted code was not self-contained I set up a minimal test together
with your already posted ProgressCellRender class.
The important bugfix is in method getColumnClass of the DefaultTableModel.
It needs to return Integer.class or Float.class because only those can be
handled by your ProgressCellRender.
And of course, the actual data in that column need to be Integer or Float.
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(Main::initGUI);
}
private static void initGUI() {
JFrame frame = new JFrame("TableModel JProgressBar Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scroll = new JScrollPane();
frame.getContentPane().add(scroll);
final JTable table = new JTable();
scroll.setViewportView(table);
DefaultTableModel model = new DefaultTableModel() {
public Class<?> getColumnClass(int column) {
switch (column) {
case 0:
return Boolean.class;
case 1:
return String.class;
case 2:
return Integer.class; // !!!!
default:
return String.class;
}
}
};
table.setModel(model);
model.addColumn("Active");
model.addColumn("Name");
model.addColumn("Progress");
table.getColumn("Progress").setCellRenderer(new ProgressCellRender());
model.addRow(new Object[] { true, "aaaa", 14 });
model.addRow(new Object[] { false, "bbbbbbbb", 0 });
model.addRow(new Object[] { true, "ccccc", 2 });
frame.pack();
frame.setVisible(true);
}
}
Then the JProgressBars are rendered correctly in the table:
In your ProgessCellRender you may want to add setStringPainted(true); to get the percentage also rendered as text.

Change the font color in a specific cell of a JTable?

Before starting, I've viewed a handful of solutions as well as documentation. I can't seem to figure out why my code isn't working the way I believe it should work. I've extended DefaultTableCellRenderer but I don't believe it is being applied - that or I messed things up somewhere.
Here are the threads / websites I've looked into before posting this question:
Swing - Is it possible to set the font color of 'specific' text within a JTable cell?
JTable Cell Renderer
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
I realize the first link uses HTML to change the font color, but I would think the way I went about it should produce the same result.
To make it easier on those who want to help me figure out the issues, I've created an SSCCE.
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class TableTest {
private static final int IMPORTANT_COLUMN = 2;
public static void createAndShowGUI() {
Object[][] data = new Object[2][4];
//create sample data
String[] realRowData = { "1", "One", "1.0.2", "compile" };
String[] fakeRowData = { "2", "Two", "1.3.2-FAKE", "compile" };
//populate sample data
for(int i = 0; i < realRowData.length; i++) {
data[0][i] = realRowData[i];
data[1][i] = fakeRowData[i];
}
//set up tableModel
JTable table = new JTable();
table.setModel(new DefaultTableModel(data,
new String[] { "ID #", "Group #", "version", "Action" })
{
Class[] types = new Class[] {
Integer.class, String.class, String.class, String.class
};
boolean[] editable = new boolean[] {
false, false, true, false
};
#Override
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return editable[columnIndex];
}
});
//set custom renderer on table
table.setDefaultRenderer(String.class, new CustomTableRenderer());
//create frame to place table
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setMinimumSize(new Dimension(400, 400));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(table);
f.add(scrollPane);
f.pack();
f.setVisible(true);
}
//MAIN
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
//Custom DefaultTableCellRenderer
public static class CustomTableRenderer extends DefaultTableCellRenderer {
public Component getTableCellRenderer(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
Component c = super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
String versionVal = table.getValueAt(row, IMPORTANT_COLUMN).toString();
if(versionVal.contains("FAKE")) {
//set to red bold font
c.setForeground(Color.RED);
c.setFont(new Font("Dialog", Font.BOLD, 12));
} else {
//stay at default
c.setForeground(Color.BLACK);
c.setFont(new Font("Dialog", Font.PLAIN, 12));
}
return c;
}
}
}
My goal is to highlight any value in the version column that contains the word FAKE in a red bold text.
I've extended DefaultTableCellRenderer but I don't believe it is being applied
Some simple debugging tips:
Add a simple System.out.println(...) to the method you think should be invoked
When overriding a method, make sure you use the #Override annotation (you used it in the TableModel class, but not your renderer class).
Your problem is a typing mistake because you are not overriding the proper method:
#Override
// public Component getTableCellRenderer(...) // this is wrong
public Component getTableCellRendererComponent(...)
The override annotation will display a compile message. Try it before changing the code.
Also, your first column is NOT an Integer class. Just because it contains String representations of an Integer does not make it an Integer. You need to add an Integer object to the model.
Replace your custom table cell rendere with the below.
Explanations are in comments. Basically, you should override getTableCellRendererComponent then check for correct column (there may be other methods instead of checking header value), then set cell depending on color.
Do not forget last else block to set color to default if it is not the column you want.
//Custom DefaultTableCellRenderer
public static class CustomTableRenderer extends DefaultTableCellRenderer {
// You should override getTableCellRendererComponent
#Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
// Check the column name, if it is "version"
if (table.getColumnName(column).compareToIgnoreCase("version") == 0) {
// You know version column includes string
String versionVal = (String) value;
if (versionVal.contains("FAKE")) {
//set to red bold font
c.setForeground(Color.RED);
c.setFont(new Font("Dialog", Font.BOLD, 12));
} else {
//stay at default
c.setForeground(Color.BLACK);
c.setFont(new Font("Dialog", Font.PLAIN, 12));
}
} else {
// Here you should also stay at default
//stay at default
c.setForeground(Color.BLACK);
c.setFont(new Font("Dialog", Font.PLAIN, 12));
}
return c;
}
}

Customizing my Cell Renderer to change one cells colour?

note: this code is not mine, I have taken it from another site and i'm simply trying to modify it.
I have a JTable with a load of details however, I want it so that when I change a particular cell for the first cell to change colour. Currently this code just highlights the row when I click on it, but I want it so that if I change one of the values to another number, the name cell for example to change red. I have tried a few things (if statements) but can't seem to work it. Any help would be great.
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class CustomCellRenderer{
JTable table;
TableColumn tcol;
public static void main(String[] args) {
new CustomCellRenderer();
}
public CustomCellRenderer(){
JFrame frame = new JFrame("Creating a Custom Cell Reanderer!");
JPanel panel = new JPanel();
String data[][] = {{"Vinod","Computer","3"},
{"Rahul","History","2"},
{"Manoj","Biology","4"},
{"Sanjay","PSD","5"}};
String col [] = {"Name","Course","Year"};
DefaultTableModel model = new DefaultTableModel(data,col);
table = new JTable(model);
tcol = table.getColumnModel().getColumn(0);
tcol.setCellRenderer(new CustomTableCellRenderer());
tcol = table.getColumnModel().getColumn(1);
tcol.setCellRenderer(new CustomTableCellRenderer());
tcol = table.getColumnModel().getColumn(2);
tcol.setCellRenderer(new CustomTableCellRenderer());
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
JScrollPane pane = new JScrollPane(table);
panel.add(pane);
frame.add(panel);
frame.setSize(500,150);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class CustomTableCellRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent (JTable table,
Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(
table, obj, isSelected, hasFocus, row, column);
if (isSelected) {
cell.setBackground(Color.green);
}
else {
if (row % 2 == 0) {
cell.setBackground(Color.lightGray);
}
else {
cell.setBackground(Color.lightGray);
}
}
return cell;
}
}
}
If you know row number you want to highlight just add in the end of the getTableCellRendererComponent method
if (row==theRowNumberToHighlight && column=0) {
cell.setForeground(Color.red);
}
Assuming your table model extends AbstractTableModel, extend TableModelListener. Use the following tableChanged method to figure out when to call your renderer:
public void tableChanged(TableModelEvent e)
{
if (e.getColumn() == columnYouAreChecking && e.getFirstRow() == rowYouAreChecking && e.getLastRow() == rowYouAreChecking)
{
// Change cell color here.
}
}
This code will get called every time the data in your table changes.

How to set icon in a column of JTable?

I am able to set the column's header but not able to set icon in all the rows of first column of JTable.
public class iconRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent(JTable table,Object obj,boolean isSelected,boolean hasFocus,int row,int column){
imageicon i=(imageicon)obj;
if(obj==i)
setIcon(i.imageIcon);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(JLabel.CENTER);
return this;
}
}
public class imageicon{
ImageIcon imageIcon;
imageicon(ImageIcon icon){
imageIcon=icon;
}
}
and below lines in my BuildTable() method.
public void SetIcon(JTable table, int col_index, ImageIcon icon){
table.getTableHeader().getColumnModel().getColumn(col_index).setHeaderRenderer(new iconRenderer());
table.getColumnModel().getColumn(col_index).setHeaderValue(new imageicon(icon));
}
How can we set it for all rows of first columns? I have tried with for loop but didnt get yet for rows to iterate to set icon. Or is there any other way?
There is no need to create a custom render. JTable already supports an Icon renderer. YOu just need to tell the table to use this renderer. This is done by overriding the getColumnClass(...) method of the table model:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableIcon extends JPanel
{
public TableIcon()
{
Icon aboutIcon = new ImageIcon("about16.gif");
Icon addIcon = new ImageIcon("add16.gif");
Icon copyIcon = new ImageIcon("copy16.gif");
String[] columnNames = {"Picture", "Description"};
Object[][] data =
{
{aboutIcon, "About"},
{addIcon, "Add"},
{copyIcon, "Copy"},
};
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
// Returning the Class of each column will allow different
// renderers to be used based on Class
public Class getColumnClass(int column)
{
switch (column)
{
case 0: return Icon.class;
default: return super.getColumnClass(column);
}
}
};
JTable table = new JTable( model );
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Table Icon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TableIcon());
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
You are just using iconRenderer for the render of your header. Also set the Column's Cell Reneder to be an instance of iconRenderer as well. Call setCellRenderer on the column.
http://download.oracle.com/javase/6/docs/api/javax/swing/table/TableColumn.html#setCellRenderer(javax.swing.table.TableCellRenderer)
Side note: Java coding standards specify that class names should start with capital letters, so iconRenderer should be IconRenderer instead.
I know the post is a little old but it's never too late ...
I will post here how to insert an icon without using a DefaultTableCellRenderer class, I use this for when I will only show an icon on the screen in a simple way not very elaborate.
I do it in a simple way ... I always create some tablemodel creators in the classes that I inherit. I usually pass by parameter the list of titles and types of objects.
Method that creates the tablemodel in the upper class:
protected void createTableModel(String[] columns, Class[] types){
String[] vetStr = new String[columns.length];
boolean[] vetBoo = new boolean[columns.length];
Arrays.fill(vetStr, null);
Arrays.fill(vetBoo, false);
table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] { vetStr },
columns
) {
boolean[] canEdit = vetBoo;
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
}
Inherited class constructor:
"See that here I set the type as ImageIcon.class for the column"
.... constructor....
super("Balança");
String[] columns = {"#", "Nome", "Porta", "Padrão"};
Class[] types = {Long.class, String.class, String.class, ImageIcon.class};
**strong text**super.createTableModel(columns, types);
When I list the items on the tablemodel there I show the image.
list.forEach( obj -> {
tableModel.addRow(new Object[]{
obj.getId(),
obj.getName(),
obj.getPort(),
(obj.getId() == Global.standardScale)?
new ImageIcon(getClass().getResource("./br/com/valentin/img/accept.png")): ""
});
});

Categories

Resources