简体   繁体   中英

Java JTable changing icon in cell

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.

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

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 :

JTable图像 and Here is the link to my project JTable Project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM