Search item in JList in Java
Description
The following code shows how to search item in JList.
Example
import java.awt.BorderLayout;
//from w w w . ja v a2 s . co m
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Modifying Model");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] items = { "A", "B", "C", "D" };
JList list = new JList(items);
String prefix = "b";
int start = 0;
int index = list.getNextMatch(prefix, start, javax.swing.text.Position.Bias.Forward);
System.out.println(index);
JScrollPane scrollPane1 = new JScrollPane(list);
frame.add(scrollPane1, BorderLayout.CENTER);
frame.setSize(640, 300);
frame.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »