add selected Item from Jcombobox to Jlist

百般思念 提交于 2019-12-13 10:53:29

问题


i have created a Jcombobox and i have populated it from data base, i have a JList and i want every item i select in the combobox to be added on the JList. i'm new to java and i tried to solve the problem with this code but it's not working :

 private void addelementActionPerformed(java.awt.event.ActionEvent evt) {                                           

   DefaultListModel liss = new DefaultListModel();

   Object  s = empcombo_s.getSelectedItem().toString();


   for(int i =0; i<=1;i++) { 
    liss.add(i, s);
}

   stagelist.setModel(liss);
} 

回答1:


now this code is working properly :

  DefaultListModel liss = new DefaultListModel();
private void addelementActionPerformed(java.awt.event.ActionEvent evt) {                                           

   String  s = empcombo_s.getSelectedItem().toString();
  liss.addElement(s);
   stagelist.setModel(liss);
}              


来源:https://stackoverflow.com/questions/31733667/add-selected-item-from-jcombobox-to-jlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!