How to delete an object in a JList and show a member of an object in a list interface to a user?

I have an ArrayList object, which is a cd type. I want to add all the objects in a JList and show the name field to the user. I can add a String type to a JList, but what about a specific field of an object?

CD:

class CD{
    public CD(String n){name = n;}
    private String name;
    public String getName(){return name;}
    public void setName(String n){name = n;}
}

ArrayList:

ArrayList<CD> myList = new ArrayList<CD>();

And now I want to add myList to the JList:

JList list = new JList(myList);
panel.add(list);
JScrollPane scrol = new JScrollPane(list);
frame.add(scrol,BorderLayout.EAST);
frame.add(panel);
frame.setVisible(true);

First of all, is this correct? Secondly, how can I show the username field for an object in a list? my desired interface:

enter image description here

The left side is the name of my object! Thanks in advance. Bernard

+5
source share
3 answers

Use custom rendering based on DefaultListCellRenderer:

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class TestJList {

    class CD {
        public CD(String n) {
            name = n;
        }

        private String name;

        public String getName() {
            return name;
        }

        public void setName(String n) {
            name = n;
        }
    }

    protected void initUI() {
        List<CD> cds = new ArrayList<CD>();
        cds.add(new CD("MJ - Bad"));
        cds.add(new CD("Mozart - Concerto 123"));
        cds.add(new CD("Nadeah - Odile"));
        JFrame frame = new JFrame(TestJList.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JList list = new JList(new Vector<CD>(cds));
        list.setVisibleRowCount(10);
        list.setCellRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                Component renderer = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (renderer instanceof JLabel && value instanceof CD) {
                    // Here value will be of the Type 'CD'
                    ((JLabel) renderer).setText(((CD) value).getName());
                }
                return renderer;
            }
        });
        frame.add(new JScrollPane(list));
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestJList().initUI();
            }
        });
    }
}
+9
source

There is no constructor for a JList that accepts an ArrayList, however you can use Vector or Array (and ArrayList can be easily converted to an array).

to display what you want for the user, you can use your own renderer. Alternatively, you can simply override the toString () method of the CD, which, if I remember correctly, is used by default to determine what to show.

0
source

All Articles