从Java中的JList获取选定的值

时间:2015-03-04 11:05:41

标签: java swing actionlistener jlist

如何从Jlist中获取所选值? 我尝试了以下代码,但所有变量都显示为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);
        }
    }

3 个答案:

答案 0 :(得分:3)

最初在JList中未选择任何元素,因此如果您不从列表中选择元素,则返回的索引将为-1,返回值将为null。 尝试此代码并从列表中选择和元素,然后测试它是否有效:

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

答案 1 :(得分:2)

这不是答案,因为它并没有真正解决所述问题。因此我不得不很快删除它。我发布它是为了显示该代码的轻微变体(作为MCVE),以证明如果列表中的项目被选中,则输出 中没有看到null 即可。那么,并且鼓励您发布实际显示所述问题的MCVE代码。

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

答案 2 :(得分:0)

此代码将获得多个选定值...

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

此代码将获得一个值...

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

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