I have a JList that shows multiple JPanels on them , i have created a custom renderer that returns a new JPanel.
The JPanels are displayed in the JList but they are not accessible, i cant select them and if i have a button or a text area in it i can not press it.
I want to try if this works in a JList because i want to do further pagination. I managed to get it work by adding panels to a Jscroll pane, but would love to make the JList thing work.
Thanks
This is the normal behavior of the
JList(andJTabel,JComboBox, etc…).The
JPanelthat your custom renderer returns is not added to the Swing hierarchy. Only itspaintmethod is used by theJList, to draw the renderer at the right place. The renderer just acts as a stamp, and what you see in theJListare not components, but images of components.This is an efficient way of displaying many components on the screen, without having the overhead of real instantiated components. Note that your renderer can return always the same instance (it’s even preferable).
See the Swing tutorial for more details.
If you want the entries of the
JListto act like real components, you can do the following. First, use aJTableinstead of aJList. AJTablewith one column and no header is roughly the same as aJList. Why use aJTable? BecauseJTableprovides Editors. Editors are registered on theJTable, just like Renderers are. An Editor appears generally when a user clicks on a JTable’s cell. The Editor is superimposed over the renderer, and this time it’s a real component. If the Renderer and the Editor components are identical, then the user has the feeling that the JTable’s cells are real components.The Swing tutorial has examples for this technique.