Hi,
I need to learn how I can use a custom data structure in a JList. In my program when I click the Add button next to the JList a JOptionPane opens and takes a String input. The input is then used to create an object which is placed in a data structure. The purpose of JList is to display the data structure and allow the selection of the objects in the data structure.

What is the idiom for using dynamic data structures with JLists?
currently I can not select anything from the list(nothing is highlighted). And the Frame has be clicked(focused) for the JList to display the updated list.

I am currently using the following methods for the gui:

/**
     * 
     */
    public void populateList(ListInterface grList)
    {        
        ls_group = new JList(getListModel(grList));
        LabelListCellRenderer renderer = new LabelListCellRenderer();
        ls_group.setCellRenderer(renderer);
                             ls_group.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane grScrPane = new JScrollPane(ls_group,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.insets = ins;
        gbc.fill = GridBagConstraints.BOTH;
        add(grScrPane, gbc);
    }

public DefaultListModel getListModel(ListInterface labels)
    {
        DefaultListModel model = new DefaultListModel();
        for(int i = 1; i <= labels.size(); i++) {
            model.addElement(labels.get(i));
        }
        return model;
    }

populateList method is called every time something is added to the data structure. Is there a simpler way?
The following is the code for LabelListCellRenderer class if you need it because I am not sure why I need it.

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;

class CheckListCellRenderer extends JCheckBox
    implements ListCellRenderer
{
    protected static Border m_noFocusBorder = 
        new EmptyBorder(1, 1, 1, 1);
        
    public CheckListCellRenderer()
    {
        super();
        setOpaque(true);
        setBorder(m_noFocusBorder);
    }
    
    public Component getListCellRendererComponent(JList list, 
        Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
        setText(value.toString());
        
        setBackground(isSelected ? list.getSelectionBackground() :
            list.getBackground());
        
        setForeground(isSelected ? list.getSelectionForeground() :
            list.getForeground());
            
        CheckListItem data = (CheckListItem)value;
            setSelected(data.isSelected());
            
        setFont(list.getFont());
        setBorder((cellHasFocus) ?
        UIManager.getBorder("List.focusCellHighlightBorder")
            : m_noFocusBorder);
            
        return this;
    }
}

Please kindly provide some help.

Thank you. Thank you. This just might work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.