如何获得从JComboBox中选择的项目的索引? [英] How do I get the index of the item selected from a JComboBox?

查看:202
本文介绍了如何获得从JComboBox中选择的项目的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我创建JComboBox的方式-

This is how I created my JComboBox -

String[] options = {"First", "Second" , "Third"};
JComboBox optionsCombo = new JComboBox(options);

在这些项目中的一个选择,我如何得到这是选择的项目的索引?我不想要该项目被选中的项目.

When one of these items is selected, how do i get the index of the item which was selected ? I don't want the item the item that was selected.

推荐答案

使用: actionListener 内部:

 ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Selected: " + optionsCombo.getSelectedItem());
        System.out.println(", Position: " + optionsCombo.getSelectedIndex());
      }
    };
    optionsCombo.addActionListener(actionListener);

这篇关于如何获得从JComboBox中选择的项目的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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