我创建了2个JLists'addGroupList'和'addApkList'。我正在使用model.addElement(arrayList1.get(arrayList1.size() - 1))向addGroupList添加元素;问题是,我想根据addGroupList的选定值更新addApkList。为此,我正在尝试添加事件监听器,以便在选择列表项但事件不会触发时我可以采取行动。 我该怎么做才能做到这一点? 以下是我正在使用的代码。
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);
这样我就定义了JList。 以下方式我添加了addGroupList
的元素model1.addElement(arrayList1.get(arrayList1.size()-1));
以下面的方式我添加了听众。
addGroupList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {
System.out.println("Selection trigerred");
}
}
});
此代码似乎没有任何变化。我做错了什么? 我也试过了
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.
}
});
答案 0 :(得分:3)
以下方式我添加了addGroupList
的元素model1.addElement(arrayList1.get(arrayList1.size()-1));
不,你没有。 model1
是addApkList
而不是addGroupList
的列表模型:
addApkList = new JList(model1);
应为model2.addElement(arrayList1.get(arrayList1.size()-1))
。
在任何情况下,我怀疑当你只是简单地将一个项目添加到列表模型时,你期望被ListSelectionEvent被解雇。这不会发生。您需要将添加的项目设置为所选项目:
Object item = arrayList1.get(arrayList1.size()-1);
model2.addElement(item);
addGroupList.setSelectedValue(item, true);
请查看JList.setSelectedValue(Object anObject, boolean shouldScroll)了解更多详情。
答案 1 :(得分:2)
我创建了2个JLists'addGroupList'和'addApkList'。我补充说 使用addGroupList的元素 model.addElement(arrayList1.get(arrayList1.size() - 1));事情是,我 想要根据addGroupList的选定值更新addApkList。对于 这个,我正在尝试添加事件监听器,以便我可以采取行动 选择列表项但事件不是trigerring时的东西。 我该怎么做才能做到这一点?以下是我正在使用的代码。
例如
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);
}
});
}
}