gpt4 book ai didi

java - 在 JTable 单元格中渲染 BufferedImage

转载 作者:行者123 更新时间:2023-12-01 06:45:46 27 4
gpt4 key购买 nike

我需要在一个 JTable 列中显示 BufferedImage。我重写了 JTable 方法

@Override
public Class<?> getColumnClass(int column) {
if (column == 1){
return BufferedImage.class;
}
return super.getColumnClass(column);
}

但我仍然获得对象的字符串表示形式而不是图像本身。有人知道我缺少什么吗?

最佳答案

我将使用 ImageIcons 填充需要显示图像的列,并让 getColumnClass() 方法返回 Icon.class,然后使用显示图标的 JLabel 对其进行渲染。事实上,我相信 DefaultCellRenderer 确实是一个 JLabel,因此它应该已经知道如何处理图标。

是的,模型所需要的只是知道它拥有图标。例如,下面的代码适用于下面的程序:

  DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0) {
@Override
public Class<?> getColumnClass(int column) {
if (getRowCount() > 0) {
return getValueAt(0, column).getClass();
}

return super.getColumnClass(column);
}
};

例如:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

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

public class ImageColumnTest2 {
public static final String IMAGE_SHEET_PATH = "http://speckycdn.sdm.netdna-cdn.com/"
+ "wp-content/uploads/2010/08/flag_icons_04.jpg";
public static final String[] COUNTRIES = {
"Denmark", "China", "Chile", "Canada", "Belgium", "Austria",
"Argentina", "France", "Malaysina", "Lebanon", "Korea", "Japan",
"Italy", "Ireland", "India", "Hong Kong", "Greece", "Germany"
};
public static final int COLS = 6;
public static final int ROWS = 3;
private static final String[] COL_NAMES = {"Country", "Flag"};

private JTable table = new JTable();
private JScrollPane mainPane = new JScrollPane(table);

public ImageColumnTest2() throws IOException {
DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0) {
@Override
public Class<?> getColumnClass(int column) {
if (getRowCount() > 0) {
return getValueAt(0, column).getClass();
}

return super.getColumnClass(column);
}
};
URL url = new URL(IMAGE_SHEET_PATH);
BufferedImage img = ImageIO.read(url);
int x1 = 15; // sorry about the magic numbers
img = img.getSubimage(x1, 0, img.getWidth() - 2 * x1, img.getHeight());

int y1 = 20 ; // ditto!
int w = img.getWidth() / COLS;
int h = img.getHeight() / ROWS;
for (int row = 0; row < ROWS; row++) {
int y = (row * img.getHeight()) / ROWS;
for (int col = 0; col < COLS; col++) {
int x = (col * img.getWidth()) / COLS;
BufferedImage subImg = img.getSubimage(x, y, w, h);

subImg = subImg.getSubimage(x1, 0, subImg.getWidth() - 2 * x1, subImg.getHeight() - y1);

ImageIcon icon = new ImageIcon(subImg);
String country = COUNTRIES[col + row * COLS];
Object[] rowData = {country, icon};
model.addRow(rowData);
}
}


table.setModel(model);
table.setRowHeight(((ImageIcon)model.getValueAt(0, 1)).getIconHeight());
}

public JComponent getMainComponent() {
return mainPane;
}

private static void createAndShowGui() {
ImageColumnTest2 imgColumnTest = null;
try {
imgColumnTest = new ImageColumnTest2();
} catch (MalformedURLException e) {
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}

JFrame frame = new JFrame("ImageColumnTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(imgColumnTest.getMainComponent());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 在 JTable 单元格中渲染 BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14793396/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com