Reputation: 1119
This has been asked a million times it appears, but I must be feeling especially dense tonight because I'm still having trouble. My first question is this, when I call
ImageIcon icon = new ImageIcon(getClass().getResource("images/x.jpg"));
where is it looking for the images folder? I've tried making it a folder under my project and under src. What am I missing? I'm using Eclipse. As you've probably guessed already, I haven't done much Java.
What I really want to do is to set the the first column in a table to an initial icon and then allow the user to double click on it and change the icon. Could someone be so kind as to gently push (or violently shove) me in the right direction? Do I need my own renderer class?
class MyRenderer extends DefaultTableCellRenderer {
....
When someone double clicks on the row I want to change the icon to y.jpg.
Edited Thanks for the help. Another dumb question. Should I see the icon when I add a row like this?
DefaultTableModel dm = (DefaultTableModel)tblNews.getModel();
ImageIcon icon = new ImageIcon(getClass().getResource("/x.jpg"));
dm.addRow(new Object[]{icon, "Text"});
I see the filename of the icon, but not the icon itself.
Upvotes: 3
Views: 2670
Reputation: 24626
In order to make images folder in your project, you need to first Right-Click your Project, and then Select Source Folder
(not Folder), then name this Source Folder as images
. Now manually add your Images to this folder by moving through the File System
. Once done, go back to your Eclipse, Refresh
your project, you be able to see your images
Source Folder
in the Project Tree
.
Now in order to access the images write this for your ImageIcon
:
ImageIcon icon = new ImageIcon(getClass().getResource("/x.jpg"));
Do remember the first Forward Slash before your actual image inside the images Source Folder
. Now Run your project and check your bin folder
, your image will be automatically added to this area.
Try this code, I had tested it and it is working flawless. I can see Images inside the JTable too, with this code.
package jtable;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableIcon extends JFrame
{
public TableIcon()
{
ImageIcon backIcon = getImage("/images/bac.png");
ImageIcon exitIcon = getImage("/images/exit.png");
ImageIcon forwardIcon = getImage("/images/forward.png");
String[] columnNames = {"Picture", "Description"};
Object[][] data =
{
{backIcon, "BACK"},
{exitIcon, "EXIT"},
{forwardIcon, "FORWARD"},
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable( model )
{
// 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();
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
}
private ImageIcon getImage(String path)
{
java.net.URL url = getClass().getResource(path);
if (url != null)
return (new ImageIcon(url));
else
{
System.out.println(url);
return null;
}
}
public static void main(String[] args)
{
TableIcon frame = new TableIcon();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
Here is the output :
and Here is the link to my project JTable Project
Upvotes: 3
Reputation: 109813
1) your ImageIcon could be placed for
(new ImageIcon(getClass().getResource("images/x.jpg"));
)
src
MyPackage
- MyClass.java
MyPackage/images
- x.jpg
more Packaging in Java
2) JTable knows Icon / ImageIcon as Object, then there no reason set for Icon in the Renderer
Upvotes: 3