在Java swing中获得组合框值 [英] Get combobox value in Java swing

查看:238
本文介绍了在Java swing中获得组合框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Swing中获取组合框的整数值。

I need to get the integer value of the combobox in Swing.

我已将整数值设置为组合框的id.I尝试了combobox.getSelectedItem()和combobox.getSelectedIndex()但它无法获得int值。

I have set an integer value as id for the combobox.I tried combobox.getSelectedItem() and combobox.getSelectedIndex() but it cant get the int value.

以下是我的代码:

CommonBean commonBean[]=new CommonBean[commonResponse.getCommonBean().length+1];         
         for(int i=0;i<commonResponse.getCommonBean().length;i++)
         {
             commonBean[i] = new CommonBean("--Please select a project--", 0);
             commonBean[i+1] = new CommonBean(commonResponse.getCommonBean()[i].getProjectName(), commonResponse.getCommonBean()[i].getProjectId());
         }

JComboBox combobox= new JComboBox(commonBean);


public CommonBean(String projectName,int projectId) {       
        this.projectName = projectName;
        this.projectId = projectId;

    }

感谢任何帮助。

推荐答案

方法对象JComboBox.getSelectedItem()返回由对象类型,因此您必须相应地进行投射。

Method Object JComboBox.getSelectedItem() returns a value that is wrapped by Object type so you have to cast it accordingly.

语法:

YourType varName = (YourType)comboBox.getSelectedItem();`
String value = comboBox.getSelectedItem().toString();

这篇关于在Java swing中获得组合框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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