gpt4 book ai didi

java - 覆盖 CellRenderer,ImageIcon 消失

转载 作者:搜寻专家 更新时间:2023-11-01 03:05:40 24 4
gpt4 key购买 nike

Hovercraft Full of Eels 已经为我提供了 TableCellRenderer,但现在:我可以毫无问题地画一个圆。我只是对原始 ImageIcons(在所有单元格内)如何被空单元格和黑色圆圈覆盖感到困惑。

上海商会:

package sccee;

import java.awt.Graphics;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class SCCEE extends JFrame {

private JTable spelbordTabel;
URL url = this.getClass().getResource("/resources/leeg.png");
ImageIcon leeg = new ImageIcon(new ImageIcon(url).getImage().getScaledInstance(100, 100, java.awt.Image.SCALE_SMOOTH));

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(() -> {
SCCEE testFrame = new SCCEE();
testFrame.setBounds(0, 0, 700, 700);
testFrame.setVisible(true);
});

}

public SCCEE() {
initComponents();
updateTableImages();
}

private void initComponents() {
spelbordTabel = new javax.swing.JTable();
spelbordTabel.setModel(new 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, null, 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[]{
"Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7"
}) {

@Override
public Class
getColumnClass(int columnIndex) {
return ImageIcon.class;
}
boolean[] canEdit = new boolean[]{false, false, false, false, false, false, false};

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit[columnIndex];
}

});
spelbordTabel.setAutoscrolls(false);
spelbordTabel.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
spelbordTabel.setOpaque(false);
spelbordTabel.setRequestFocusEnabled(false);
spelbordTabel.setRowHeight(100);
spelbordTabel.setRowSelectionAllowed(false);
spelbordTabel.setTableHeader(null);
spelbordTabel.setUpdateSelectionOnSort(false);
spelbordTabel.setVerifyInputWhenFocusTarget(false);
spelbordTabel.setBounds(0, 0, 700, 700);
spelbordTabel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 4, true));
spelbordTabel.setDragEnabled(true);
spelbordTabel.setVisible(true);
spelbordTabel.getColumn("Title 1").setCellRenderer(
new DefaultTableCellRenderer() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(30, 30, 20, 20);
}

}
);
add(spelbordTabel);
}

public void updateTableImages() {
SwingWorker<Void, Void> worker2 = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws InterruptedException {
Thread.sleep(50);
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
spelbordTabel.setValueAt(leeg, i, j);
}
}
return null;
}

};
worker2.execute();
}
}

State atm

我遗漏了什么明显的东西吗? (第一次摆弄 tablecellrenderers)。

最佳答案

我不是 100% 清楚您的实际问题是什么,但相信您的问题可以通过为您的 JTable 创建自定义表格单元格渲染器来解决,这样每​​个单元格都可以绘制所需的任何圆圈。

关于java - 覆盖 CellRenderer,ImageIcon 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23700756/

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