将图像插入 JTable

发布于 2024-11-17 02:23:33 字数 1434 浏览 4 评论 0原文

我在将图像插入 JTable 时遇到问题。我搜索了一下,发现有两种方法。您可以覆盖表模型的 getcolumnclass 或创建自定义 tablecellrender。

我选择使用以下代码制作自己的单元格渲染器:

public class MyRenderer extends DefaultTableCellRenderer {

    /*
     * @see TableCellRenderer#getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int)
     */
    public Component getTableCellRendererComponent(JTable table, Object value,
                                                 boolean isSelected, boolean hasFocus, 
                                                 int row, int column) {
      ImageIcon icon = new ImageIcon("Images/green.png");
      setText((String)value);
      setIcon(icon);
      return this;
    }
}

在填写表格字段的方法中,我使用此行添加图像。

laneTable.getColumnModel().getColumn(4).setCellRenderer(new MyRenderer());

问题是图像显示为空白。它肯定会渲染一些东西,因为当您选择该行时,所有字段都会突出显示蓝色,除了第四列(带有图像的列)仍然是白色的。我一生都无法弄清楚为什么图像没有显示。我以各种方式调整了行高,看看图像是否在那里,但在视图之外或其他什么地方。看来并非如此。

我也尝试执行覆盖方法,但我想我没有正确执行语法,因为 netbeans 给出了语法错误,而且我还没有找到关于如何执行此操作的合适示例。

谢谢。

编辑:我尝试了更简单的方法,使用下面的代码覆盖该类,但单元格仍然是空白的。我已经测试过将图像插入到标签中并且这些工作正常,因此路径是正确的。

           @Override
        public Class getColumnClass(int column)
        {

            if (column == 4)
            {

                return ImageIcon.class;
            }
            return Object.class;
            // other code; default to Object.class
        }

I'm having an issue inserting images into a JTable. Ive searched and found there are 2 ways. You can either override the getcolumnclass of the tablemodel or create your custom tablecellrender.

I chose to make my own cell renderer with this code:

public class MyRenderer extends DefaultTableCellRenderer {

    /*
     * @see TableCellRenderer#getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int)
     */
    public Component getTableCellRendererComponent(JTable table, Object value,
                                                 boolean isSelected, boolean hasFocus, 
                                                 int row, int column) {
      ImageIcon icon = new ImageIcon("Images/green.png");
      setText((String)value);
      setIcon(icon);
      return this;
    }
}

In the method where I fill in my table's fields, I am adding the images with this line..

laneTable.getColumnModel().getColumn(4).setCellRenderer(new MyRenderer());

The problem is that the images are showing up as blank white space. Its definitely rendering something because when you select the row, all of the fields highlight blue except for the 4th column(the one with the images) is still white. I cannot for the life of me figure out why the images are not showing. I've adjusted the row height in all sorts of ways to see if maybe the image was there, but out of view or something. Doesn't seem to be the case.

I also tried to do the override method but I guess I didn't do the syntax correctly because netbeans was giving syntax errors, and I have yet to find a decent example on how to do it.

Thanks.

edit: I've attempted the easier way, overriding the class using the code below but the cells are still blank. And I have tested inserting images into labels and those work, so the path is correct.

           @Override
        public Class getColumnClass(int column)
        {

            if (column == 4)
            {

                return ImageIcon.class;
            }
            return Object.class;
            // other code; default to Object.class
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味超标? 2024-11-24 02:23:33

我选择使用此代码制作自己的单元格渲染器..

为什么要重新发明轮子呢?正如您所看到的,您遇到了问题,因此只需使用默认渲染器并覆盖 getColumnClass() 方法即可。

另外,您永远不会阅读红色代码中的图像。

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableIcon extends JFrame
{
    public TableIcon()
    {
        ImageIcon aboutIcon = new ImageIcon("about16.gif");
        ImageIcon addIcon = new ImageIcon("add16.gif");
        ImageIcon copyIcon = new ImageIcon("copy16.gif");

        String[] columnNames = {"Picture", "Description"};
        Object[][] data =
        {
            {aboutIcon, "About"},
            {addIcon, "Add"},
            {copyIcon, "Copy"},
        };

        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 );
    }

    public static void main(String[] args)
    {
        TableIcon frame = new TableIcon();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }

}

更新:

也许这更容易理解:

public Class getColumnClass(int column)
{
//  return getValueAt(0, column).getClass();
    return (column == 0) ? Icon.class : Object.class;
}

I chose to make my own cell renderer with this code..

Why reinvent the wheel? As you can see you are having problems, so just use the default renderer and override the getColumnClass() method.

Also, you would NEVER read the image in the rednering code.

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableIcon extends JFrame
{
    public TableIcon()
    {
        ImageIcon aboutIcon = new ImageIcon("about16.gif");
        ImageIcon addIcon = new ImageIcon("add16.gif");
        ImageIcon copyIcon = new ImageIcon("copy16.gif");

        String[] columnNames = {"Picture", "Description"};
        Object[][] data =
        {
            {aboutIcon, "About"},
            {addIcon, "Add"},
            {copyIcon, "Copy"},
        };

        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 );
    }

    public static void main(String[] args)
    {
        TableIcon frame = new TableIcon();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }

}

Update:

Maybe this is easier to understand:

public Class getColumnClass(int column)
{
//  return getValueAt(0, column).getClass();
    return (column == 0) ? Icon.class : Object.class;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文