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.

Search item in JList in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer