Unable to get each item from selected multiple line Jlist?

After doing lots of research i am still unable to get items from selected multiple line jlist. Here is my code for listSelectionListener:

list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            ListModel model = list.getModel();

            for(int i=0; i < model.getSize(); i++){
                System.out.println( model.getElementAt(i));  
            }
        }
    });

I also added a button to print the selected items:

btnNewButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            ListModel model = list.getModel();

            for(int i=0; i < model.getSize(); i++){
                System.out.println( model.getElementAt(i));  
            }
        }
    });

but Still getting the same output :

I am expecting output “Name: Paul0 Age: 0.” I also tried adding DefaultlistModel instead of ListModel but the output is same. Please help I want each item inside selected line.I am using JDK8. Below is complete working code.

    import java.awt.Component;

    import javax.swing.DefaultListCellRenderer;
    import javax.swing.DefaultListModel;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.ListModel;
    import javax.swing.SwingUtilities;
    import javax.swing.JButton;

    import java.awt.BorderLayout;

    import javax.swing.event.ListSelectionListener;
    import javax.swing.event.ListSelectionEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;


public class Test3 {  

JList<Student> list;
DefaultListModel model;

public Test3() {
    list = new JList();
    list.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            ListModel model = list.getModel();

            for(int i=0; i < model.getSize(); i++){
                System.out.println( model.getElementAt(i));  
            }
        }
    });
    model = new DefaultListModel();
    for (int i = 0; i < 10; i++) {
        model.addElement(new Student("Paul" + i, i));
    }
    list.setModel(model);
    list.setCellRenderer(new MyListCellRenderer());

    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(list);

    JButton btnNewButton = new JButton("Print");
    btnNewButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            ListModel model = list.getModel();

            for(int i=0; i < model.getSize(); i++){
                System.out.println( model.getElementAt(i));  
            }
        }
    });
    frame.getContentPane().add(btnNewButton, BorderLayout.NORTH);
    frame.pack();
    frame.setVisible(true);
}

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

private class MyListCellRenderer extends DefaultListCellRenderer {

    @Override
    public Component getListCellRendererComponent(
            JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        Student label = (Student) value;
        String name = label.getName();
        int age = label.getAge();
        String labelText = "<html>Name: " + name + "<br/>Age: " + age;
        setText(labelText);

        return this;
    }

   }
}

class Student {
    String name;
    int age;
    public Student(String name, int age) {
       this.name = name;
       this.age = age;
    }
    public String getName() { return name; }
    public int getAge() { return age; }
}

Answer

getElementAt is returning the Object at the specified position, which happens to be a Student in your case. System.out.println is using the object’s toString method to print the object, so, based on your code, it seems to be working.

But, if you want the output of "Name: Paul0 Age: 0.", you need to change the Student class and override the toString method to meet your requirements

class Student {
    String name;
    int age;
    public Student(String name, int age) {
       this.name = name;
       this.age = age;
    }
    public String getName() { return name; }
    public int getAge() { return age; }
    public String toString() {
        return "Name: " + getName() + " Age: " + getAge());
    }
}

Or something like that

You might also want to take a look at JList#getSelectedValuesList to get a List of the items that the user has selected (but I might be misinterrupting your question)

You could also have a look at Object as a Superclass (look for the section called “The toString() Method”) for more details

Leave a Reply

Your email address will not be published. Required fields are marked *