简体   繁体   中英

i can't select item from JList

this is part of my code , i faced problem which is i can't select item from my list ( i can't press on items to select it when my interface run ) !..

DefaultListModel model = new DefaultListModel ();
JList list=new JList(model);
comboBox_2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
          JComboBox comboBox_2 = (JComboBox) e.getSource();
           Object selected = comboBox.getSelectedItem();
           Object selected_2 = comboBox_2.getSelectedItem();
         if(selected.toString().equals("1")) {   
              if (selected_2.toString().equals("Oils")) {
                try{
                    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/flyer","root","000");
                    String query="SELECT item_name FROM item WHERE ( month(expired_date) = month(curdate()) AND day(expired_date) >= day(curdate()) AND year(expired_date) = year(curdate()) AND category_id=1) OR ( month(expired_date) = month(curdate()) +1 AND day(expired_date) <= day(curdate()) AND year(expired_date) = year(curdate()) AND category_id=1) ";
                    Statement st= conn.createStatement();
                    ResultSet r=st.executeQuery(query);
                    model.removeAllElements();
                    while(r.next()){
                        String itemCode = r.getString("item_name"); 
                        model.addElement(itemCode);
                }}
                catch (Exception ex){
                    System.err.println(e);

                }}

Check your output and see if you are getting any exceptions. Also, you are trying to print action listener in catch(Exception ex) block.

PS: Your code is really messy. You should fix indentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM