图像未在 JTable (Java Swing) 中显示
您好,我创建了以下代码。 问题是图像未显示在 JTable 列中。
我扩展了 DefaultTableModel 并重写了 getColumnClass 方法。 有人建议这是一种方法。 有什么线索吗? 代码如下。
//package javaapplication12;
import javax.swing.*;
public class NewJFrame2 extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame2() {
initComponents();
}
public class imageTableModel extends javax.swing.table.DefaultTableModel
{
imageTableModel(Object[][] data, Object[] columnNames)
{
super(data,columnNames);
}
@Override
public Class getColumnClass(int column)
{
if (column == 0)
{
return ImageIcon.class;
}
return Object.class;
// other code; default to Object.class
}
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new imageTableModel(
new Object [][] {
{new ImageIcon("simply.jpg"), "pp"},
},
new String [] {
"image", "name"
}
));
jScrollPane1.setViewportView(jTable1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(19, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(19, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
Hi I have created the following code. The problem is the image is not displaying in the JTable column.
I extend the DefaultTableModel and override the method getColumnClass. It was suggested this was one way to do it. Any clues? the code is below.
//package javaapplication12;
import javax.swing.*;
public class NewJFrame2 extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame2() {
initComponents();
}
public class imageTableModel extends javax.swing.table.DefaultTableModel
{
imageTableModel(Object[][] data, Object[] columnNames)
{
super(data,columnNames);
}
@Override
public Class getColumnClass(int column)
{
if (column == 0)
{
return ImageIcon.class;
}
return Object.class;
// other code; default to Object.class
}
}
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new imageTableModel(
new Object [][] {
{new ImageIcon("simply.jpg"), "pp"},
},
new String [] {
"image", "name"
}
));
jScrollPane1.setViewportView(jTable1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(19, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(19, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame2().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是正确的方法,但无法正确找到您的 jpg。 更改您的代码以将其作为资源 URL 进行查找,并且应该可以正确找到它。 以下更改在我的机器上完美运行:
This is the correct way to do it, but your jpg isn't being found correctly. Change your code to look it up as a resource URL and it should find it correctly. The following change worked perfectly on my machine: