Get the selected values from JList in Java

旧街凉风 提交于 2020-01-05 08:12:22

问题


How can i get the selected value from Jlist? I tried the following code, but all variables are displayed null. Why index variable is null?

 public class GetSelectedValueFromJList extends JFrame implements ActionListener {
    private JList list;
    private JButton checkButton;

    public GetSelectedValueFromJList() {



        String[] nameList = { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5"};
        list = new JList(data);
        checkButton = new Button("Check");
        button.addActionListener(this);

        //add list to frame
        add(list);
        add(checkButton);

    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getCommand().equals("Check"))
        {
            int index = list.getSelectedIndex();
            System.out.println("Index Selected: " + index);
            String s = (String) list.getSelectedValue();
            System.out.println("Value Selected: " + s);
        }
    }

回答1:


Initially no element is selected in JList so if you don't select an element from the list the returned index will be -1 and returned value will be null. try this code and select and element from the list then test if it works:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

public class Frame extends JFrame implements ActionListener
{
    private JList list;
    private JButton checkButton;

    public Frame()
    {
        setBounds(100,100,300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String[] nameList = { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" };
        list = new JList(nameList);
        checkButton = new JButton("Check");
        checkButton.addActionListener(this);

        // add list to frame
        JPanel panel = new JPanel();
        panel.add(list);
        panel.add(checkButton);
        add(panel);
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new Frame();
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals("Check"))
        {
            int index = list.getSelectedIndex();
            System.out.println("Index Selected: " + index);
            String s = (String) list.getSelectedValue();
            System.out.println("Value Selected: " + s);
        }
    }
}



回答2:


This is not an answer, since it does not really address the stated problem. As such I'll have to delete it soon. I am posting it to show a slight variant of that code (as an MCVE) to demonstrate that there is no null seen in the output if an item in the list is selected. Well, that and to encourage you to post an MCVE of code that actually shows the stated problem.

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;

public class GetSelectedValueFromJList
        extends JFrame implements ActionListener {

    private JList list;
    private JButton button;

    public GetSelectedValueFromJList() {
        String[] nameList = {
            "Value 1", "Value 2", "Value 3", "Value 4", "Value 5"
        };
        list = new JList(nameList);
        list.setSelectedIndex(2);

        button = new JButton("Check");
        button.addActionListener(this);

        add(list);
        add(button, BorderLayout.PAGE_END);

        pack();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Check")) {
            int index = list.getSelectedIndex();
            System.out.println("Index Selected: " + index);
            String s = (String) list.getSelectedValue();
            System.out.println("Value Selected: " + s);
        }
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                new GetSelectedValueFromJList().setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}



回答3:


This code will get more than one selected values...

 int[] selectedIndices = jList1.getSelectedIndices();
String[] myArray = new String[50];
for (int i = 0; i < selectedIndices.length; i++) {
               myArray[i] =  String.valueOf(jList1.getModel().getElementAt(selectedIndices[i]));
        }

this code will get one value...

String myString = String.valueOf(jList1.getModel().getElementAt(jList1.getSelectedIndex());

or

 String myString = String.valueOf(jList1.getSelectedValue());


来源:https://stackoverflow.com/questions/28852864/get-the-selected-values-from-jlist-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!