JList with custom renderer

I have a JList that shows several JPanels on them, I created my own renderer that returns a new JPanel. JPanels are displayed in the JList, but they are not available, I can’t select them, and if I have a button or text area, I can’t click it. I want to try if this works in JList because I want to continue pagination. I managed to get it working by adding panels to the Jscroll panel, but I would like the JList to work.

thank

+3
source share
3 answers

This is normal behavior JList(and JTabel, JComboBoxetc.).

JPanel, , Swing. paint JList . , , JList, , .

, , . , ( ).

. Swing.

, JList , . JTable JList. A JTable JList. JTable? JTable . JTable, Renderers. , JTable. , . Renderer Editor , , JTable .

Swing .

+10

, ,

- . , .

. , , , , .

0

Here is my solution:

public class AccountRenderer extends DefaultListCellRenderer {

private static final long serialVersionUID = 1L;

@Override
public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {
    JLabel renderer = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    if (value != null) {
        Account entry = (Account) value;
        renderer.setText(entry.getName());
    }
    return renderer;
}

}

0
source

Source: https://habr.com/ru/post/1783882/


All Articles