Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:从列表选择列表器获取结果_Java_Netbeans 7_Jlist_Listselectionlistener - Fatal编程技术网

Java:从列表选择列表器获取结果

Java:从列表选择列表器获取结果,java,netbeans-7,jlist,listselectionlistener,Java,Netbeans 7,Jlist,Listselectionlistener,我在下面向您展示的列表选择侦听器有一些问题 1.监听器打印两次的结果表明我对此一无所知。。。!??!为什么要打印两次? 2.当我按下searchBt,结果显示,我选择了一个结果,我想从valueChanged(ListSelectionEvent e)返回ChooseIndex,但它不能有一个返回语句,并且所选的列表索引是无用的…有什么问题吗 public class SearchPage extends javax.swing.JFrame { public SearchPage() {

我在下面向您展示的列表选择侦听器有一些问题 1.监听器打印两次的结果表明我对此一无所知。。。!??!为什么要打印两次? 2.当我按下searchBt,结果显示,我选择了一个结果,我想从valueChanged(ListSelectionEvent e)返回ChooseIndex,但它不能有一个返回语句,并且所选的列表索引是无用的…有什么问题吗

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                   
}    
公共类搜索页面扩展了javax.swing.JFrame{
公共搜索页(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
SearchBox=newjavax.swing.JTextField();
SearchBt=newjavax.swing.JButton();
jScrollPane1=newjavax.swing.JScrollPane();
jList1=newjavax.swing.JList();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
SearchBt.setText(“搜索”);
SearchBt.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
已执行的搜索操作(evt);
}
});
jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE\u INTERVAL\u SELECTION);
jList1.setToolTipText(“”);
setCursor(新的java.awt.Cursor(java.awt.Cursor.DEFAULT_Cursor));
setSelectionBackground(新java.awt.Color(102,0102));
jList1.setValueIsAdjusting(真);
jList1.setVerifyInputWhenFocusTarget(假);
jScrollPane1.setViewportView(jList1);
jList1.getAccessibleContext().setAccessibleName(“”);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
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\u SIZE,543,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(SearchBox,javax.swing.GroupLayout.PREFERRED\u SIZE,244,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(SearchBt,javax.swing.GroupLayout.PREFERRED\u SIZE,140,javax.swing.GroupLayout.PREFERRED\u SIZE)))
.addContainerGap(281,简称最大值))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(125125125)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(SearchBox,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addComponent(SearchBt))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT_SIZE,315,Short.MAX_值)
.addContainerGap())
);
包装();
}//                         
private void SearchBtActionPerformed(java.awt.event.ActionEvent evt){
String tag=SearchBox.getText().trim();
向量=新向量();

对于(int i=0;i选择侦听器只告诉我们所选项目的索引。这很正常。列表不关心内容。您必须存储列表模型(根据法律,实际显示的字符串列表)然后使用侦听器中的索引值在模型中的索引处查找内容。这是常见的模式


对于第一个问题,我在print语句上设置了一个断点,调试并查看stacktrace。然后我会看到为什么该方法被调用了两次。

真的tnx人…我在等待第一个…真的tnx吗