Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dd an object to the JList and show member of the object on the list interface to the user?

Tags:

java

swing

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

CD is:

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

ArrayList is:

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

And now I want to add myList to a 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 way correct? Secondly how can I show the user name field of the object in the list? my desired interface is :

enter image description here

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

like image 551
Bernard Avatar asked Sep 18 '12 14:09

Bernard


2 Answers

Use a custom renderer based on the 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();
            }
        });
    }
}
like image 59
Guillaume Polet Avatar answered Oct 17 '22 21:10

Guillaume Polet


I think that if you want to work with the CD class directly with JList, you'll need to write your own renderer, that will work with CD class. The renderer may extend the JLabel and display the contents of the name field.

There's also other way: for() that iterates over the ArrayList and adds only the name field to the DefaultListRenderer. The way that you'll do it is your choice.

You can also add ListSelectionListener to show the album details of the selected list item.

All that you need to know is here:

  • http://docs.oracle.com/javase/tutorial/uiswing/components/list.html#renderer
  • http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer
  • http://docs.oracle.com/javase/tutorial/uiswing/components/list.html#selection

// Thanks for kleopatra :)

like image 36
m4tx Avatar answered Oct 17 '22 20:10

m4tx