package dmaze2;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Dmaze2 extends JPanel
{
JTable jt;
public Dmaze2()
{
String[] columns = {"1","2","3","4","5","6","7","8"};
Object[][] table={{"f","f","f","f","f","f","f","f"},
//if this table is string makes problem to add picture
{"f","f","f","f","f","f","f","f"},
{"f","f","f","f","f","f","f","f"},
{"f","f","f","f","f","f","f","f"},
{"f","f","f","o","f","f","f","f"},
{"f","f","f","f","f","f","f","f"},
{"f","f","f","f","f","f","f","f"},
{"f","f","f","f","f","f","f","f"}};
int num=0;
ImageIcon Icon = new ImageIcon("x.png");
//i have the image in all files of the project to be sure it finds it
for (int i = 0; i < 8 ; i++)
{
int a=1;
for (int j = 0; j<7 && a<8; j++,a++)
{
if(table[i][j]=="f" && table[i][a]=="f")
{
num=num+1;
table[i][j]=Icon;
//if i try to enter the image here it will show it as x.png (as string) instead of the actual picture
table[i][a]="u";
}
}
//int b=1;
for (int j = 0; j<8 && i<7; j++)
{
if(table[i][j]=="f" && table[i+1][j]=="f")
{
num=num+1;
table[i][j]="u";//we put the block used
table[i+1][j]="u";
}
}
System.out.println("");
}
jt = new JTable(table,columns);
{
}
jt.setPreferredScrollableViewportSize(new Dimension(350,363));
jt.setFillsViewportHeight(true);
JScrollPane jps = new JScrollPane(jt);
add(jps);
}
public static void main(String[] args)
{
JFrame jf = new JFrame();
Dmaze2 t = new Dmaze2();
jf.setTitle("Depth First Search");
jf.setSize(500, 500);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(t);
}
}

- 5,826
- 6
- 51
- 55
-
1The title should be small summed up question. In the description you can elaborate your problem. – mbx May 25 '14 at 16:28
1 Answers
You need to @Override
the getColumnClass()
of the table's XxxTableModel
. If you don't the renderer will render the column as the Object.toString()
. See more at Concepts: Editors and Renderers
DefaultTableModel model = new DefaultTableModel(tableData, columns) {
@Override
public Class<?> getColumnClass(int column) {
switch(column) {
case 1: return ImageIcon.class; // or whichever column you want
default: return String.class;
}
}
};
JTable table = new JTable(model);
Side Notes:
Have a look at How Do I Compare Strings in Java
Set your frame visible after adding all your components
Swing apps should be run on the Event Dispatch Thread. See more at Initial Threads
You may want to read your image files from the class path if the images are resources of your application. Passing a String path to the
ImageIcon
signifies a read from the local file system. At time of deployment, the path you use will no longer be valid. See the answers from this question and this question for more details on how you can accomplish this task of reading from the class path and embedding your resources.

- 1
- 1

- 205,037
- 37
- 486
- 720
-
Its not working still , i try something similar to add background color as well with the tablecellrenderer. – user3535878 May 25 '14 at 16:22
-
1. You don't need to do anything with the renderer. The default one takes care of it for you, as long as you correctly override the `getColumnClass()`. 2. With the code above, you still need to set the model for the table. You can just pass the model to the table constructor. 3. Make sure the column you want the image to be in, is the `case` column number. Remember column indicies are 0 based – Paul Samsotha May 25 '14 at 16:26
-
Oh and 4. You need to make sure you are adding the ImageIcon as the data. – Paul Samsotha May 25 '14 at 16:38
-
Thanks for the answers , i try everything you said but still nothing. – user3535878 May 25 '14 at 17:12
-
What exactly is it that you are trying to achieve? You haven't said much in your post. – Paul Samsotha May 25 '14 at 17:13
-
Basically the table represents a chessboard 8x8 , and i want to put pictures of dominoes in each cell , so it looks like a checkboard/table with pictures/dominoes on it. The value table[i][j]="f" shows that that cell is free/unused and be placing the picture or "u" (means used table cell) so the next time inside the for loop it will not place another Picture on the picture(or replace "u" again with another "u"). – user3535878 May 25 '14 at 17:27
-
Check your string comparisons. See the link I posted. Also use the model to set the value. It will automatically change the image after you change it. Use `setValueAt()` to change from ImageIcon back to whaever – Paul Samsotha May 25 '14 at 17:31
-
Basically for each cell always have an ImageIcon. For the `getColumnClass()` forget the switch. Just simply return `ImageIcon.class`. Have two types of images: one for each possible state. When you need to change one cell just use `model.setVlueAt(differenImage, row, col);` – Paul Samsotha May 25 '14 at 17:40