2014-11-04 44 views
0

我的問題是,我怎麼能重畫我的JList雙擊列表項目? 請幫我我是新的Java這是我的第一個Java程序。如何在按下某個項目後重新繪製JList?

這裏我的JList剪斷:

MDAY和修改時間來自數據庫與字符串JList中

liste = new JList(mday); 
    System.out.println(mday); 
    list.frame.add(liste, BorderLayout.CENTER); 
    list.frame.validate(); 
    list.frame.repaint(); 
    liste.addMouseListener(new MouseAdapter() { 
     public void mouseClicked(MouseEvent evt) { 
      liste0 = (JList) evt.getSource(); 
      if (evt.getClickCount() == 2) { 
       int index = liste0.locationToIndex(evt.getPoint()); 
       index = index + 1; 
       System.out.println(index); 
       try { 
        c = DriverManager.getConnection(DB_PATH); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       System.out.println("Opened database successfully"); 
       try { 
        stmt = c.createStatement(); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       String sql = "SELECT * from playscr WHERE _id LIKE " + index; 
       try { 
        rs = stmt.executeQuery(sql); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       DefaultListModel<String> mtime = new DefaultListModel<String>(); 
       try { 
        while(rs.next()){ 
         String name = ""; 
         String zeit = rs.getString("zeit"); 
         String daten = ""; 
         GetData data = new GetData(name, zeit, daten); 
         mtime.addElement(data.toString()); 
        } 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       liste = new JList(mtime); 
       System.out.println(mtime); 
       list.frame.revalidate(); 
       list.frame.repaint(); 
       list.frame.add(liste, BorderLayout.CENTER);    
      } 
     } 
    }); 

回答

1

您need't重新創建默認模型JList,只需創建列表。從模型JList<String> list = new JList<String>(new DefaultListModel<String>());

,然後在鼠標監聽刪除項目:((DefaultListModel<String>)list.getModel()).removeAllElements();,並添加新項目((DefaultListModel<String>)list.getModel()).addElement("test");

JList tutorial

相關問題