繁体   English   中英

无法从选定的多行Jlist中获取每个项目?

[英]Unable to get each item from selected multiple line Jlist?

经过大量研究后,我仍然无法从选定的多行jlist中获取项目。 这是我的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));  
            }
        }
    });

我还添加了一个按钮来打印所选项目:

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));  
            }
        }
    });

但仍然得到相同的输出:

com.example.main.Student@c36b5fb
com.example.main.Student@2e61eb2
com.example.main.Student@151f708e
com.example.main.Student@563e55b0

我期望输出“名称:Paul0年龄:0。” 我也尝试添加DefaultlistModel而不是ListModel,但是输出是相同的。 请帮助我让每个项目都在所选行中。我正在使用JDK8。 以下是完整的工作代码。

    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; }
}

getElementAt返回指定位置的Object ,在您的情况下恰好是Student System.out.println正在使用对象的toString方法来打印对象,因此,根据您的代码,它似乎正在工作。

但是,如果要输出"Name: Paul0 Age: 0." ,您需要更改Student类并覆盖toString方法以满足您的要求

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());
    }
}

或类似的东西

您可能还想看看JList#getSelectedValuesList以获得用户选择的项目的List (但我可能会打扰您的问题)

您还可以查看作为超类的对象 (请参见“ toString()方法”一节),以了解更多详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM