AutoComplete Suggestion for JTextfield Using JList
In Java, implementing an auto-complete feature for a JTextfield is possible by utilizing a JList to display suggestion options as the user inputs characters. Here's how you can approach this:
Example Output:
[Image of auto-complete suggestions displayed in a JTextfield below the input field]
Sample Code:
import java.awt.*; import java.util.ArrayList; import javax.swing.*; public class AutoCompleteTextField { private JFrame frame; private ArrayList<String> listSomeString = new ArrayList<>(); private Java2sAutoTextField someTextField = new Java2sAutoTextField(listSomeString); private ArrayList<String> listSomeAnotherString = new ArrayList<>(); private Java2sAutoComboBox someComboBox = new Java2sAutoComboBox(listSomeAnotherString); public AutoCompleteTextField() { // Populate suggestion lists listSomeString.add("-"); listSomeString.add("Snowboarding"); listSomeString.add("Rowing"); listSomeString.add("Knitting"); listSomeString.add("Speed reading"); listSomeString.add("Pool"); listSomeString.add("None of the above"); listSomeAnotherString.add("-"); listSomeAnotherString.add("XxxZxx Snowboarding"); listSomeAnotherString.add("AaaBbb Rowing"); listSomeAnotherString.add("CccDdd Knitting"); listSomeAnotherString.add("Eee Fff Speed reading"); listSomeAnotherString.add("Eee Fff Pool"); listSomeAnotherString.add("Eee Fff None of the above"); // Configure JTextfield someTextField.setFont(new Font("Serif", Font.BOLD, 16)); someTextField.setForeground(Color.black); someTextField.setBackground(Color.orange); someTextField.setName("someTextField"); someTextField.setDataList(listSomeString); // Configure JList someComboBox.setPrototypeDisplayValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); someComboBox.setFont(new Font("Serif", Font.BOLD, 16)); someComboBox.setForeground(Color.black); someComboBox.setBackground(Color.YELLOW); someComboBox.getEditor().selectAll(); someComboBox.getEditor().getEditorComponent().setBackground(Color.YELLOW); ((JTextField) someComboBox.getEditor().getEditorComponent()).setDisabledTextColor(Color.black); someComboBox.setName("someComboBox"); someComboBox.setDataList(listSomeAnotherString); // Create JFrame frame = new JFrame(); frame.setLayout(new GridLayout(0, 1, 10, 10)); frame.add(someTextField); frame.add(someComboBox); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(100, 100); frame.pack(); frame.setVisible(true); // Set initial focus someTextField.grabFocus(); someTextField.requestFocus(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { AutoCompleteTextField aCTF = new AutoCompleteTextField(); } }); } }
By following this approach, you can integrate auto-complete suggestions into your Java application using a JTextfield and a JList effectively.
The above is the detailed content of How can I implement auto-complete functionality in a JTextfield using a JList in Java?. For more information, please follow other related articles on the PHP Chinese website!