简体   繁体   English

如何在JList中的组件上启用事件

[英]How to enable events on components in JList

I'm using custom renderer on JList, but none of components rendered are accessible. 我在JList上使用自定义渲染器,但是渲染的组件均不可访问。

list.setCellRenderer(new ListCellRenderer() {

    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {

        JCheckBox c = new JCheckBox();
        JButton b = new JButton("My Button!");

        JPanel p = new JPanel(new FlowLayout());
        p.add(c);
        p.add(b);

        if (isSelected) {
            p.setBackground(Color.LIGHT_GRAY);
        }
        return p;
    }
});

Clicking on checkbox or button doesn't do anything. 单击复选框或按钮不会执行任何操作。 I've also tried to add ActionListener to buttons, but it didn't fire while clicked. 我也尝试将ActionListener添加到按钮,但是在单击时并未触发。 The only thing that works is background color while selected item (see screenshot). 唯一有效的是所选项目时的背景色(请参见屏幕截图)。

There is not even the "click animation" when I click the button. 当我点击按钮时,甚至没有“点击动画”。

alt text http://foto.darth.cz/pictures/2009-05-19_151057.png 替代文字http://foto.darth.cz/pictures/2009-05-19_151057.png

So my question is, do I have to do something else to enable evens on rendered components? 所以我的问题是,是否需要做其他事情才能在渲染的组件上启用偶数?

Same as for Tables really. 确实与表格相同。 A renderer component is not actually a real component. 渲染器组件实际上不是真正的组件。 It just like a template used to draw a fake component. 就像用来绘制假部件的模板一样。 Why don't you try sticking your components in either a JScrollPane or make them into a one column JTable and implement the appropriate renderer/editors instead? 为什么不尝试将组件粘贴在JScrollPane中或将它们放入一列JTable中,而是实现适当的渲染器/编辑器呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM