[英]i can't select item from 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);
}}
檢查您的輸出,看看是否有任何異常。 另外,您嘗試在catch(Exception ex)
塊中打印動作偵聽器。
PS:您的代碼確實很亂。 您應該修復縮進。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.