我想使用一个更复杂的渲染器,它包含一个列表的几个组件(更准确地说是这样的(一个文本字段输入,一些按钮排列在面板中)。但是,当我尝试使用列表中的按钮时,它似乎忽略了点击.
最小的例子
from javax.swing import DefaultListCellRenderer
from javax.swing import DefaultListSelectionModel
from javax.swing import JButton
from javax.swing import JLabel
from javax.swing import JPanel
from javax.swing import JList
from javax.swing import JFrame
def printer():
print "pressed the button"
class cellRenderer(DefaultListCellRenderer):
def getListCellRendererComponent(self, theList, value, index, selected, hasFocus):
renderer = DefaultListCellRenderer.getListCellRendererComponent(self, theList, value, index, selected, hasFocus)
if selected:
pan = JPanel()
pan.add(JLabel('beer'))
pan.add(JButton('get one', actionPerformed=printer))
renderer = pan
return renderer
panel = JFrame('example', size=(200,200))
configurations = JList([1,2,3,4],
cellRenderer = cellRenderer(),
selectionMode = DefaultListSelectionModel.SINGLE_SELECTION )
panel.add(configurations)
panel.visible = True
编辑:按照建议添加最小示例。更新的问题,因为在最小的示例创建过程中解决了原始问题