JList - 列表选择监听器
我想将侦听器添加到 jlist 中。但项目会动态添加到 jlist 中。 所以我无法注册监听器。 并且事件不会触发。 任何人都可以帮助我吗??? 如果你有任何例子请联系我。 我的电子邮件 ID [电子邮件受保护]
这是我的代码:
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误是构造函数中的以下行:
用它替换 initComponents 中创建的列表
The error is the following line in your constructor:
with that, you replace the list that is created in initComponents
您将列表的创建与添加数据分开。
并且您不需要跟踪选定的字段,该列表将为您完成此操作。
另外,更好的名称将有助于使代码更清晰。
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.