JList - ListSelectionListener

1.3k views Asked by At

i want to add listener to a jlist. but items are added dynamically to jlist. so i cant register listener. and event doesnt fire. can any1 help me??? plzz contact me if u have any example. my email id [email protected]
here is my code :

DefaultListModel f=new DefaultListModel();
DefaultListModel sf=new DefaultListModel();

public Jlistdemo() {
    initComponents();

    System.out.println("hi");
    for(int i=0;i<10;i++)
    {
        f.addElement("hello"+i);
        //System.out.println("helloo"+i);
    }
    fields=new JList(f);
    jScrollPane1.setViewportView(fields);

}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    fields = new javax.swing.JList();
    jScrollPane2 = new javax.swing.JScrollPane();
    sel_fields = new javax.swing.JList();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    fields.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jScrollPane1.setViewportView(fields);

    sel_fields.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    sel_fields.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
        public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
            sel_fieldsValueChanged(evt);
        }
    });
    jScrollPane2.setViewportView(sel_fields);

    jButton1.setText(">");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setText("<");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    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(40, 40, 40)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(91, 91, 91))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))))
            .addContainerGap(68, Short.MAX_VALUE))
    );

    pack();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    if(fields.getSelectedValue()!=null)
    {
        int i=fields.getSelectedIndex();

        sf.addElement(fields.getSelectedValue());
        f.removeElement(fields.getSelectedValue());
        sel_fields=new JList(sf);
        sel_fields.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );
        jScrollPane2.setViewportView(sel_fields);
        fields.setSelectedIndex(i);
        jScrollPane2.revalidate();
        jScrollPane2.repaint();
    }
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    if(sel_fields.getSelectedValue()!=null)
    {
        int i=sel_fields.getSelectedIndex();
        f.addElement(sel_fields.getSelectedValue());
        sf.removeElementAt(sel_fields.getSelectedIndex());
        sel_fields.setSelectedIndex(i);
    }

}

private void sel_fieldsValueChanged(javax.swing.event.ListSelectionEvent evt) {
        System.out.println("fired");
}
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new Jlistdemo().setVisible(true);
        }
    });
}

private javax.swing.JList fields;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JList sel_fields;
2

There are 2 answers

0
kleopatra On BEST ANSWER

The error is the following line in your constructor:

 fields = new JList(f)

with that, you replace the list that is created in initComponents

2
Jill On

you're separating the creation of the list from adding the data.

and you don't need to keep track of selected fields, the list will do that for you.

plus, better names will help make the code clearer.