1

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);
        }
    }
3
  • 1
    1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 2) For better help sooner, post an MCVE (Minimal Complete Verifiable Example) or SSCCE (Short, Self Contained, Correct Example). Commented Mar 4, 2015 at 11:06
  • checkButton = new Button("Check"); Don't mix Swing and AWT. Use a Swing JButton. Commented Mar 4, 2015 at 11:09
  • 1
    if(e.getCommand().equals("Check")) BTW - that code (as well as other parts) even if complete and with imports, will not compile. Don't post 'something like' the code being used, it wastes your time, as well as the time of other people who are trying to help (for free). Copy/paste a proper MCVE. Commented Mar 4, 2015 at 11:12

3 Answers 3

5

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Regardeless of the chosen JList element, index is -1 and value selected is null.
@AdrianTrifan I tested the given example above and it's working fine, i think you have another piece of code which reset your JList.
"Regardeless of the chosen JList element, index is -1 and value selected is null." Then post a compilable and runnable example as I advised an hour ago, and two other people have already done!
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);
    }
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.