jlistにリスナーを追加します。項目はjlistに動的に追加されます。 だから私はレジスタリスナーを傾ける。 とイベントが発生しません。 any1は私を助けることができますか? plzzさんに私に連絡してください。 私の電子メールIDは、ここで[email protected]
は私のコードです:
JList - ListSelectionListener
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;
ようこそSO :-)質問と回答は一般公開される予定ですので、ご注意ください。誰もがあなたのコードを非公開にすることを期待しないでください(それがあなたが望むものなら、コンサルタントを雇うことを考えてください:-) BTW:any1は英語のafaikではありませんし、スティッキーな疑問符は叫んでいるかもしれません... – kleopatra