2013-06-20 1 views
0

아래에 표시되는 내 목록 선택 수신기에 대한 몇 가지 문제가 있습니다. 수신기 청취 결과가 두 번 나 들었습니다. 왜 두 번 인쇄하는거야? 2. searchBt를 누르고 결과가 표시되면 결과 중 하나를 선택합니다. valueChanged (ListSelectionEvent e)에서 ChooseIndex를 반환하지만 return 문과 Thge j 목록의 선택된 인덱스는 반환 할 수 없습니다. 쓸모없는 ... 뭐가 문제 야?Java : 목록 선택 수신기에서 결과 가져 오기

public class SearchPage extends javax.swing.JFrame { 

public SearchPage() { 
    initComponents(); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    SearchBox = new javax.swing.JTextField(); 
    SearchBt = new javax.swing.JButton(); 
    jScrollPane1 = new javax.swing.JScrollPane(); 
    jList1 = new javax.swing.JList(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    SearchBt.setText("Search"); 
    SearchBt.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      SearchBtActionPerformed(evt); 
     } 
    }); 

    jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION); 
    jList1.setToolTipText(""); 
    jList1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); 
    jList1.setSelectionBackground(new java.awt.Color(102, 0, 102)); 
    jList1.setValueIsAdjusting(true); 
    jList1.setVerifyInputWhenFocusTarget(false); 
    jScrollPane1.setViewportView(jList1); 
    jList1.getAccessibleContext().setAccessibleName(""); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(16, 16, 16) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 543, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(SearchBox, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
        .addComponent(SearchBt, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))) 
      .addContainerGap(281, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(125, 125, 125) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(SearchBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(SearchBt)) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
      .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE) 
      .addContainerGap()) 
    ); 

    pack(); 
}// </editor-fold>       

private void SearchBtActionPerformed(java.awt.event.ActionEvent evt) {           
    String tag = SearchBox.getText().trim(); 
    Vector<String> vector = new Vector<String>(); 
    for (int i = 0; i <Code.CodeSearch(tag).size(); i++) { 
     String string = Code.FNameExtractor(Code.CodeSearch(tag).get(i).getFileName())+" Uploaded By "+Code.CodeSearch(tag).get(i).getUserdetails().getUsername(); 
     vector.add(string);    
    } 
    jList1 = new JList<String>(vector); 
    ListSelectionModel listSelectionModel = jList1.getSelectionModel(); 
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler()); 
    listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    jScrollPane1.setViewportView(jList1); 
}           

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(SearchPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(SearchPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(SearchPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(SearchPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new SearchPage().setVisible(true); 
     } 
    }); 
} 
      class SharedListSelectionHandler implements ListSelectionListener { 
    public void valueChanged(ListSelectionEvent e) { 
     ListSelectionModel lsm = (ListSelectionModel)e.getSource(); 
     int ChooseIndex =lsm.getMaxSelectionIndex(); 
     System.out.println(ChooseIndex); 
    } 
} 
// Variables declaration - do not modify      
private javax.swing.JTextField SearchBox; 
private javax.swing.JButton SearchBt; 
private javax.swing.JList jList1; 
private javax.swing.JScrollPane jScrollPane1; 
// End of variables declaration     
}  

답변

1

선택 수신기는 선택된 항목의 색인 만 알려줍니다. 그건 정상입니다. 목록은 내용에 관심이 없습니다. 목록 모델 (iaw, 실제로 표시되는 문자열 목록)을 어딘가에 저장하고 수신기의 색인 값을 사용하여 모델의 색인에서 콘텐츠를 조회해야합니다. 그것은 일반적인 패턴입니다.

첫 번째 질문은 print 문에 중단 점을 설정하고 디버그하여 stacktrace를 살펴 보겠습니다. 그렇다면이 방법이 두 번 호출 된 이유를 알 수 있습니다.

+0

정말 tnx 남자 ... 첫 번째 것을 기다리고 있습니다 ... 정말 tnx –