无法使用按钮或 ListSelectionListener 从 JList 获取值

Can't get value from a JList neither with a button or ListSelectionListener

我有一个 swing GUI,在 JScrollPane 中有一个 JList,我用字符串填充了它。我想从列表中获取选定的字符串,但每次都是 returns -1。我还选择了不起作用的听众。从此处的帖子中查找其他帖子,但无论我点击多少,听众都不会被解雇。选择单选模式。 这是按钮方法。值为空,索引为-1。

 loadSongBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index=list.getSelectedIndex();
                System.out.println("test "+list.getSelectedValue());
                String name=(String)list.getModel().getElementAt(index); //works if i put a number
            }
        });

这里是初始化:

 private void initList(){
        String[] songNames = extractSongs(); 
        JList songsList=new JList(songNames); //tried to see if the problem is here
        this.list=songsList;
    }
private void createUIComponents() {
        initList();
        songListPane = new JScrollPane(list);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

这里是 SelectionListener 以防万一

list.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        JList<String> lst = (JList<String>) e.getSource();
                        String selection = lst.getSelectedValue();
                        System.out.println(selection);//doesnt go into the method at all in debug 
                }
            }
        });

将所有东西放在一起它正在工作,所以你可能搞砸了一些变量(也许你初始化了一个 JList 并在那里注册了监听器,然后在方法 initList() 中覆盖它)。 这是一个工作示例:

package test;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class TestJList {
    
    protected JList<String> list;
    protected JScrollPane songListPane;
    protected JButton loadSongBtn;
    
    private void initList(){
        String[] songNames = new String[] { "abba", "dsst", "crvg" };
        JList<String> songsList=new JList(songNames); //tried to see if the problem is here
        this.list=songsList;
    }
    
    private void createUIComponents() {
        initList();
        loadSongBtn=new JButton("Load");
        
        songListPane = new JScrollPane(list);
        songListPane.setPreferredSize(new Dimension(200,400));
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        list.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        JList<String> lst = (JList<String>) e.getSource();
                        String selection = lst.getSelectedValue();
                        System.out.println("SEL CHANGED "+selection);//doesnt go into the method at all in debug 
                }
            }
        });
        
        loadSongBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index=list.getSelectedIndex();
                if (index<0) {
                    System.out.println("Nothing selected");
                    return;
                }
                System.out.println("BUTTON "+list.getSelectedValue());
                String name=(String)list.getModel().getElementAt(index); //works if i put a number
            }
        });
    }
    
    public TestJList() {
        initList();
        createUIComponents();
        JFrame frame=new JFrame("Test JList");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        Box content=new Box(BoxLayout.Y_AXIS);
        content.add(songListPane);
        content.add(loadSongBtn);
                
        frame.setContentPane(content);
        frame.pack();
        frame.setVisible(true);
    
    }
    public static void main(String[] args) {
        new TestJList();
    }
}