提问人:Rafael Bueno 提问时间:11/12/2023 最后编辑:Rafael Bueno 更新时间:11/12/2023 访问量:38
Java Swing JTable(表格单元格编辑器/渲染器问题)
Java Swing JTable (Table Cell Editor/Renderer Problem)
问:
我找不到问题发生的原因。
我的程序有 3 个 JTables,其中创建为 JLabel 的按钮将调用特定方法。
当我传递 TblBotao 对象时,问题发生了,但是当它们在 JPanel 构造函数中实例化时,问题不会发生。
你能帮我告诉我我的代码有什么问题吗?
public interface IClique {
void clicar();
}
public class TblBotao extends JLabel {
public TblBotao(String titulo, String icone, IClique clique) {
setOpaque(false);
setToolTipText(titulo);
setIcon(new FlatSVGIcon(icone, 14, 14));
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
clique.clicar();
}
});
}
}
//How objects are instantiated.
final var BOTOES = new TblBotao[] {
new TblBotao("Editar", "img/icons/util/edit.svg", new IClique() {
@Override
public void clicar() {
gerenciarCliente(TipoAcao.EDITAR);
}
}),
new TblBotao("Apagar", "img/icons/util/delete.svg", new IClique() {
@Override
public void clicar() {
gerenciarCliente(TipoAcao.APAGAR);
}
})
};
//Adding the buttons
int col = tblCliente.getColumnCount() - 1;
tblCliente.getColumnModel().getColumn(col).setCellRenderer(new TblCelulaRenderer(BOTOES));
tblCliente.getColumnModel().getColumn(col).setCellEditor(new TblCelulaEditor(BOTOES));
public class TblPanel extends JPanel {
public TblPanel(TblBotao[] botoes) {
setLayout(new FlowLayout(FlowLayout.CENTER, 5, 0));
// This way there are problems.
for (TblBotao botao : botoes) {
add(botao);
}
// So there's no problem
//add(new TblBotao("Editar", "img/icons/util/edit.svg", null));
//add(new TblBotao("Apagar", "img/icons/util/delete.svg", null));
}
@Override
public boolean isFocusable() {
return false;
}
}
public class TblCelulaRenderer extends DefaultTableCellRenderer {
private final TblBotao[] botoes;
public TblCelulaRenderer(TblBotao[] botoes) {
this.botoes = botoes;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
TblPanel panel = new TblPanel(botoes);
panel.setBackground(isSelected ? table.getSelectionBackground() : c.getBackground());
return panel;
}
}
public class TblCelulaEditor extends DefaultCellEditor {
private final TblBotao[] botoes;
public TblCelulaEditor(TblBotao[] botoes) {
super(new JTextField());
this.botoes = botoes;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
TblPanel panel = new TblPanel(botoes);
panel.setBackground(table.getSelectionBackground());
setClickCountToStart(1);
return panel;
}
}
查看 2 种表单的行为,包括有问题和没有问题。
// In the constructor of the TblPanel class, if it is
// used this way, the problem with the first animation happens.
for (TblBotao botao : botoes) {
add(botao);
}
// Now if it is implemented this way, the problem does not happen,
// and it works perfectly, as in the second animation.
add(new TblBotao("Editar", "img/icons/util/edit.svg", null));
add(new TblBotao("Apagar", "img/icons/util/delete.svg", null));
当选择单元格时,内容消失了,这是怎么回事? 我无法理解,因为当在 JPanel 构造函数中实例化时,它们正常工作。
答: 暂无答案
评论