update/refresh combobox with glazedlists

770 Views Asked by At

how could i ahhm.."auto-update" my comboBox..?im using glazedlists autoComplete and im bit lost on how to do it..ive read some like use eventlists and basiclistbut i couldnt get the idea on how to make it work.. pls help :( heres my sample code..but i dunno whats next to it..ive tried using eventlists but couldnt make it update on its own..

abc = AutoCompleteSupport.install(comboSearch,GlazedLists.eventListOf(auto));
abc.setStrict(false);
public void count(){


    try{

         String sql2 = "select count(*) from daily_input";

         stmt = conn.prepareStatement(sql2);

         rs=stmt.executeQuery();
         while(rs.next()){
         String x = rs.getString("count(*)");

         z = Integer.parseInt(x);

         }

         auto = new String[z];
         idNum = new int[z];


     }

      catch(SQLException | NumberFormatException e){

      }

}

public void cB(){
    count();
     i=0;

     try{

            String sql = "Select concat(first_name, ' ',last_name) as full_name from      daily_input";

            stmt = conn.prepareStatement(sql);
            rs=stmt.executeQuery();

            while(rs.next()){

              String name = rs.getString("full_name");

                auto[i] = name;

                i++;

             }



            comboSearch.isEditable();


      }
1

There are 1 best solutions below

3
On

You need to connect your combo-box with event list and then add elements to event list.

public class ComboBoxTest {
    private final EventList<String> values;

    public ComboBoxTest() {
        this.values = GlazedLists.eventListOf("A", "B", "C", "D");
    }

    public Component createControl() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(this.createComboBox(), BorderLayout.NORTH);
        panel.add(new JButton(new AbstractAction("Add Elements") {
            @Override
            public void actionPerformed(ActionEvent e) {
                ComboBoxTest.this.addElements();
            }
        }), BorderLayout.SOUTH);

        return panel;
    }

    public void addElements() {
        List<String> toAdd = new ArrayList<>(2);
        toAdd.add("E");
        toAdd.add("F");
        this.values.addAll(toAdd);
    }

    private Component createComboBox() {
        JComboBox<String> box = new JComboBox<>();
        box.setEditable(true);

        AutoCompleteSupport.install(box, this.values);

        return box;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ComboBoxTest testApp = new ComboBoxTest();
                JFrame frame = new JFrame("ComboBox Test");
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.add(testApp.createControl());
                frame.setSize(600, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}