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

124 views Asked by At

I have a swing GUI with a JList inside a JScrollPane and I've populated it with Strings. I want to get the selected String from the list but everytime it returns -1. I had also made selection listeners which didn't work. Looked up other ones from posts here but the listener is never fired no matter how much I click. Single selection mode is selected. Here's the button method. Value is null, index is -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
            }
        });

Here's the initialization:

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

Here's the SelectionListener just in case

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

There are 1 answers

1
Rocco On BEST ANSWER

Putting everything together it's working, so you probably messed up some variables (maybe you initialize a JList and register listeners there, then overvrite it in method initList()). Here a working example:

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