按照这里的垃圾神出色的例子,我整理了一个小演示,它以一种可能令人费解的方式完成了一项简单的任务。下面显示的 GUI 显示一列代表真/假值的图标。如果您单击一个图标,它会将值更改为与之相反的值。很像一个复选框,但外观不同,而且可扩展性更强(例如,我将来可以将其更改为循环遍历十几个符号,而不仅仅是两个布尔符号)。
我通过使用自定义编辑器来做到这一点,该编辑器是 JComponent 的虚拟扩展。不过,您甚至都看不到这个虚拟组件,因为一旦它接收到一个MousePressed
事件,它就会导致编辑器fireEditingStopped()
. 它工作得很好,除了我发现的一个奇怪的错误。如果您单击一个符号来更改它,然后将鼠标移动到屏幕上的其他位置并按下键盘键,它会在最后一个单击的单元格中弹出虚拟编辑器(这实际上是空白单元格),它会一直停留在那里,直到您将鼠标移入单元格或单击其他单元格。
作为对此的 hacky 修复,我在渲染器中添加了一行,它在渲染后总是取消选择整个表格。这很好用,我已经验证了整个表确实被取消了。但是,尽管如此,如果您按下键盘键,它仍会为最后编辑的单元格执行编辑器。如何防止这种行为?我的应用程序中有其他键盘侦听器,如果未选择任何单元格,我认为不应执行任何编辑器。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.imageio.ImageIO;
import javax.swing.AbstractCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class JTableBooleanIcons {
private JFrame frame;
private DefaultTableModel tableModel;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JTableBooleanIcons window = new JTableBooleanIcons();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public JTableBooleanIcons() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tableModel = new DefaultTableModel(new Object[]{"Words", "Pictures"},0);
table = new JTable(tableModel);
table.setRowHeight(40);
tableModel.addRow(new Object[]{"click icon to change", false});
tableModel.addRow(new Object[]{"click icon to change", true});
tableModel.addRow(new Object[]{"click icon to change", false});
tableModel.addRow(new Object[]{"click icon to change", true});
tableModel.addRow(new Object[]{"click icon to change", false});
tableModel.addRow(new Object[]{"click icon to change", true});
tableModel.addRow(new Object[]{"click icon to change", false});
tableModel.addRow(new Object[]{"click icon to change", true});
frame.getContentPane().add(table, BorderLayout.CENTER);
table.getColumn("Pictures").setCellRenderer(new BooleanIconRenderer());
table.getColumn("Pictures").setCellEditor(new BooleanIconEditor());
}
@SuppressWarnings("serial")
private class BooleanIconRenderer extends DefaultTableCellRenderer implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int col) {
String iconFilename = null;
if ((boolean) value) {
iconFilename = "yes.png";
value = true;
} else {
iconFilename = "no.png";
value = false;
}
try {
setIcon(new ImageIcon(ImageIO.read(BooleanIconRenderer.class.getResourceAsStream(iconFilename))));
} catch (Exception e) {
System.err.println("Failed to load the icon.");
}
if (isSelected) {
this.setBackground(Color.white);
} else {
this.setBackground(Color.white);
}
table.getSelectionModel().clearSelection();
return this;
}
}
@SuppressWarnings("serial")
private class BooleanIconEditor extends AbstractCellEditor implements TableCellEditor, MouseListener {
private BooleanComponent boolComp;
public BooleanIconEditor() {
boolComp = new BooleanComponent(false);
boolComp.addMouseListener(this);
}
@Override
public Object getCellEditorValue() {
return boolComp.getValue();
}
@Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
boolComp.setValue(! (boolean) value);
return boolComp;
}
@Override
public void mouseClicked(MouseEvent e) {
this.fireEditingStopped();
}
@Override
public void mousePressed(MouseEvent e) {
this.fireEditingStopped();
}
@Override
public void mouseReleased(MouseEvent e) {
this.fireEditingStopped();
}
@Override
public void mouseEntered(MouseEvent e) {
this.fireEditingStopped();
}
@Override
public void mouseExited(MouseEvent e) {
this.fireEditingStopped();
}
}
@SuppressWarnings("serial")
private class BooleanComponent extends JComponent {
private boolean value;
public BooleanComponent(boolean value) {
this.value = value;
}
public boolean getValue() {
return value;
}
public void setValue(boolean value) {
this.value = value;
}
}
}