简体   繁体   English

Java JTable在单元格中更改图标

[英]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. 我试过在我的项目下和src下创建一个文件夹。 What am I missing? 我错过了什么? I'm using Eclipse. 我正在使用Eclipse。 As you've probably guessed already, I haven't done much Java. 正如您可能已经猜到的那样,我没有做过多少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. 当有人双击该行时,我想将图标更改为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")); ) 1)你的ImageIcon可以放置( new ImageIcon(getClass().getResource("images/x.jpg"));

  • src SRC

    • MyPackage MyPackage的

        - MyClass.java 
    • MyPackage/images MyPackage的/图片

        - x.jpg 

more Packaging in Java 更多Java 打包

2) JTable knows Icon / ImageIcon as Object, then there no reason set for Icon in the Renderer 2) JTable将Icon / ImageIcon知道为Object,然后在Renderer中没有为Icon设置原因

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 . 要在项目中创建图像文件夹,首先需要右键单击项目,然后选择“ Source Folder (而不是“文件夹”),然后将此源文件夹命名为images Now manually add your Images to this folder by moving through the File System . 现在,通过在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 . 完成后,返回Eclipse, Refresh项目,您可以在Project Tree看到images Source Folder

Now in order to access the images write this for your ImageIcon : 现在,为了访问图像,请为您的ImageIcon写下这个:

ImageIcon icon = new ImageIcon(getClass().getResource("/x.jpg"));

Do remember the first Forward Slash before your actual image inside the images Source Folder . 请记住images Source Folder实际图像之前的第一个前斜线。 Now Run your project and check your bin folder , your image will be automatically added to this area. 现在运行您的项目并检查您的bin folder ,您的图像将自动添加到此区域。

Try this code, I had tested it and it is working flawless. 尝试这个代码,我测试了它,它完美无瑕。 I can see Images inside the JTable too, with this code. 我也可以使用此代码在JTable中看到图像。

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 这是我的项目JTable项目的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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