将Jcombobox中的选定项目添加到Jlist [英] add selected Item from Jcombobox to Jlist

查看:87
本文介绍了将Jcombobox中的选定项目添加到Jlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Jcombobox,并从数据库中填充了它,我有一个JList,我希望我在组合框中选择的每个项目都添加到JList上.我是java的新手,我试图用此代码解决问题,但它不起作用:

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

推荐答案

现在此代码可以正常运行:

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

这篇关于将Jcombobox中的选定项目添加到Jlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆