JTable 中的图像为空白

发布于 2024-11-18 01:39:31 字数 4595 浏览 4 评论 0原文

我正在尝试将图像添加到 JTable 中的列,但当列被填充时它们显示为空白。

我正在重写 getColumnClass。这是表声明...

   laneTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null}
        },
        new String [] {
            "Lane ID", "Status", "Traffic", "Gate Arm", "Lane Indicator", "Temperature"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
        };

        @Override
        public Class getColumnClass(int column)
        {

            if (column == 4)
            {

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


    });

很明显,第 4 列是我要显示图像的列。我正在用...填充我的表。

for(int i = 0; i < 200; i++){
  ImageIcon icon = new ImageIcon("Images/test.jpg");
  laneTable.getModel().setValueAt(icon,i,4);
}

我的图像的路径是 /src/Images/test.jpg 以防我的路径名不正确... 上周我一直在处理这个问题,我无法弄清楚为什么这些图像在我的表格中一直显示为空白。

有想法吗?

编辑这就是我猜的SSCCE。不知道它是否有效。

import javax.swing.table.DefaultTableModel;
import javax.swing.*;

import java.awt.event.*;
import java.awt.*;

public class LaneGUI extends javax.swing.JApplet implements Runnable {
    private Connection connection;
private XMLParser xmlParser;
private laneData laneData;
private SiteData siteData;
private Thread connectionThread;


public void init() {        


    try {
        java.awt.EventQueue.invokeAndWait(new Runnable() {

            public void run() {                    
                initComponents();                  
              ImageIcon icon = new ImageIcon("Images/test.jpg");

               for(int i = 0; i < 200; i++){
          laneTable.getModel().setValueAt(icon,i,4);
            }

        });
    } catch (Exception ex) {
        ex.printStackTrace();
    }


}   


/** This method is called from within the init() method to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")   




// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {


    jScrollPane1 = new javax.swing.JScrollPane();
    laneTable = new javax.swing.JTable();



    laneTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null}
        },
        new String [] {
            "Lane ID", "Status", "Traffic", "Gate Arm", "Lane Indicator", "Temperature"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
        };

        @Override
        public Class getColumnClass(int column)
        {

            if (column == 4)
            {

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


    });
    laneTable.setRowHeight(100);
    jScrollPane1.setViewportView(laneTable);

    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)     
            .addContainerGap())
    );

}// </editor-fold>                        







// Variables declaration - do not modify                     

private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable laneTable;
// End of variables declaration                   
}

编辑测试了新标签中的图像,图像在标签内正确显示,但在表格内仍然是空白。

I'm trying to add an image to a column in a JTable but they appear blank when the columns are filled.

I am overriding the getColumnClass. Here is the table declaration...

   laneTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null}
        },
        new String [] {
            "Lane ID", "Status", "Traffic", "Gate Arm", "Lane Indicator", "Temperature"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
        };

        @Override
        public Class getColumnClass(int column)
        {

            if (column == 4)
            {

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


    });

so obviously, column 4 is my column where images are to be displayed. I am populating my table with..

for(int i = 0; i < 200; i++){
  ImageIcon icon = new ImageIcon("Images/test.jpg");
  laneTable.getModel().setValueAt(icon,i,4);
}

The path to my images is /src/Images/test.jpg in case my pathname is somehow incorrect...
Ive been dealing with this for the last week and i CANNOT figure out why these images keep showing up blank in my table.

Ideas?

edit here is what I guess is an SSCCE. No idea if it will work or not..

import javax.swing.table.DefaultTableModel;
import javax.swing.*;

import java.awt.event.*;
import java.awt.*;

public class LaneGUI extends javax.swing.JApplet implements Runnable {
    private Connection connection;
private XMLParser xmlParser;
private laneData laneData;
private SiteData siteData;
private Thread connectionThread;


public void init() {        


    try {
        java.awt.EventQueue.invokeAndWait(new Runnable() {

            public void run() {                    
                initComponents();                  
              ImageIcon icon = new ImageIcon("Images/test.jpg");

               for(int i = 0; i < 200; i++){
          laneTable.getModel().setValueAt(icon,i,4);
            }

        });
    } catch (Exception ex) {
        ex.printStackTrace();
    }


}   


/** This method is called from within the init() method to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")   




// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {


    jScrollPane1 = new javax.swing.JScrollPane();
    laneTable = new javax.swing.JTable();



    laneTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null}
        },
        new String [] {
            "Lane ID", "Status", "Traffic", "Gate Arm", "Lane Indicator", "Temperature"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
        };

        @Override
        public Class getColumnClass(int column)
        {

            if (column == 4)
            {

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


    });
    laneTable.setRowHeight(100);
    jScrollPane1.setViewportView(laneTable);

    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)     
            .addContainerGap())
    );

}// </editor-fold>                        







// Variables declaration - do not modify                     

private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable laneTable;
// End of variables declaration                   
}

edit Tested the images in a new label, the images show correctly inside a label, but are still blank inside of the table.

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

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

发布评论

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

评论(1

转身以后 2024-11-25 01:39:31

空白图像可能不是路径问题,而是渲染问题。查看您的 CellRenderer 中的该列。是使用默认渲染器还是您正在做一些特殊的事情。

尝试打印出路径以确保它进入单元格。另请尝试从 JTable 外部访问该路径,以确保根本不存在路径问题。

A blank image might not be a path issue but a rendering issue. Look at your CellRenderer for that column. Is it using the default renderer or are you doing something special.

Try just printing out the path to make sure it is getting in the cell. Also try accessing that path from outside the JTable to make sure there is not a path issue at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文