JTable中不同单元格中的不同imageIcon [英] Different imageIcon in different cells in JTable
本文介绍了JTable中不同单元格中的不同imageIcon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想我的imageIcon在每个单元格中都有不同的显示方式,但出于某种原因,当我编译它时,图像不会显示出来。它显示图像的名称,但图像本身不显示。这是一张图片。 http://i49.tinypic.com/r9ibrn.jpg
I think I got the imageIcon to show up differently in each cell but for some reason when I compile it, the images don't show up. It displays the name of the image but the image itself doesn't show up. Here is an image. http://i49.tinypic.com/r9ibrn.jpg
public class movies extends JFrame {
public movies() {
initComponents();
}
private void initComponents() {
panel = new JPanel();
logo = new JLabel();
pane = new JScrollPane();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBackground(new Color(255, 255, 204));
setResizable(false);
panel.setBackground(new Color(51, 51, 51));
panel.setPreferredSize(new Dimension(290, 75));
logo.setIcon(new ImageIcon(getClass().getResource("logo.png")));
logo.setName("logo");
logo.setRequestFocusEnabled(false);
logo.setVerifyInputWhenFocusTarget(false);
logo.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
ImageIcon icon1 = new ImageIcon("1.jpg");
ImageIcon icon2 = new ImageIcon("2.jpg");
ImageIcon icon3 = new ImageIcon("3.jpg");
String[] columnNames = {"Section 1", "Section 2"};
Object[][] data =
{
{icon1 + " Music", icon2 + " News"},
{icon2 + " Movies"},
{icon3 + " Games"},
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable( model )
{
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setBackground(new Color(255, 255, 204));
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
table.setRowHeight(50);
pane.setViewportView(table);
table.getColumnModel().getColumn(0).setResizable(false);
table.getColumnModel().getColumn(1).setResizable(false);
}
public static void main(String args[]) {
public void run() {
new movies().setVisible(true);
}
});
}
private JLabel logo;
private JScrollPane pane;
private JPanel panel;
}
推荐答案
当你调用 new ImageRenderer
构造函数时,你可以传入图像的名称(读这个)。
You can pass in the name of the image when you call the new ImageRenderer
constructor (read this).
public class Movies extends javax.swing.JFrame {
public Movies() {
initComponents();
table.getColumnModel().getColumn(1).setCellRenderer(new ImageRenderer("1.jpg"));
table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer("2.jpg"));
}
}
class ImageRenderer extends DefaultTableCellRenderer {
ImageIcon icon = null;
ImageRenderer(String iconName) {
icon = new ImageIcon(getClass().getResource(iconName));
}
}
这篇关于JTable中不同单元格中的不同imageIcon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文