我无法从JList中选择项目

时间:2017-04-19 10:32:20

标签: java swing jlist

这是我的代码的一部分,我遇到的问题是我无法从我的列表中选择项目(当我的界面运行时我不能按项目来选择它)!..

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);

                }}

1 个答案:

答案 0 :(得分:2)

检查您的输出,看看您是否收到任何例外情况。此外,您正尝试在catch(Exception ex)块中打印动作侦听器。

P.S:你的代码非常混乱。你应该修改缩进。