ListCellRenderer not Firing Events on Child Components

The following ListCellRenderer does not receive click events on the nested ComboBoxes. Do I need to enable something?

class FilterCellRenderer implements ListCellRenderer {

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

        JPanel filterPanel = new JPanel();
        FlowLayout layout = new FlowLayout();
        layout.setAlignment(FlowLayout.LEFT);
        filterPanel.setLayout(layout);
        filterPanel.add(new JLabel(filter.getLabel()));

        final List<Object> options = filter.getOptions();
        if (options.size() > 1) {
            JComboBox optionCombo = new JComboBox(new AbstractComboBoxModel() {

                public int getSize() {
                    return options.size();
                }

                public Object getElementAt(int index) {
                    return options.get(index);
                }
            });
            optionCombo.setSelectedItem(filter.getValue());
            filterPanel.add(optionCombo);
        }

        if (isSelected) {
            filterPanel.setBackground(list.getSelectionBackground());
            filterPanel.setForeground(list.getSelectionForeground());
        } 
        return filterPanel;
    }

}


Asked by: Vanessa677 | Posted: 21-01-2022






Answer 1

Renderer components in swing work like "rubber stamps" -they are just used to render/paint a value and are not added to the parent container in the usual way (just think how a single component could be added in multiple places!).

It sounds like you may want an editor rather than a renderer (an editor is a fully-fledged component, added in one place at any given time). Failing that you will have to install the MouseListener on the JList instead.

Answered by: Ned690 | Posted: 22-02-2022



Answer 2

Since I didn't need to select rows, I ended up just dynamically adding and elements to a JPanel with a custom layout. Allowed for full component behaviour without having to hack a table.

Answered by: Edgar599 | Posted: 22-02-2022



Answer 3

It's a little bit tricky this. I believe you need to replace the JList with a single column JTable. Then set a table cell editor as well as renderer. IIRC, there might be a problem losing the first click (which gets used to select that cell edited).

Also it's a very good idea to reuse the components between each call to getCellRendererComponent. The components are used as a stamp and then discarded. Performance will suck massively if they are recreated each time.

Answered by: John734 | Posted: 22-02-2022



Similar questions

Java Swing: JList with ListCellRenderer selected item different height

I'm making a custom ListCellRenderer. I know that you can have different dimensions for each individual cell. But now I want to have a different dimension for the selected cell. Somehow, the JList is caching the dimension for each individual cell the first time it has to calculate bounds for each cell. This is my code: public class Test { static class Oh extends JPanel { public Oh() { ...


java - broken img tag with JEditorPane / ListCellRenderer

I have this code in a ListCellRenderer which extends JEditorPane. The editor pane doesn't show the image, but instead shows a 'broken icon'. What's wrong with it? public class TweetCellRenderer extends JEditorPane implements ListCellRenderer { public Component getListCellRendererComponent( javax.swing.JList list, Object value, int index, boolean isSelected, boole...


java - Can't not use ListCellRenderer

I'm using Netbeans to develop my Java application.I want to display list of items with icons. I have use ListCellRenderer but it just display item, but not icon. Here is my code //Item class public class Item { private String title; private String imagePath; private ImageIcon image; //getter and setter} //ItemRenderer public class ItemRend...


java - Wrong background colors in Swing ListCellRenderer

I'm currently trying to write a custom ListCellRenderer for a JList. Unfortunately, nearly all examples simply use DefaultListCellRenderer as a JLabel and be done with it; I needed a JPanel, however (since I need to display a little more info than just an icon and one line of text). Now I have a problem with the background colors, specifically with th...


focus - Custom Java ListCellRenderer - Can't click JCheckBox

Made a custom ListCellRenderer: import java.awt.Component; import javax.swing.JCheckBox; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListCellRenderer; /** * * @author Spencer */ public class TaskRenderer implements ListCellRenderer { private Task task; private JPanel panel = new JPanel(); private JCheckBox checkbox = new JCheckBox(); ...


Java Custom ListCellRenderer casting problem (SSCCE included)

I am trying to create a custom ListCellRenderer in order to give different foreground colors in each line, depending on the input of the jList. I am not an expert or anything, but I really can't figure this out. I get a casting error: Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to app.CustomObject Thanks for your time. Here is the SSCCE:


java - Why removeAll() is required in ListCellRenderer?

This is my code:- public class MyRender extends JPanel implements ListCellRenderer { ImageIcon on_img; JLabel name = new JLabel(); JLabel icn = new JLabel(); JLabel img = new JLabel(); public MyRender(Atalk) { setOpaque(true); setBackground(Color.WHITE); setForeground(Color.black); on_img = new ImageIcon(MyCls.class.getClassLoader().getResource("imgPath"...


java - Custom ListCellRenderer will not change background color

I have this class: @SuppressWarnings("serial") private class DataCellRenderer extends JLabel implements ListCellRenderer { public DataCellRenderer() { setHorizontalAlignment(SwingConstants.RIGHT); } @Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) ...


java - Multiple ListCellRenderer

Is it possible to have multiple ListCellRenderer's implementation in a single class? Actually I have multiple JList's in my application and I would I am in need of different ListCellRenderer's for each. Can I have different class names for Implementing ListCellRenderer's Component method. For ex: If I have a class with name "MultiColumnCellRenderer" with some implementation of Component method and a...


swing - Why do i get class cast exception on ListCellRenderer of JComboBox in java?

I have a custom combo box ListCellRenderer as below. class ArtikelListRenderer extends JLabel implements ListCellRenderer { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Artikel artikels = (Artikel) value; setText(artikels.getName()); return this; } } I add this render to comb...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top