Question

I have created 2 JLists 'addGroupList' and 'addApkList'. I am adding elements to addGroupList using model.addElement(arrayList1.get(arrayList1.size()-1)); the thing is, I want to update addApkList based on selected value of addGroupList. For this, I'm trying to add event listener so that I can act upon something when list item is selected but the event is not trigerring. What do I do to accomplish this? following is the code I'm using.

    model1 = new DefaultListModel();
    model2 = new DefaultListModel();

    addApkList = new JList(model1);
    addGroupList = new JList(model2);

    scrollPane1 = new JScrollPane();
    scrollPane1.setViewportView(addApkList);
    scrollPane2 = new JScrollPane();
    scrollPane2.setViewportView(addGroupList);

this way i've defined JList. in following way i've added elements to addGroupList

model1.addElement(arrayList1.get(arrayList1.size()-1));

and in following way I've added listener to it.

addGroupList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent lse) {
            if (!lse.getValueIsAdjusting()) {
              System.out.println("Selection trigerred");
            }
        }
    });

There doesnt seem to happen any change with this code. what m i doing wrong? I've also tried following

model1.addListDataListener(new ListDataListener() {

            @Override
            public void intervalAdded(ListDataEvent lde) {
                System.out.println("ddddddddddd");
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void intervalRemoved(ListDataEvent lde) {
                System.out.println("ddddddddddd");
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void contentsChanged(ListDataEvent lde) {
                System.out.println("ddddddddddd");
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        });
Was it helpful?

Solution

In following way i've added elements to addGroupList

model1.addElement(arrayList1.get(arrayList1.size()-1));

No you haven't. model1 is the list model for addApkList not addGroupList:

 addApkList = new JList(model1);

It should be model2.addElement(arrayList1.get(arrayList1.size()-1)).

In any case I suspect you're expecting for a ListSelectionEvent being fired when you just simply add an item to list model. That won't happen. You need to set the added item as the selected one:

Object item = arrayList1.get(arrayList1.size()-1);
model2.addElement(item);
addGroupList.setSelectedValue(item, true);

Take a look to JList.setSelectedValue(Object anObject, boolean shouldScroll) for further details.

OTHER TIPS

I have created 2 JLists 'addGroupList' and 'addApkList'. I am adding elements to addGroupList using model.addElement(arrayList1.get(arrayList1.size()-1)); the thing is, I want to update addApkList based on selected value of addGroupList. For this, I'm trying to add event listener so that I can act upon something when list item is selected but the event is not trigerring. What do I do to accomplish this? following is the code I'm using.

for example

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Testing extends JFrame {

    private static final long serialVersionUID = 1L;
    private DefaultListModel listModel = new DefaultListModel();
    private DefaultListModel listModelEmpty = new DefaultListModel();
    private JList list = new JList(listModel);
    private JList listEmpty = new JList(listModelEmpty);
    private JPanel panel = new JPanel(new GridLayout(1, 1));
    private int currentSelectedRow = 0;
    private int xX = 0;

    public Testing() {
        setLocation(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        for (int x = 0; x < 9; x++) {
            listModel.addElement("" + x);
            xX++;
        }
        JScrollPane sp = new JScrollPane(list);
        list.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent lse) {
                if (!lse.getValueIsAdjusting()) {
                    System.out.println("Selection trigerred");
                }
            }
        });
        JScrollPane spEmpty = new JScrollPane(listEmpty);
        panel.add(spEmpty);
        panel.add(sp);
        add(panel, BorderLayout.CENTER);
        JButton btn1 = new JButton("Reset Model CastingModel");
        add(btn1, BorderLayout.NORTH);
        btn1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                //list.clearSelection();
                DefaultListModel model = (DefaultListModel) list.getModel();
                model.removeAllElements();
                for (int x = 0; x < 9; x++) {
                    model.addElement("" + (x + xX));
                    xX++;
                }
                list.setModel(model);
            }
        });

        JButton btn2 = new JButton("Reset Model directly from Model");
        add(btn2, BorderLayout.SOUTH);
        btn2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                //list.clearSelection();
                listModel.removeAllElements();
                for (int x = 0; x < 9; x++) {
                    listModel.addElement("" + (x + xX));
                    xX++;
                }
            }
        });
        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Testing().setVisible(true);
            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top