Domanda

Ho creato 2 jlist 'addgrouplist' e 'addapklist'.Sto aggiungendo elementi ad addduplist usando modello.addelement (arraylist1.get (arraylist1.size () - 1));La cosa è che voglio aggiornare ADDAPKLIST in base al valore selezionato di AddGruplist.Per questo, sto cercando di aggiungere listener di eventi in modo da poter agire su qualcosa quando viene selezionato l'elemento dell'elenco, ma l'evento non è trigilare. Cosa faccio per realizzare questo? Di seguito è riportato il codice che sto usando.

    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);
.

In questo modo ho definito Jlist. Nel modo in cui ho aggiunto gli elementi ad addduplist

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

E nel modo in cui ho aggiunto l'ascoltatore ad esso.

addGroupList.addListSelectionListener(new ListSelectionListener() {

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

Non sembra accadere alcun cambiamento con questo codice.cosa sto sbagliando? Ho anche provato a seguire

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.
            }
        });
.

È stato utile?

Soluzione

.

Nel modo in cui ho aggiunto gli elementi ad addduplist

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

No non hai.model1 è il modello di elenco per addApkList non addGroupList:

 addApkList = new JList(model1);
.

dovrebbe essere model2.addElement(arrayList1.get(arrayList1.size()-1)).

In ogni caso, sospetto che ti aspetti per un ListseseseSelectionEvent viene licenziato quando si aggiunge semplicemente un elemento al modello di elenco.Che non succederà.È necessario impostare l'elemento aggiunto come quello selezionato:

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

Dai un'occhiata a jlist.seselectedValue (oggetto anobject, boolean secrsoll) per ulteriori dettagli.

Altri suggerimenti

.

Ho creato 2 jlist 'addgrouplist' e 'addapklist'.Sto aggiungendo ELEMENTI PER ADDGRAULLIST UTILIZZO modello.addelement (arraylist1.get (arraylist1.size () - 1));La cosa è, io Volete aggiornare ADDAPKLIST in base al valore selezionato di addgrouplumpist.Per Questo, sto cercando di aggiungere listener di eventi in modo da poter agire Qualcosa quando è selezionato l'elemento dell'elenco ma l'evento non è trigilare. Cosa faccio per realizzare questo?Di seguito è riportato il codice che sto usando.

Ad esempio

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);
            }
        });
    }
}
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top