Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: how to convert sql databse into string array to populate jcombobox to filter typing in jtextfield

  1. #1
    Junior Member
    Join Date
    Oct 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to convert sql databse into string array to populate jcombobox to filter typing in jtextfield

    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
    Last edited by JEYARAMAN S; October 17th, 2020 at 04:39 AM. Reason: not done properly

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to convert sql databse into string array to populate jcombobox to filter typing in jtextfield

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Also add some comments describing what the code is trying to do and how it is going to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to convert sql databse into string array to populate jcombobox to filter typing in jtextfield

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Also add some comments describing what the code is trying to do and how it is going to do it.
    sir,
    I am a beginner in java
    i do not know how to do it.

    what i want is

    I WANT TO POPULATE JCOMBOBO FROM DATABASE
    WHEN I TYPE CUSTOMER NAME IN JTEXTFIELD
    I WANT ALL THE CUSTOMERS NAME DISPLAYED ACCORDING TO THE
    TYPING OF LETTERS.

    WHAT HAPPENS IN MY ABOVE CODE ONLY ONE CUSTOMER NAME APPEARS

    I WANT TO ALL THE CUSTOMERS NAME DISPLAYED INTO TEXTFIELD.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to convert sql databse into string array to populate jcombobox to filter typing in jtextfield

    ONLY ONE CUSTOMER NAME APPEARS
    Is it the last item returned by the getString method?
    That would be because the loop containing the getString method does not save what is returned. The value in NEWITEMS must be added to the List after each time its value is set from the getString method. Otherwise only the last value will be in NEWITEMS.


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] [Beginner]
    By adem2660 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 3rd, 2014, 07:45 AM
  2. how do i populate jcombobox from mysql query?
    By ankit1122 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 13th, 2014, 05:15 AM
  3. Convert List<Set<String>> to string array
    By shatlav in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2013, 08:16 AM
  4. How to convert String array to Integer array...?'
    By suyog53 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 25th, 2012, 08:50 AM
  5. I need to convert String into array
    By talha07 in forum Collections and Generics
    Replies: 1
    Last Post: October 9th, 2011, 01:58 PM