下面的ListCellRenderer不接收嵌套ComboBoxes上的单击事件。我是否需要启用某些功能?
class FilterCellRenderer implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Filter filter = (Filter)value;
JPanel filterPanel = new JPanel();
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);
filterPanel.setLayout(layout);
filterPanel.add(new JLabel(filter.getLabel()));
final List<Object> options = filter.getOptions();
if (options.size() > 1) {
JComboBox optionCombo = new JComboBox(new AbstractComboBoxModel() {
public int getSize() {
return options.size();
}
public Object getElementAt(int index) {
return options.get(index);
}
});
optionCombo.setSelectedItem(filter.getValue());
filterPanel.add(optionCombo);
}
if (isSelected) {
filterPanel.setBackground(list.getSelectionBackground());
filterPanel.setForeground(list.getSelectionForeground());
}
return filterPanel;
}
}发布于 2008-10-09 20:13:28
swing中的渲染器组件就像“橡皮图章”-they一样,只是用于渲染/绘制一个值,而不是以通常的方式添加到父容器中(只需考虑如何在多个位置添加单个组件!)。
听起来您可能需要一个编辑器,而不是渲染器(编辑器是一个完全成熟的组件,可以在任何给定的时间添加到一个位置)。如果失败,您将不得不在JList上安装MouseListener。
发布于 2008-10-11 00:06:33
因为我不需要选择行,所以我最终只需要动态地向具有自定义布局的JPanel添加和元素。允许完整的组件行为,而不必修改表。
发布于 2008-10-09 18:27:55
这有点棘手。我认为您需要用单列JTable替换JList。然后设置表格单元格编辑器和渲染器。IIRC,可能会出现丢失第一次单击(用于选择已编辑的单元格)的问题。
此外,在每次getCellRendererComponent调用之间重用组件也是一个非常好的主意。组件被用作图章,然后被丢弃。如果每次都重新创建它们,性能将会非常糟糕。
https://stackoverflow.com/questions/187687
复制相似问题