2

I would like to use a more complex renderer consisting of several components for a list (more precisely something like this (a textfieldinput with some buttons arranged in a panel). However when I try to use a button in a list it seems to ignore clicks.

Minimal example

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

edit:added minimal example as suggested. Updated question since original problem was solved during minimal example creation

Community
  • 1
  • 1
ted
  • 4,791
  • 5
  • 38
  • 84
  • I'm running what I assume is very similar code to yours. It's not pretty, but I do get different components depending on selection state. Could you post runnable code which displays the problem? Edit: I'm overriding `DefaultListCellRenderer#getListCellRendererComponent`, it's not entirely clear from your pseudocode exactly what you're doing. – Jacob Raihle Jul 10 '12 at 08:21
  • For better help sooner, post an [SSCCE](http://sscce.org) – Guillaume Polet Jul 10 '12 at 08:57
  • I will delete this (in an hour or so, so you have a chance to see the comment), I am sorry abotu your time, I thought I had everything right, but when I created the minimal example, it turns out the principle works, so my mistake must be somewhere in the bigger mess – ted Jul 10 '12 at 09:26
  • well i got "lucky" i have found a related issue, i will update the question, with minimal example – ted Jul 10 '12 at 09:39

1 Answers1

2

A renderer alone is not sufficient; you also need an editor, which is not part of the JList API. As an alternative, use a JTable with a custom renderer and editor. JRadioAsRendererEditor is an example that adds a StatusPanel containing radio buttons. Of course, you can also use a multi-column table, too.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • since I have worked with tables and custom editors before I will just accept this, I hoped that lists also had those possibilities – ted Jul 10 '12 at 11:44
  • 1
    there are some issues with this as well though, the laf is slightly different (can be fixed with using the default list renderer as cell renderer) and some keybindings like arrowkeys won't work out of the box – ted Jul 10 '12 at 12:30