package rdms;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
@SuppressWarnings("serial")
public class Search extends JFrame {
private JPanel contentPane;
static String ITEMS;
static String NEWITEMS;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Search frame = new Search();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
static Connection conn = null;
// PUBLIC METHOD RETRIEVE CUSTOMER
/**
* Create the frame.
*/
public Search() {
conn = DB_CONNECTION.dbConnector();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
setTitle("Auto Completion Test");
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBackground(Color.PINK);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
try {
String query = "select CUSTOMER from TBL_DRUG_123";
Statement pst = conn.createStatement();
ResultSet rs = pst.executeQuery(query);
while (rs.next()) {
NEWITEMS = rs.getString("CUSTOMER");
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
ArrayList<String> items = new ArrayList<String>();
items.add(NEWITEMS);
for (int i = 0; i < items.size(); i++) {
System.out.println(items.get(i));
JTextField txtInput = new JTextField();
txtInput.setFont(new Font("Verdana", Font.BOLD, 14));
setupAutoComplete(txtInput, items);
txtInput.setColumns(30);
contentPane.setLayout(new FlowLayout());
contentPane.add(txtInput, BorderLayout.NORTH);
contentPane.setVisible(true);
}
}
@SuppressWarnings("rawtypes")
private static boolean isAdjusting(JComboBox cbInput) {
if (cbInput.getClientProperty("is_adjusting") instanceof Boolean) {
return (Boolean) cbInput.getClientProperty("is_adjusting");
}
return false;
}
@SuppressWarnings("rawtypes")
private static void setAdjusting(JComboBox cbInput, boolean adjusting) {
cbInput.putClientProperty("is_adjusting", adjusting);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void setupAutoComplete(final JTextField txtInput, final ArrayList<String> items) {
final DefaultComboBoxModel model = new DefaultComboBoxModel();
final JComboBox cbInput = new JComboBox(model) {
public Dimension getPreferredSize() {
return new Dimension(super.getPreferredSize().width, 0);
}
};
cbInput.setFont(new Font("Times New Roman", Font.BOLD | Font.ITALIC, 14));
cbInput.setForeground(Color.BLACK);
cbInput.setBackground(Color.WHITE);
setAdjusting(cbInput, false);
for (String item : items) {
model.addElement(item);
}
cbInput.setSelectedItem(null);
cbInput.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!isAdjusting(cbInput)) {
if (cbInput.getSelectedItem() != null) {
txtInput.setText(cbInput.getSelectedItem().toStrin g());
}
}
}
});
txtInput.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
setAdjusting(cbInput, true);
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if (cbInput.isPopupVisible()) {
e.setKeyCode(KeyEvent.VK_ENTER);
}
}
if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_UP
|| e.getKeyCode() == KeyEvent.VK_DOWN) {
e.setSource(cbInput);
cbInput.dispatchEvent(e);
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtInput.setText(cbInput.getSelectedItem().toStrin g());
cbInput.setPopupVisible(false);
}
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
cbInput.setPopupVisible(false);
}
setAdjusting(cbInput, false);
}
});
txtInput.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
updateList();
}
public void removeUpdate(DocumentEvent e) {
updateList();
}
public void changedUpdate(DocumentEvent e) {
updateList();
}
private void updateList() {
setAdjusting(cbInput, true);
model.removeAllElements();
String input = txtInput.getText();
if (!input.isEmpty()) {
for (String item : items) {
if (item.toLowerCase().startsWith(input.toLowerCase() )) {
model.addElement(item);
}
}
}
cbInput.setPopupVisible(model.getSize() > 0);
setAdjusting(cbInput, false);
}
});
txtInput.setLayout(new BorderLayout());
txtInput.add(cbInput, BorderLayout.SOUTH);
}
}
--- Update ---
from jeyaraman s
the above code shwos only one customer name
BUT I WANT ENTIRE LIST FROM SQL DATABASE FROM CUSTOMER COLUMN
output
senthil knits wear