Get the selected values from JList in Java
19,626
Solution 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);
}
}
}
Solution 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);
}
}
Author by
Adrian Trifan
Updated on June 04, 2022Comments
-
Adrian Trifan about 2 years
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); } }
-
Andrew Thompson over 9 years
-
Andrew Thompson over 9 years
checkButton = new Button("Check");
Don't mix Swing and AWT. Use a SwingJButton
. -
Andrew Thompson over 9 years
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.
-
-
Adrian Trifan over 9 yearsRegardeless of the chosen JList element, index is -1 and value selected is null.
-
Naruto Biju Mode over 9 years@AdrianTrifan I tested the given example above and it's working fine, i think you have another piece of code which reset your JList.
-
Andrew Thompson over 9 years"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!