当用户更改图像列的列宽时,如何让JTable自动调整绑定的ImageIcons的大小?
答案 0 :(得分:1)
“当用户更改图像列的列宽时,如何让JTable自动调整绑定的ImageIcons?”
这是一个用于拉伸图像的辅助类它只是一个绘制Image的扩展JPanel。您需要将Image
传递给它
它的基本功能是将图像绘制到面板的宽度和高度。因此,稍后在渲染器中使用它时,它会在显示区域拉伸时自动调整大小
import java.awt.*;
import javax.swing.*;
public class ImageViewer extends JPanel {
private java.awt.Image image;
private int xCoordinate;
private boolean stretched = true;
private int yCoordinate;
public ImageViewer() {
}
public ImageViewer(Image image) {
this.image = image;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
if (isStretched()) {
g.drawImage(image, xCoordinate, yCoordinate,
getSize().width, getSize().height, this);
} else {
g.drawImage(image, xCoordinate, yCoordinate, this);
}
}
}
public java.awt.Image getImage() {
return image;
}
public void setImage(java.awt.Image image) {
this.image = image;
repaint();
}
public boolean isStretched() {
return stretched;
}
public void setStretched(boolean stretched) {
this.stretched = stretched;
repaint();
}
public int getXCoordinate() {
return xCoordinate;
}
public void setXCoordinate(int xCoordinate) {
this.xCoordinate = xCoordinate;
repaint();
}
public int getYCoordinate() {
return yCoordinate;
}
public void setYCoordinate(int yCoordinate) {
this.yCoordinate = yCoordinate;
repaint();
}
}
我还使用了这个自定义单元格渲染器,我使用ImageViewer
类
import bookclasses.ImageViewer;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class MyImageCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean isFocused, int row, int column) {
Image image = ((ImageIcon) value).getImage();
ImageViewer imageViewer = new ImageViewer(image);
return imageViewer;
}
}
这是一个示例程序,我使用渲染器类
import java.awt.BorderLayout;
import java.util.GregorianCalendar;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TestTableCellRendererDemo extends JApplet {
private String[] columnNames
= {"Title", "Copies Needed", "Publisher", "Date Published",
"In-stock", "Book Photo"};
private ImageIcon intro1eImageIcon = new ImageIcon("icons/stacko/stackoverflow5.png");
private ImageIcon intro2eImageIcon = new ImageIcon("icons/stacko/stackoverflow5.png");
private ImageIcon intro3eImageIcon = new ImageIcon("icons/stacko/stackoverflow5.png");
private Object[][] rowData = {
{"Introduction to Java Programming", 120,
"Que Education & Training",
new GregorianCalendar(1998, 1 - 1, 6).getTime(),
false, intro1eImageIcon},
{"Introduction to Java Programming, 2E", 220,
"Que Education & Training",
new GregorianCalendar(1999, 1 - 1, 6).getTime(),
false, intro2eImageIcon},
{"Introduction to Java Programming, 3E", 220,
"Prentice Hall",
new GregorianCalendar(2000, 12 - 1, 0).getTime(),
true, intro3eImageIcon},};
private MyTableModel tableModel = new MyTableModel(
rowData, columnNames);
private JTable jTable1 = new JTable(tableModel);
public TestTableCellRendererDemo() {
jTable1.setDefaultRenderer(jTable1.getColumnClass(5),
new MyImageCellRenderer());
jTable1.setRowHeight(60);
add(new JScrollPane(jTable1), BorderLayout.CENTER);
}
}
结果是带有图像的单元格,如果展开框架,图像将随之展开
所以,基本上
将我的自定义单元格渲染器设置为您想要的列
jTable1.setDefaultRenderer(jTable1.getColumnClass(5),
new MyImageCellRenderer());
jTable1.setRowHeight(60);
确保表格中的列为ImageIcon
private ImageIcon intro1eImageIcon = new ImageIcon("icons/stacko/stackoverflow5.png");
Object[] row = {
"Introduction to Java Programming",
120,
"Que Education & Training",
new GregorianCalendar(1998, 1 - 1, 6).getTime(),
false,
intro1eImageIcon
};
渲染器已调用ImageViewer
,因此您无需在任何地方自行调用它。只需确保该文件与渲染器类